Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RunWith(JUnit4.class)
- public class ExceptionHandlingTests {
- class ShortCircuitCF<T> {
- final CompletableFuture<T> result = new CompletableFuture<>();
- final CompletableFuture<T> notCompleted = new CompletableFuture<>();
- CompletableFuture<T> propagate(T value) {
- result.complete(value);
- return notCompleted;
- }
- CompletableFuture<T> complete(T value) {
- return CompletableFuture.completedFuture(value);
- }
- CompletableFuture<T> getResult() {
- return result;
- }
- }
- @Test
- public void test1() {
- for(int shortCutAt: IntStream.range(0, 4).toArray()) {
- System.out.println("Example execution with "
- + (shortCutAt == 0 ? "no shortcut" : "shortcut at " + shortCutAt));
- ShortCircuitCF<Integer> result = new ShortCircuitCF<>();
- CompletableFuture.completedFuture(null).thenCompose(justVoid -> { // runAsync
- System.out.println("Completing result1. Result: " + result.getResult().isDone());
- if (shortCutAt == 1) {
- return result.propagate(10);
- }
- return result.complete(null);
- }).thenCompose(x -> {
- System.out.println("Completing result2. Result: " + result.getResult().isDone());
- if (shortCutAt == 2) {
- return result.propagate(10);
- }
- return result.complete(5);
- }).thenCompose(x -> {
- System.out.println("Completing result3. Result: " + result.getResult().isDone());
- if (shortCutAt == 3) {
- return result.propagate(10);
- }
- return result.complete(5);
- }).applyToEither(result.getResult(), Function.identity())
- .thenAccept(fr -> System.out.println("final result: " + fr));
- System.out.println();
- }
- }
- }
Add Comment
Please, Sign In to add comment