@Test public void testOnlyFirstOfTwoFailing(final TestContext testContext) throws Exception { final Async async = testContext.async(); shortCircuitedFuture(Arrays.asList( indexedFutureSupplier(1, false), indexedFutureSupplier(2, true)) .stream(), false) .thenAccept(b -> executionContext.runOnContext(() -> { assertThat(b, is(false)); assertThat(endStep.get(), is(1)); async.complete(); })); }
public static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers, final Boolean fallbackOn) { return futureSuppliers.reduce(completedFuture(!fallbackOn), (f, a) -> f.thenCompose(b -> (b != fallbackOn) ? a.get() : completedFuture(fallbackOn)), (bf1, bf2) -> bf1.thenCompose(b -> (b != fallbackOn) ? bf2 : CompletableFuture.completedFuture(fallbackOn))); }
FutureUtils { public static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers, final Boolean fallbackOn) { return futureSuppliers.reduce(completedFuture(!fallbackOn), (f, a) -> f.thenCompose(b -> (b != fallbackOn) ? a.get() : completedFuture(fallbackOn)), (bf1, bf2) -> bf1.thenCompose(b -> (b != fallbackOn) ? bf2 : CompletableFuture.completedFuture(fallbackOn))); } }
FutureUtils { public static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers, final Boolean fallbackOn) { return futureSuppliers.reduce(completedFuture(!fallbackOn), (f, a) -> f.thenCompose(b -> (b != fallbackOn) ? a.get() : completedFuture(fallbackOn)), (bf1, bf2) -> bf1.thenCompose(b -> (b != fallbackOn) ? bf2 : CompletableFuture.completedFuture(fallbackOn))); }  }
FutureUtils { public static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers, final Boolean fallbackOn) { return futureSuppliers.reduce(completedFuture(!fallbackOn), (f, a) -> f.thenCompose(b -> (b != fallbackOn) ? a.get() : completedFuture(fallbackOn)), (bf1, bf2) -> bf1.thenCompose(b -> (b != fallbackOn) ? bf2 : CompletableFuture.completedFuture(fallbackOn))); }  static CompletableFuture<Void> allOf(Stream<CompletableFuture<T>> futures); static CompletableFuture<List<T>> combineFuturesToList(List<CompletableFuture<T>> futureList); static CompletableFuture<T> withFallback(final CompletableFuture<T> originalFuture,
                                                                          final Supplier<CompletableFuture<T>> fallbackFutureSupplier); static CompletableFuture<Boolean> allInSequence(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers); static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers,
                                                                  final Boolean fallbackOn); }
FutureUtils { public static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers, final Boolean fallbackOn) { return futureSuppliers.reduce(completedFuture(!fallbackOn), (f, a) -> f.thenCompose(b -> (b != fallbackOn) ? a.get() : completedFuture(fallbackOn)), (bf1, bf2) -> bf1.thenCompose(b -> (b != fallbackOn) ? bf2 : CompletableFuture.completedFuture(fallbackOn))); }  static CompletableFuture<Void> allOf(Stream<CompletableFuture<T>> futures); static CompletableFuture<List<T>> combineFuturesToList(List<CompletableFuture<T>> futureList); static CompletableFuture<T> withFallback(final CompletableFuture<T> originalFuture,
                                                                          final Supplier<CompletableFuture<T>> fallbackFutureSupplier); static CompletableFuture<Boolean> allInSequence(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers); static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers,
                                                                  final Boolean fallbackOn);  }