public final class Futures
extends java.lang.Object
| Constructor and Description |
|---|
Futures() |
| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.concurrent.CompletableFuture<java.lang.Void> |
allOf(java.util.Collection<java.util.concurrent.CompletableFuture<T>> futures)
Similar implementation to CompletableFuture.allOf(vararg) but that works on a Collection.
|
static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> |
allOfWithResults(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)
Similar implementation to CompletableFuture.allOf(vararg) but that works on a Collection and that returns another
Collection which has the results of the given CompletableFutures.
|
static <K,V> java.util.concurrent.CompletableFuture<java.util.Map<K,V>> |
allOfWithResults(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futureMap)
Similar to CompletableFuture.allOf(varargs), but that works on a Map and that returns another Map which has the
results of the given CompletableFutures, with the same input keys.
|
static <T> boolean |
await(java.util.concurrent.CompletableFuture<T> f)
Waits for the provided future to be complete, and returns true if it was successful, false otherwise.
|
static <T> boolean |
await(java.util.concurrent.CompletableFuture<T> f,
long timeout)
Waits for the provided future to be complete, and returns true if it was successful, false if it failed
or did not complete.
|
static <T> void |
completeAfter(java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends T>> futureSupplier,
java.util.concurrent.CompletableFuture<T> toComplete)
Given a Supplier returning a Future, completes another future either with the result of the first future, in case
of normal completion, or exceptionally with the exception of the first future.
|
static java.util.concurrent.CompletableFuture<java.lang.Void> |
delayedFuture(java.time.Duration delay,
java.util.concurrent.ScheduledExecutorService executorService)
Creates a CompletableFuture that will do nothing and complete after a specified delay, without using a thread during
the delay.
|
static <T> java.util.concurrent.CompletableFuture<T> |
delayedFuture(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> task,
long delay,
java.util.concurrent.ScheduledExecutorService executorService)
Executes the asynchronous task after the specified delay.
|
static <T> java.util.concurrent.CompletableFuture<T> |
delayedTask(java.util.function.Supplier<T> task,
java.time.Duration delay,
java.util.concurrent.ScheduledExecutorService executorService)
Executes the given task after the specified delay.
|
static <T> java.util.concurrent.CompletableFuture<java.lang.Void> |
doWhileLoop(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody,
java.util.function.Predicate<T> condition,
java.util.concurrent.Executor executor)
Executes a code fragment returning a CompletableFutures while a condition on the returned value is satisfied.
|
static <T> java.util.concurrent.CompletableFuture<T> |
exceptionallyCompose(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Function<java.lang.Throwable,java.util.concurrent.CompletableFuture<T>> handler)
Same as CompletableFuture.exceptionally(), except that it allows returning a CompletableFuture instead of a single value.
|
static <T> java.util.concurrent.CompletableFuture<T> |
exceptionallyComposeExpecting(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Predicate<java.lang.Throwable> isExpected,
java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> exceptionFutureSupplier)
Same as exceptionallyExpecting(), except that it allows executing/returning a Future as a result in case of an
expected exception.
|
static <T> java.util.concurrent.CompletableFuture<T> |
exceptionallyExpecting(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Predicate<java.lang.Throwable> isExpected,
T exceptionValue)
Same as CompletableFuture.exceptionally(), except that it allows certain exceptions, as defined by the isExpected parameter.
|
static <T,E extends java.lang.Throwable> |
exceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture,
java.lang.Class<E> exceptionClass,
java.util.function.Consumer<E> exceptionListener)
Registers an exception listener to the given CompletableFuture for a particular type of exception.
|
static <T> void |
exceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture,
java.util.function.Consumer<java.lang.Throwable> exceptionListener)
Registers an exception listener to the given CompletableFuture.
|
static <T> java.util.concurrent.CompletableFuture<T> |
failedFuture(java.lang.Throwable exception)
Creates a new CompletableFuture that is failed with the given exception.
|
static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> |
filter(java.util.List<T> input,
java.util.function.Function<T,java.util.concurrent.CompletableFuture<java.lang.Boolean>> predicate)
Filter that takes a predicate that evaluates in future and returns the filtered list that evaluates in future.
|
static <T> java.util.concurrent.CompletableFuture<T> |
futureWithTimeout(java.time.Duration timeout,
java.util.concurrent.ScheduledExecutorService executorService)
Creates a new CompletableFuture that will timeout after the given amount of time.
|
static <T> java.util.concurrent.CompletableFuture<T> |
futureWithTimeout(java.time.Duration timeout,
java.lang.String tag,
java.util.concurrent.ScheduledExecutorService executorService)
Creates a new CompletableFuture that will timeout after the given amount of time.
|
static <ResultT,ExceptionT extends java.lang.Exception> |
getAndHandleExceptions(java.util.concurrent.Future<ResultT> future,
java.util.function.Consumer<java.lang.Throwable> handler,
long timeoutMillis)
Similar to
getAndHandleExceptions(Future, Function) but with an exception handler rather than a transforming function
and a timeout on get(). |
static <ResultT,ExceptionT extends java.lang.Exception> |
getAndHandleExceptions(java.util.concurrent.Future<ResultT> future,
java.util.function.Function<java.lang.Throwable,ExceptionT> exceptionConstructor)
Calls get on the provided future, handling interrupted, and transforming the executionException into an exception
of the type whose constructor is provided.
|
static <T> java.lang.Throwable |
getException(java.util.concurrent.CompletableFuture<T> future)
If the future has failed returns the exception that caused it.
|
static <ResultT,E1 extends java.lang.Exception,E2 extends java.lang.Exception,E3 extends java.lang.Exception> |
getThrowingException(java.util.concurrent.Future<ResultT> future)
Gets a future returning its result, or the exception that caused it to fail.
|
static <T> boolean |
isSuccessful(java.util.concurrent.CompletableFuture<T> f)
Returns true if the future is done and successful.
|
static <K,V> java.util.concurrent.CompletableFuture<java.util.Map<K,V>> |
keysAllOfWithResults(java.util.Map<java.util.concurrent.CompletableFuture<K>,V> futureMap)
Similar to CompletableFuture.allOf(varargs), but that works on a Map and that returns another Map which has the
results of the given CompletableFutures, with the same input keys.
|
static <T> java.util.concurrent.CompletableFuture<java.lang.Void> |
loop(java.util.function.Supplier<java.lang.Boolean> condition,
java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody,
java.util.function.Consumer<T> resultConsumer,
java.util.concurrent.Executor executor)
Executes a loop using CompletableFutures, without invoking join()/get() on any of them or exclusively hogging a thread.
|
static java.util.concurrent.CompletableFuture<java.lang.Void> |
loop(java.util.function.Supplier<java.lang.Boolean> condition,
java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Void>> loopBody,
java.util.concurrent.Executor executor)
Executes a loop using CompletableFutures, without invoking join()/get() on any of them or exclusively hogging a thread.
|
static <T> void |
onTimeout(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Consumer<java.util.concurrent.TimeoutException> callback)
Attaches the given callback as an exception listener to the given CompletableFuture, which will be invoked when
the future times out (fails with a TimeoutException).
|
static <T,R> R |
runOrFail(java.util.concurrent.Callable<R> callable,
java.util.concurrent.CompletableFuture<T> future)
Runs the provided Callable in the current thread (synchronously) if it throws any Exception or
Throwable the exception is propagated, but the supplied future is also failed.
|
static <T> java.util.concurrent.CompletableFuture<java.lang.Void> |
toVoid(java.util.concurrent.CompletableFuture<T> future)
Returns a CompletableFuture that will end when the given future ends, but discards its result.
|
static <T,E extends java.lang.Exception> |
toVoidExpecting(java.util.concurrent.CompletableFuture<T> future,
T expectedValue,
java.util.function.Supplier<E> exceptionConstructor)
Returns a CompletableFuture that will end when the given future ends, expecting a certain
result.
|
public static <T> boolean await(java.util.concurrent.CompletableFuture<T> f)
T - The Type of the future's result.f - The future to wait for.public static <T> boolean await(java.util.concurrent.CompletableFuture<T> f,
long timeout)
T - The Type of the future's result.timeout - The maximum number of milliseconds to blockf - The future to wait for.public static <T> void completeAfter(java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends T>> futureSupplier,
java.util.concurrent.CompletableFuture<T> toComplete)
T - Return type of Future.futureSupplier - A Supplier returning a Future to listen to.toComplete - A CompletableFuture that has not yet been completed, which will be completed with the result
of the Future from futureSupplier.public static <T> boolean isSuccessful(java.util.concurrent.CompletableFuture<T> f)
T - The Type of the future's result.f - The future to inspect.public static <T> java.lang.Throwable getException(java.util.concurrent.CompletableFuture<T> future)
T - The Type of the future's result.future - The future to inspect.public static <ResultT,E1 extends java.lang.Exception,E2 extends java.lang.Exception,E3 extends java.lang.Exception> ResultT getThrowingException(java.util.concurrent.Future<ResultT> future)
throws E1 extends java.lang.Exception,
E2 extends java.lang.Exception,
E3 extends java.lang.Exception
ResultT - The result type of the provided futureE1 - A type of exception that may cause the future to fail.E2 - A type of exception that may cause the future to fail.E3 - A type of exception that may cause the future to fail.future - The future to call get() on.E1 - If exception E1 occurs.E2 - If exception E2 occurs.E3 - If exception E3 occurs.E1 extends java.lang.Exceptionpublic static <ResultT,ExceptionT extends java.lang.Exception> ResultT getAndHandleExceptions(java.util.concurrent.Future<ResultT> future,
java.util.function.Function<java.lang.Throwable,ExceptionT> exceptionConstructor)
throws ExceptionT extends java.lang.Exception
ResultT - Type of the result.ExceptionT - Type of the Exception.future - The future whose result is wantedexceptionConstructor - This can be any function that either transforms an exception
i.e. Passing RuntimeException::new will wrap the exception in a new RuntimeException.
If null is returned from the function no exception will be thrown.ExceptionT - If thrown by the future.ExceptionT extends java.lang.Exceptionpublic static <ResultT,ExceptionT extends java.lang.Exception> ResultT getAndHandleExceptions(java.util.concurrent.Future<ResultT> future,
java.util.function.Consumer<java.lang.Throwable> handler,
long timeoutMillis)
throws ExceptionT extends java.lang.Exception
getAndHandleExceptions(Future, Function) but with an exception handler rather than a transforming function
and a timeout on get().ResultT - Type of the result.ExceptionT - Type of the Exception.future - The future whose result is wantedhandler - An exception handlertimeoutMillis - the timeout expressed in milliseconds before throwing TimeoutExceptionExceptionT - If thrown by the future.ExceptionT extends java.lang.Exceptionpublic static <T> java.util.concurrent.CompletableFuture<T> failedFuture(java.lang.Throwable exception)
T - The Type of the future's result.exception - The exception to fail the CompletableFuture.public static <T> void exceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture,
java.util.function.Consumer<java.lang.Throwable> exceptionListener)
T - The Type of the future's result.completableFuture - The Future to register to.exceptionListener - The Listener to register.public static <T,E extends java.lang.Throwable> void exceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture,
java.lang.Class<E> exceptionClass,
java.util.function.Consumer<E> exceptionListener)
T - The Type of the future's result.E - The Type of the exception.completableFuture - The Future to register to.exceptionClass - The type of exception to listen to.exceptionListener - The Listener to register.public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyCompose(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Function<java.lang.Throwable,java.util.concurrent.CompletableFuture<T>> handler)
T - Type of the value of the original Future.future - The original CompletableFuture to attach an Exception Listener.handler - A Function that consumes a Throwable and returns a CompletableFuture of the same type as the original one.
This Function will be invoked if the original Future completed exceptionally.public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyExpecting(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Predicate<java.lang.Throwable> isExpected,
T exceptionValue)
T - The Type of the Future's result.future - The original CompletableFuture to attach to.isExpected - A Predicate that can check whether an Exception is expected or not.exceptionValue - The value to return in case the thrown Exception if of type exceptionClass.public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyComposeExpecting(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Predicate<java.lang.Throwable> isExpected,
java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> exceptionFutureSupplier)
T - The Type of the Future's result.future - The original CompletableFuture to attach to.isExpected - A Predicate that can check whether an Exception is expected or not.exceptionFutureSupplier - A Supplier that returns a CompletableFuture which will be invoked in case the thrown
Exception if of type exceptionClass.public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> toVoid(java.util.concurrent.CompletableFuture<T> future)
T - The type of the input's future result.future - The CompletableFuture to attach to.public static <T,E extends java.lang.Exception> java.util.concurrent.CompletableFuture<java.lang.Void> toVoidExpecting(java.util.concurrent.CompletableFuture<T> future,
T expectedValue,
java.util.function.Supplier<E> exceptionConstructor)
T - The type of the value expectedE - The type of the exception to be throw if the value is not found.future - the CompletableFuture to attach to.expectedValue - The value expectedexceptionConstructor - Constructor for an exception in the event there is not a match.public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> allOfWithResults(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)
T - The type of the results items.futures - A List of CompletableFutures to wait on.public static <K,V> java.util.concurrent.CompletableFuture<java.util.Map<K,V>> allOfWithResults(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futureMap)
K - The type of the Keys.V - The Type of the Values.futureMap - A Map of Keys to CompletableFutures to wait on.public static <K,V> java.util.concurrent.CompletableFuture<java.util.Map<K,V>> keysAllOfWithResults(java.util.Map<java.util.concurrent.CompletableFuture<K>,V> futureMap)
K - The type of the Keys.V - The Type of the Values.futureMap - A Map of Keys to CompletableFutures to wait on.public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> allOf(java.util.Collection<java.util.concurrent.CompletableFuture<T>> futures)
T - The type of the results items.futures - A Collection of CompletableFutures to wait on.public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> filter(java.util.List<T> input,
java.util.function.Function<T,java.util.concurrent.CompletableFuture<java.lang.Boolean>> predicate)
T - Type parameter.input - Input list.predicate - Predicate that evaluates in the future.public static <T> java.util.concurrent.CompletableFuture<T> futureWithTimeout(java.time.Duration timeout,
java.util.concurrent.ScheduledExecutorService executorService)
T - The Type argument for the CompletableFuture to create.timeout - The timeout for the future.executorService - An ExecutorService that will be used to invoke the timeout on.public static <T> java.util.concurrent.CompletableFuture<T> futureWithTimeout(java.time.Duration timeout,
java.lang.String tag,
java.util.concurrent.ScheduledExecutorService executorService)
T - The Type argument for the CompletableFuture to create.timeout - The timeout for the future.tag - A tag (identifier) to be used as a parameter to the TimeoutException.executorService - An ExecutorService that will be used to invoke the timeout on.public static <T> void onTimeout(java.util.concurrent.CompletableFuture<T> future,
java.util.function.Consumer<java.util.concurrent.TimeoutException> callback)
T - The Type of the future's result.future - The future to attach to.callback - The callback to invoke.public static java.util.concurrent.CompletableFuture<java.lang.Void> delayedFuture(java.time.Duration delay,
java.util.concurrent.ScheduledExecutorService executorService)
delay - The duration of the delay (how much to wait until completing the Future).executorService - An ExecutorService that will be used to complete the Future on.public static <T> java.util.concurrent.CompletableFuture<T> delayedFuture(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> task,
long delay,
java.util.concurrent.ScheduledExecutorService executorService)
T - Type parameter.task - Asynchronous task.delay - Delay in milliseconds.executorService - Executor on which to execute the task.public static <T> java.util.concurrent.CompletableFuture<T> delayedTask(java.util.function.Supplier<T> task,
java.time.Duration delay,
java.util.concurrent.ScheduledExecutorService executorService)
T - Type parameter.task - Asynchronous task.delay - Delay.executorService - Executor on which to execute the task.public static <T,R> R runOrFail(java.util.concurrent.Callable<R> callable,
java.util.concurrent.CompletableFuture<T> future)
T - The type of the future.R - The return type of the callable.callable - The function to invoke.future - The future to fail if the function fails.public static java.util.concurrent.CompletableFuture<java.lang.Void> loop(java.util.function.Supplier<java.lang.Boolean> condition,
java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Void>> loopBody,
java.util.concurrent.Executor executor)
condition - A Supplier that indicates whether to proceed with the loop or not.loopBody - A Supplier that returns a CompletableFuture which represents the body of the loop. This
supplier is invoked every time the loopBody needs to execute.executor - An Executor that is used to execute the condition and the loop support code.public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> loop(java.util.function.Supplier<java.lang.Boolean> condition,
java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody,
java.util.function.Consumer<T> resultConsumer,
java.util.concurrent.Executor executor)
T - The Type of the future's result.condition - A Supplier that indicates whether to proceed with the loop or not.loopBody - A Supplier that returns a CompletableFuture which represents the body of the loop. This
supplier is invoked every time the loopBody needs to execute.resultConsumer - A Consumer that will be invoked with the result of every call to loopBody.executor - An Executor that is used to execute the condition and the loop support code.public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> doWhileLoop(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody,
java.util.function.Predicate<T> condition,
java.util.concurrent.Executor executor)
T - Return type of the executor.condition - Predicate that indicates whether to proceed with the loop or not.loopBody - A Supplier that returns a CompletableFuture which represents the body of the loop. This
supplier is invoked every time the loopBody needs to execute.executor - An Executor that is used to execute the condition and the loop support code.