package test.nw;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

public class FutureTest {
	
	static AtomicInteger erro = new AtomicInteger(0);
	
	public static void main(String[] args) {
		String result =  call("aaa")
				.join();
		System.out.println(result);
		
	}
	
	private static CompletableFuture<String> call(String user) {
		
	    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
	    	int i= 1/0;
	    	return "resl";
	    });
	    
	    return future.handleAsync((String result, Throwable ex) -> {
	        // or any other Predicate that is satisfied against ex
	        if(ex != null) {
	        	ex.printStackTrace();
	        	System.out.println(erro.get());
	        	erro.getAndIncrement();
	            return call(user);
	        } else {
	            return future;
	        }
	    }).thenCompose(Function.identity());
	}

}
