package pl.ciruk.blog.flow;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

public class CompletableFutureFlow {

    public static final int MAXIMUM_POOL_SIZE = 4;

    static CompletableFuture<Integer> flowWithId(int id, ExecutorService pool) {
        return firstOperation(id, pool)
                .thenCompose(__ -> secondOperation(id, pool))
                .thenCompose(__ -> thirdOperation(id, pool));
    }

    private static CompletableFuture<Integer> firstOperation(int id, ExecutorService pool) {
        return slowOperationAsync(id, 1, pool);
    }

    private static CompletableFuture<Integer> secondOperation(int id, ExecutorService pool) {
        return slowOperationAsync(id, 2, pool);
    }

    private static CompletableFuture<Integer> thirdOperation(int id, ExecutorService pool) {
        return slowOperationAsync(id, 3, pool);
    }

    static void getFuture(CompletableFuture<Integer> future) {
        try {
            future.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    private static int slowOperation(int flow, int step) {
        System.out.println(String.format("[%d.%d] - %s", flow, step, Thread.currentThread().getName()));
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return flow;
    }

    private static CompletableFuture<Integer> slowOperationAsync(int flow, int step, ExecutorService pool) {
        return CompletableFuture.supplyAsync(
                () -> slowOperation(flow, step),
                pool
        );
    }
}

@@ -205,60 +205,8 @@
  %7B%0A%0A
-    public static final int MAXIMUM_POOL_SIZE = 4;%0A%0A
     
@@ -652,320 +652,115 @@
 r%3E s
-econdOperation(int id, ExecutorService pool) %7B%0A        return slowOperationAsync(id, 2, pool);%0A    %7D%0A%0A    private static CompletableFuture%3CInteger%3E thirdOperation(int id, ExecutorService pool) %7B%0A        return slowOperationAsync(id, 3, pool);%0A    %7D%0A%0A    static void getFuture(CompletableFuture%3CInteger%3E future) %7B
+lowOperationAsync(int flow, int step, ExecutorService pool) %7B%0A        return CompletableFuture.supplyAsync(
 %0A   
@@ -768,136 +768,69 @@
     
-try %7B%0A
+ 
        
-     future.get();%0A        %7D catch (InterruptedException %7C ExecutionException e) %7B%0A            e.printStackTrace();
+() -%3E slowOperation(flow, step),%0A                pool
 %0A   
@@ -834,17 +834,18 @@
         
-%7D
+);
 %0A    %7D%0A%0A
@@ -950,11 +950,22 @@
 t(%22%5B
-%25d.
+Flow:%25d%5D%5BStep:
 %25d%5D 
@@ -1103,24 +1103,76 @@
 eption e) %7B%0A
+            Thread.currentThread().isInterrupted();%0A
             
@@ -1282,185 +1282,457 @@
 r%3E s
-lowOperationAsync(int flow, int step, ExecutorService pool) %7B%0A        return CompletableFuture.supplyAsync(%0A                () -%3E slowOperation(flow, step),%0A                pool
+econdOperation(int id, ExecutorService pool) %7B%0A        return slowOperationAsync(id, 2, pool);%0A    %7D%0A%0A    private static CompletableFuture%3CInteger%3E thirdOperation(int id, ExecutorService pool) %7B%0A        return slowOperationAsync(id, 3, pool);%0A    %7D%0A%0A    static void getFuture(CompletableFuture%3CInteger%3E future) %7B%0A        try %7B%0A            future.get();%0A        %7D catch (InterruptedException %7C ExecutionException e) %7B%0A            e.printStackTrace();
 %0A   
@@ -1732,23 +1732,22 @@
 ();%0A        
-);
+%7D
 %0A    %7D%0A%7D%0A

Reorder methods for convenience