Package software.amazon.awssdk.utils
Class CompletableFutureUtils
- java.lang.Object
-
- software.amazon.awssdk.utils.CompletableFutureUtils
-
public final class CompletableFutureUtils extends Object
Utility class for working withCompletableFuture
.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static CompletableFuture<Void>
allOfExceptionForwarded(CompletableFuture<?>[] futures)
Similar toCompletableFuture.allOf(CompletableFuture[])
, but when any future is completed exceptionally, forwards the exception to other futures.static CompletionException
errorAsCompletionException(Throwable t)
Wraps the given error in aCompletionException
if necessary.static <U> CompletableFuture<U>
failedFuture(Throwable t)
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 theThrowable
fromsrc
todst
.static <T> CompletableFuture<T>
forwardResultTo(CompletableFuture<T> src, CompletableFuture<T> dst)
Completes thedst
future based on the result of thesrc
future asynchronously on the providedExecutor
and return thesrc
future.static <T> CompletableFuture<T>
forwardResultTo(CompletableFuture<T> src, CompletableFuture<T> dst, Executor executor)
Completes thedst
future based on the result of thesrc
future asynchronously on the providedExecutor
and return thesrc
future.static <T> CompletableFuture<T>
forwardTransformedExceptionTo(CompletableFuture<T> src, CompletableFuture<?> dst, Function<Throwable,Throwable> transformationFunction)
Forward theThrowable
that can be transformed as per the transformationFunction fromsrc
todst
.static <SourceT,DestT>
CompletableFuture<SourceT>forwardTransformedResultTo(CompletableFuture<SourceT> src, CompletableFuture<DestT> dst, Function<SourceT,DestT> function)
Completes thedst
future based on the result of thesrc
future, synchronously, after applying the provided transformationFunction
if successful.static <T> T
joinInterruptibly(CompletableFuture<T> future)
static void
joinInterruptiblyIgnoringFailures(CompletableFuture<?> future)
static <T> T
joinLikeSync(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 Detail
-
failedFuture
public static <U> CompletableFuture<U> failedFuture(Throwable t)
Convenience method for creating a future that is immediately completed exceptionally with the givenThrowable
.Similar to
CompletableFuture#failedFuture
which was added in Java 9.- Type Parameters:
U
- The type of the element.- Parameters:
t
- The failure.- Returns:
- The failed future.
-
errorAsCompletionException
public static CompletionException errorAsCompletionException(Throwable t)
Wraps the given error in aCompletionException
if 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 theThrowable
fromsrc
todst
.- Parameters:
src
- The source of theThrowable
.dst
- The destination where theThrowable
will be forwarded to.- Returns:
src
.
-
forwardTransformedExceptionTo
public static <T> CompletableFuture<T> forwardTransformedExceptionTo(CompletableFuture<T> src, CompletableFuture<?> dst, Function<Throwable,Throwable> transformationFunction)
Forward theThrowable
that can be transformed as per the transformationFunction fromsrc
todst
.- Parameters:
src
- The source of theThrowable
.dst
- The destination where theThrowable
will 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 thedst
future based on the result of thesrc
future asynchronously on the providedExecutor
and return thesrc
future.- Parameters:
src
- The sourceCompletableFuture
dst
- The destination where theThrowable
or response will be forwarded to.- Returns:
- the
src
future.
-
forwardResultTo
public static <T> CompletableFuture<T> forwardResultTo(CompletableFuture<T> src, CompletableFuture<T> dst, Executor executor)
Completes thedst
future based on the result of thesrc
future asynchronously on the providedExecutor
and return thesrc
future.- Parameters:
src
- The sourceCompletableFuture
dst
- The destination where theThrowable
or response will be forwarded to.executor
- the executor to complete the des future- Returns:
- the
src
future.
-
forwardTransformedResultTo
public static <SourceT,DestT> CompletableFuture<SourceT> forwardTransformedResultTo(CompletableFuture<SourceT> src, CompletableFuture<DestT> dst, Function<SourceT,DestT> function)
Completes thedst
future based on the result of thesrc
future, synchronously, after applying the provided transformationFunction
if successful. If the function threw an exception, the destination future will be completed exceptionally with that exception.- Parameters:
src
- The sourceCompletableFuture
dst
- The destination where theThrowable
or transformed result will be forwarded to.- Returns:
- the
src
future.
-
allOfExceptionForwarded
public static CompletableFuture<Void> allOfExceptionForwarded(CompletableFuture<?>[] futures)
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
futures
are.
-
joinInterruptibly
public static <T> T joinInterruptibly(CompletableFuture<T> future)
-
joinInterruptiblyIgnoringFailures
public static void joinInterruptiblyIgnoringFailures(CompletableFuture<?> future)
-
joinLikeSync
public static <T> T joinLikeSync(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.
-
-