package com.millross.blog.async;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 *
 */
public class SimpleCompletableFutureTest {

    @Test
    public void testSimpleFutureCompletion() throws Exception {
        final CompletableFuture<Integer> future = blockingSuccessFuture(1);
        assertThat(future.get(), is(1));
    }

    @Test
    public void demoThenApply() throws Exception {
        final CompletableFuture<Integer> future = blockingSuccessFuture(1)
                .thenApply(i -> i + 3);
        assertThat(future.get(), is(4));
    }

    @Test
    public void demoThenCompose() throws Exception {
        // We'll use one future to specify how long a subsequent future will take
        final CompletableFuture<Integer> future = blockingSuccessFuture(3)
                .thenApply(i -> {
                    System.out.println("First future completed, " + i);
                    return i;
                })
                .thenCompose(i ->  specifiedDelayFuture(i * 100, 2));
        assertThat(future.get(), is(2));
    }

    public CompletableFuture<Integer> blockingSuccessFuture(final int value) {
        final CompletableFuture<Integer> future = new CompletableFuture<Integer>();
        final Thread futureThread = new Thread(() -> {
            try {
                Thread.sleep(1000);
                future.complete(value);
            } catch (Throwable t) {
                future.completeExceptionally(t);
            }

        });
        // Kick off processing
        futureThread.run();
        return future;
    }

    public CompletableFuture<Integer> specifiedDelayFuture(final int delay, final int finalValue) {
        final CompletableFuture<Integer> future = new CompletableFuture<>();
        final Thread futureThread = new Thread(() -> {
            try {
                Thread.sleep(delay);
                future.complete(finalValue);
            } catch (Throwable t) {
                future.completeExceptionally(t);
            }

        });
        // Kick off processing
        futureThread.run();
        return future;
    }

}

@@ -102,160 +102,533 @@
 re;%0A
-%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0A%0A/**%0A *%0A */%0Apublic class SimpleCompletableFutureTest %7B
+import java.util.concurrent.Executor;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.function.Supplier;%0A%0Aimport static java.util.concurrent.CompletableFuture.*;%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0A%0A/**%0A *%0A */%0Apublic class SimpleCompletableFutureTest %7B%0A%0A    // We'll allow a 3-sized thread pool for async execution, rather than using the fork/join pool for asynchronous%0A    // value generation%0A    final Executor executor = Executors.newFixedThreadPool(3);
 %0A%0A  
@@ -749,39 +749,61 @@
 uture = 
-blockingSuccessFuture(1
+supplyAsync(delayedValueSupplier(1), executor
 );%0A     
@@ -960,31 +960,53 @@
 e = 
-blockingSuccessFuture(1
+supplyAsync(delayedValueSupplier(1), executor
 )%0A  
@@ -1290,31 +1290,53 @@
 e = 
-blockingSuccessFuture(3
+supplyAsync(delayedValueSupplier(3), executor
 )%0A  
@@ -1521,17 +1521,16 @@
 ose(i -%3E
- 
  specifi
@@ -1620,391 +1620,272 @@
 lic 
-CompletableFuture%3CInteger%3E blockingSuccessFuture(final int value) %7B%0A        final CompletableFuture%3CInteger%3E future = new CompletableFuture%3CInteger%3E();%0A        final Thread futureThread = new Thread(() -%3E %7B%0A            try %7B%0A                Thread.sleep(1000);%0A                future.complete(value);%0A            %7D catch (Throwable t) %7B%0A                future.completeExceptionally(t
+Supplier%3CInteger%3E delayedValueSupplier(final int value) %7B%0A        return () -%3E %7B%0A            try %7B%0A                Thread.sleep(1000);%0A            %7D catch (InterruptedException e) %7B%0A                throw new RuntimeException(%22Problem while waiting to return value%22
 );%0A 
@@ -1893,33 +1893,32 @@
           %7D%0A
-%0A
         
 %7D);%0A        
@@ -1909,92 +1909,35 @@
     
-%7D);%0A        // Kick off processing%0A        futureThread.run();%0A        return future
+    return value;%0A        %7D
 ;%0A  

Replace blockingSuccessFuture with use of an existing CompletableFuture factory method. Let's not roll our own futures where we don't need to!