Package software.amazon.awssdk.utils
Class CompletableFutureUtils
java.lang.Object
software.amazon.awssdk.utils.CompletableFutureUtils
Utility class for working with
CompletableFuture.-
Method Summary
Modifier and TypeMethodDescriptionstatic CompletableFuture<Void> allOfExceptionForwarded(CompletableFuture<?>[] futures) Similar toCompletableFuture.allOf(CompletableFuture[]), but when any future is completed exceptionally, forwards the exception to other futures.static CompletionExceptionWraps the given error in aCompletionExceptionif necessary.static <U> CompletableFuture<U> Convenience method for creating a future that is immediately completed exceptionally with the givenThrowable.static <T> CompletableFuture<T> forwardExceptionTo(CompletableFuture<T> src, CompletableFuture<?> dst) Forward theThrowablefromsrctodst.static <T> CompletableFuture<T> forwardResultTo(CompletableFuture<T> src, CompletableFuture<T> dst) Completes thedstfuture based on the result of thesrcfuture asynchronously on the providedExecutorand return thesrcfuture.static <T> CompletableFuture<T> forwardResultTo(CompletableFuture<T> src, CompletableFuture<T> dst, Executor executor) Completes thedstfuture based on the result of thesrcfuture asynchronously on the providedExecutorand return thesrcfuture.static <T> CompletableFuture<T> forwardTransformedExceptionTo(CompletableFuture<T> src, CompletableFuture<?> dst, Function<Throwable, Throwable> transformationFunction) Forward theThrowablethat can be transformed as per the transformationFunction fromsrctodst.static <SourceT,DestT>
CompletableFuture<SourceT> forwardTransformedResultTo(CompletableFuture<SourceT> src, CompletableFuture<DestT> dst, Function<SourceT, DestT> function) Completes thedstfuture based on the result of thesrcfuture, synchronously, after applying the provided transformationFunctionif successful.static <T> TjoinInterruptibly(CompletableFuture<T> future) static voidstatic <T> TjoinLikeSync(CompletableFuture<T> future) Joins (interruptibly) on the future, and re-throws any RuntimeExceptions or Errors just like the async task would have thrown if it was executed synchronously.
-
Method Details
-
failedFuture
Convenience method for creating a future that is immediately completed exceptionally with the givenThrowable.Similar to
CompletableFuture#failedFuturewhich was added in Java 9.- Type Parameters:
U- The type of the element.- Parameters:
t- The failure.- Returns:
- The failed future.
-
errorAsCompletionException
Wraps the given error in aCompletionExceptionif necessary. Useful if an exception needs to be rethrown from withinCompletableFuture.handle(java.util.function.BiFunction)or similar methods.- Parameters:
t- The error.- Returns:
- The error as a CompletionException.
-
forwardExceptionTo
public static <T> CompletableFuture<T> forwardExceptionTo(CompletableFuture<T> src, CompletableFuture<?> dst) Forward theThrowablefromsrctodst.- Parameters:
src- The source of theThrowable.dst- The destination where theThrowablewill be forwarded to.- Returns:
src.
-
forwardTransformedExceptionTo
public static <T> CompletableFuture<T> forwardTransformedExceptionTo(CompletableFuture<T> src, CompletableFuture<?> dst, Function<Throwable, Throwable> transformationFunction) Forward theThrowablethat can be transformed as per the transformationFunction fromsrctodst.- Parameters:
src- The source of theThrowable.dst- The destination where theThrowablewill be forwarded totransformationFunction- Transformation function taht will be applied on to the forwarded exception.- Returns:
-
forwardResultTo
public static <T> CompletableFuture<T> forwardResultTo(CompletableFuture<T> src, CompletableFuture<T> dst) Completes thedstfuture based on the result of thesrcfuture asynchronously on the providedExecutorand return thesrcfuture.- Parameters:
src- The sourceCompletableFuturedst- The destination where theThrowableor response will be forwarded to.- Returns:
- the
srcfuture.
-
forwardResultTo
public static <T> CompletableFuture<T> forwardResultTo(CompletableFuture<T> src, CompletableFuture<T> dst, Executor executor) Completes thedstfuture based on the result of thesrcfuture asynchronously on the providedExecutorand return thesrcfuture.- Parameters:
src- The sourceCompletableFuturedst- The destination where theThrowableor response will be forwarded to.executor- the executor to complete the des future- Returns:
- the
srcfuture.
-
forwardTransformedResultTo
public static <SourceT,DestT> CompletableFuture<SourceT> forwardTransformedResultTo(CompletableFuture<SourceT> src, CompletableFuture<DestT> dst, Function<SourceT, DestT> function) Completes thedstfuture based on the result of thesrcfuture, synchronously, after applying the provided transformationFunctionif successful.- Parameters:
src- The sourceCompletableFuturedst- The destination where theThrowableor transformed result will be forwarded to.- Returns:
- the
srcfuture.
-
allOfExceptionForwarded
Similar toCompletableFuture.allOf(CompletableFuture[]), but when any future is completed exceptionally, forwards the exception to other futures.- Parameters:
futures- The futures.- Returns:
- The new future that is completed when all the futures in
futuresare.
-
joinInterruptibly
-
joinInterruptiblyIgnoringFailures
-
joinLikeSync
Joins (interruptibly) on the future, and re-throws any RuntimeExceptions or Errors just like the async task would have thrown if it was executed synchronously.
-