@Test public void testOptionalFutureFailure() { FailedRequestException exception = new FailedRequestException("a tag", 500); CompletableFuture<Integer> future = new CompletableFuture<>(); future.completeExceptionally(exception); CompletableFuture<Optional<Integer>> optFuture = FutureUtils.optionalFuture(future); expectFailedFuture(optFuture, exception); }
public static <T> CompletableFuture<Optional<T>> optionalFuture(CompletableFuture<? extends T> future) { CompletableFuture<Optional<T>> optFuture = future.thenApply(Optional::of); return wrapFutureForConditionalFallback(optFuture, FutureUtils::resourceNotFound, () -> CompletableFuture.completedFuture(Optional.empty())); }
FutureUtils { public static <T> CompletableFuture<Optional<T>> optionalFuture(CompletableFuture<? extends T> future) { CompletableFuture<Optional<T>> optFuture = future.thenApply(Optional::of); return wrapFutureForConditionalFallback(optFuture, FutureUtils::resourceNotFound, () -> CompletableFuture.completedFuture(Optional.empty())); } }
FutureUtils { public static <T> CompletableFuture<Optional<T>> optionalFuture(CompletableFuture<? extends T> future) { CompletableFuture<Optional<T>> optFuture = future.thenApply(Optional::of); return wrapFutureForConditionalFallback(optFuture, FutureUtils::resourceNotFound, () -> CompletableFuture.completedFuture(Optional.empty())); } private  FutureUtils(); }
FutureUtils { public static <T> CompletableFuture<Optional<T>> optionalFuture(CompletableFuture<? extends T> future) { CompletableFuture<Optional<T>> optFuture = future.thenApply(Optional::of); return wrapFutureForConditionalFallback(optFuture, FutureUtils::resourceNotFound, () -> CompletableFuture.completedFuture(Optional.empty())); } private  FutureUtils(); static T block(CompletableFuture<? extends T> future); static CompletableFuture<T> wrapFutureForConditionalFallback(CompletableFuture<T> future,
                                                                            Predicate<? super Throwable> condition,
                                                                            Supplier<? extends CompletableFuture<T>> fallback); static CompletableFuture<Optional<T>> optionalFuture(CompletableFuture<? extends T> future); static CompletableFuture<T> orFallback(CompletableFuture<Optional<T>> future,
                                                      Supplier<? extends CompletableFuture<T>> fallback); static CompletableFuture<Optional<T>> orRetry(CompletableFuture<Optional<T>> future,
                                                             Supplier<? extends CompletableFuture<Optional<T>>> retry); static boolean isFailedRequestWithStatus(Throwable exception, int statusCode); static CompletableFuture<T> failedFuture(Throwable ex); static CompletableFuture<Collection<T>> sequence(Collection<CompletableFuture<T>> futures,
                                                                Function<Throwable, Boolean> exceptionHandler); static CompletableFuture<T> wrapInFuture(Callable<? extends T> action, String errMsg); }
FutureUtils { public static <T> CompletableFuture<Optional<T>> optionalFuture(CompletableFuture<? extends T> future) { CompletableFuture<Optional<T>> optFuture = future.thenApply(Optional::of); return wrapFutureForConditionalFallback(optFuture, FutureUtils::resourceNotFound, () -> CompletableFuture.completedFuture(Optional.empty())); } private  FutureUtils(); static T block(CompletableFuture<? extends T> future); static CompletableFuture<T> wrapFutureForConditionalFallback(CompletableFuture<T> future,
                                                                            Predicate<? super Throwable> condition,
                                                                            Supplier<? extends CompletableFuture<T>> fallback); static CompletableFuture<Optional<T>> optionalFuture(CompletableFuture<? extends T> future); static CompletableFuture<T> orFallback(CompletableFuture<Optional<T>> future,
                                                      Supplier<? extends CompletableFuture<T>> fallback); static CompletableFuture<Optional<T>> orRetry(CompletableFuture<Optional<T>> future,
                                                             Supplier<? extends CompletableFuture<Optional<T>>> retry); static boolean isFailedRequestWithStatus(Throwable exception, int statusCode); static CompletableFuture<T> failedFuture(Throwable ex); static CompletableFuture<Collection<T>> sequence(Collection<CompletableFuture<T>> futures,
                                                                Function<Throwable, Boolean> exceptionHandler); static CompletableFuture<T> wrapInFuture(Callable<? extends T> action, String errMsg);  }