Class Futures
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> BiConsumer<T,Throwable> copyHandler(CompletableFuture<T> copyFuture) Creates a future handler that will copy the state of the handled future to the given future.static <T> CompletableFuture<T>create(Supplier<CompletableFuture<T>> initiator) Create aCompletableFutureusing the given supplier.static CompletableFuture<?>firstFailureOrAllOf(List<CompletableFuture<?>> allFutures) static ThrowablegetThrowableNow(CompletableFuture<?> future) static <T> BiConsumer<T,Throwable> handler(BiConsumer<T, Throwable> delegate) Creates a future handler that will delegate to the givenBiConsumerafter having unwrapped the throwable passed as input if it is aCompletionException.static <T,R> BiFunction<T, Throwable, R> handler(BiFunction<T, Throwable, R> delegate) Creates a future handler that will delegate to the givenBiFunctionafter having unwrapped the throwable passed as input if it is aCompletionException.Creates a future handler that will delegate to the givenFunctionafter having unwrapped the throwable passed as input if it is aCompletionException.static CompletableFuture<Void>runAsync(Runnable runnable, ExecutorService executor) Similar toCompletableFuture.runAsync(Runnable, Executor), but callingCompletableFuture.cancel(boolean)on the returned future actually has an effect and may interrupt the thread.static <T,R> Function<T, CompletionStage<R>> safeComposer(Function<? super T, ? extends CompletionStage<R>> delegate) Creates a composition function that will delegate to the givenFunctionbut will catch any exception during composition to return a future completed exceptionally.static <T> TunwrappedExceptionGet(Future<T> future) CallCompletableFuture.get()and unwrap anyExecutionException, expecting the exception to be aRuntimeException.static <T> TunwrappedExceptionJoin(CompletableFuture<T> future) CallCompletableFuture.join()and unwrap anyCompletionException, expecting the exception to be aRuntimeException.static <T> CompletableFuture<T>whenCompleteExecute(CompletableFuture<?> self, Supplier<? extends CompletionStage<T>> action) Compose the given future with another as soon as it's complete, regardless of errors, and return a completable future that will take errors of both parent futures into account (usingThrowable.addSuppressed(Throwable)if need be).
-
Method Details
-
create
Create aCompletableFutureusing the given supplier.This method is guaranteed to never throw any exception: any exception thrown by the given supplier will instead complete the resulting future
exceptionally.This is useful in particular if you want to handle errors during the
CompletableFuturecreation the same way as errors thrown during post-processing operations (for instance operations passed toCompletableFuture.thenApply(Function)).- Parameters:
initiator- A supplier that will initiate (synchronously) the asynchronous operation.- Returns:
- A
CompletableFuturewrapping the result of both the initiation and execution of the operation.
-
handler
Creates a future handler that will delegate to the givenFunctionafter having unwrapped the throwable passed as input if it is aCompletionException.This method is meant to be used in conjunction with
CompletableFuture.exceptionally(Function).- Parameters:
delegate- The exception function to delegate to- Returns:
- The new, delegating exception function.
-
handler
Creates a future handler that will delegate to the givenBiFunctionafter having unwrapped the throwable passed as input if it is aCompletionException.This method is meant to be used in conjunction with
CompletableFuture.handle(BiFunction).- Parameters:
delegate- The handler to delegate to- Returns:
- The new, delegating handler.
-
handler
Creates a future handler that will delegate to the givenBiConsumerafter having unwrapped the throwable passed as input if it is aCompletionException.This method is meant to be used in conjunction with
CompletableFuture.whenComplete(BiConsumer).- Parameters:
delegate- The handler to delegate to- Returns:
- The new, delegating handler.
-
copyHandler
Creates a future handler that will copy the state of the handled future to the given future.This method is meant to be used in conjunction with
CompletableFuture.whenComplete(BiConsumer).- Parameters:
copyFuture- The future to copy to- Returns:
- the copy handler
-
safeComposer
public static <T,R> Function<T,CompletionStage<R>> safeComposer(Function<? super T, ? extends CompletionStage<R>> delegate) Creates a composition function that will delegate to the givenFunctionbut will catch any exception during composition to return a future completed exceptionally.This method is meant to be used in conjunction with
CompletableFuture.thenCompose(Function). It is useful in particular when you want to apply the same error handling to the composition function and to the resulting future.- Parameters:
delegate- The composition function to delegate to.- Returns:
- The new, delegating composition function .
-
whenCompleteExecute
public static <T> CompletableFuture<T> whenCompleteExecute(CompletableFuture<?> self, Supplier<? extends CompletionStage<T>> action) Compose the given future with another as soon as it's complete, regardless of errors, and return a completable future that will take errors of both parent futures into account (usingThrowable.addSuppressed(Throwable)if need be).- Parameters:
self- The future to wait for before launching the next oneaction- the composition consumer- Returns:
- A completable future that will be complete once
selffinished executing andactionand its resulting future finished executing.
-
unwrappedExceptionJoin
@SuppressForbiddenApis(reason="Safer wrapper") public static <T> T unwrappedExceptionJoin(CompletableFuture<T> future) CallCompletableFuture.join()and unwrap anyCompletionException, expecting the exception to be aRuntimeException.- Type Parameters:
T- The type of result the future will return.- Parameters:
future- The future to join on.- Returns:
- The result returned by the future.
- Throws:
RuntimeException- If the future fails.
-
unwrappedExceptionGet
CallCompletableFuture.get()and unwrap anyExecutionException, expecting the exception to be aRuntimeException.- Type Parameters:
T- The type of result the future will return.- Parameters:
future- The future to join on.- Returns:
- The result returned by the future.
- Throws:
RuntimeException- If the future fails.InterruptedException- If the thread is interrupted.
-
runAsync
Similar toCompletableFuture.runAsync(Runnable, Executor), but callingCompletableFuture.cancel(boolean)on the returned future actually has an effect and may interrupt the thread.This is mainly useful when the task to execute includes blocking calls, which is usually not the case when dealing with
CompletableFuture.- Parameters:
runnable- the task to submitexecutor- an executor to submit the task to- Returns:
- a
CompletableFuturethat will complete once the given runnable has finished executing, potentially with an exception. - Throws:
RejectedExecutionException- if the task cannot be scheduled for execution.NullPointerException- if the task is null.
-
getThrowableNow
-
firstFailureOrAllOf
- Returns:
- a
CompletableFuturethat will complete either when one of the future completes exceptionally, or when all futures complete successfully, whichever happens first.
-