Class FutureUtils
CompletableFuture.- Since:
- 5.0.0
- Author:
- Allard Buijze
-
Method Summary
Modifier and TypeMethodDescriptionstatic CompletableFuture<Void> allOrEmpty(List<CompletableFuture<Void>> futures) Combines the givenfuturesinto a singleCompletableFuturethat completes once all of them complete, returning an already completed future whenfuturesis empty.static <T> BiConsumer<T, Throwable> alsoComplete(CompletableFuture<T> future) Creates a function that can be passed to aCompletableFuture.whenComplete(BiConsumer)orCompletableFuture.whenCompleteAsync(BiConsumer)invocation, to complete the givenfuturewith the same result as theCompletableFuturethat this function is passed to.static <T> CompletableFuture<T> Creates a completedCompletableFuturewithnullas result.static <T> VoidignoreResult(T toIgnore) Utility method that doesn't do anything.static <T> @Nullable TjoinAndUnwrap(CompletableFuture<T> future) Joins aCompletableFutureand unwraps anyCompletionExceptionto throw the actual cause, preserving the exact exception type without wrapping checked exceptions.static <T> TjoinAndUnwrap(CompletableFuture<T> future, Duration timeout) Joins aCompletableFutureand unwraps anyCompletionExceptionto throw the actual cause, preserving the exact exception type without wrapping checked exceptions.static <T> CompletableFuture<T> runFailing(Supplier<CompletableFuture<T>> fn) Safely catches exceptions thrown by the givenfnand returns aCompletableFuturethat completes.static ThrowableUnwrap givenexceptionfrom the exception-wrappers added byCompletableFuture.
-
Method Details
-
ignoreResult
Utility method that doesn't do anything. Its purpose is to simplify conversion of aCompletableFuturewith any generic type to aCompletableFuture<Void>.Example:
completableFuture.thenApply(FutureUtils::ignoreResult- Type Parameters:
T- The declared result of theCompletableFutureto ignore the result of.- Parameters:
toIgnore- The actual result of theCompletableFuture.- Returns:
null, as that's the only valid value forVoid.
-
emptyCompletedFuture
Creates a completedCompletableFuturewithnullas result.- Type Parameters:
T- The declared type to return in theCompletableFuture.- Returns:
- A
CompletableFuturethat is completed withnull.
-
allOrEmpty
Combines the givenfuturesinto a singleCompletableFuturethat completes once all of them complete, returning an already completed future whenfuturesis empty.This is a drop-in alternative to
CompletableFuture.allOf(CompletableFuture[])for code paths that routinely deal with empty collections, such as fan-out over a dynamic set of handlers, subscribers, or segments. It avoids allocating an array and a dependent future for a combination that has nothing to wait for.The returned future completes exceptionally with a
CompletionExceptionas soon as any of the givenfuturescompletes exceptionally, matching the behavior ofCompletableFuture.allOf(CompletableFuture[]).- Parameters:
futures- the futures to combine- Returns:
- a future that completes once every future in
futurescompletes, or a completed future iffuturesis empty - Since:
- 5.3.0
-
alsoComplete
Creates a function that can be passed to aCompletableFuture.whenComplete(BiConsumer)orCompletableFuture.whenCompleteAsync(BiConsumer)invocation, to complete the givenfuturewith the same result as theCompletableFuturethat this function is passed to.- Type Parameters:
T- The declared type of result from theCompletableFuture.- Parameters:
future- TheCompletableFutureto also complete.- Returns:
- A function that completes another
futurewith the same results.
-
unwrap
Unwrap givenexceptionfrom the exception-wrappers added byCompletableFuture.More specifically, if the given
exceptionis aCompletionExceptionorExecutionException, it returns the cause. Otherwise, it will return the exception as-is.- Parameters:
exception- The exception to unwrap.- Returns:
- The unwrapped exception if the given
exceptionis of typeCompletionExceptionorExecutionException. Otherwise, it is returned as is.
-
runFailing
Safely catches exceptions thrown by the givenfnand returns aCompletableFuturethat completes.- Type Parameters:
T- Type of the completable future.- Parameters:
fn- A lambda returning aCompletableFuture.- Returns:
- A completable future that completes exceptionally if the given lambda throws an exception.
-
joinAndUnwrap
Joins aCompletableFutureand unwraps anyCompletionExceptionto throw the actual cause, preserving the exact exception type without wrapping checked exceptions.Applies a default safety-net timeout of 30 seconds. If the future does not complete within that window, a
TimeoutExceptionis thrown. Callers that expect longer completion times should usejoinAndUnwrap(CompletableFuture, Duration)with an explicit timeout.- Type Parameters:
T- The type of the future's result.- Parameters:
future- TheCompletableFutureto join.- Returns:
- The result of the future.
- Throws:
TimeoutException- if the future does not complete within the default timeout.Throwable- the unwrapped cause if the future completed exceptionally (exact type preserved).
-
joinAndUnwrap
Joins aCompletableFutureand unwraps anyCompletionExceptionto throw the actual cause, preserving the exact exception type without wrapping checked exceptions. If the future does not complete within the giventimeout, aTimeoutExceptionis thrown.This method uses
CompletableFuture.orTimeout(long, TimeUnit)to enforce the deadline. On futures that are already complete (the common case), the timeout is a no-op.- Type Parameters:
T- The type of the future's result.- Parameters:
future- TheCompletableFutureto join.timeout- The maximum time to wait for the future to complete.- Returns:
- The result of the future, or
nullif the future completed with anullvalue. - Throws:
TimeoutException- if the future does not complete within the giventimeout.Throwable- the unwrapped cause if the future completed exceptionally (exact type preserved).
-