T - The type this Future will hold as resultpublic interface Future<T>
Promise which is also the way to get hold of a Future. result(long, TimeUnit) is provided. apply(ThrowableFunction0) method to provide a function that will be executed in the
future.Future<Integer> resultSuccess = Future.apply(() -> 9 / 3); // The Future will at some point contain: Success(3) Future<Integer> resultFailure = Future.apply(() -> 9 / 0); // The Future will at some point contain: Failure(ArithmeticException)
| Modifier and Type | Method and Description |
|---|---|
static <T> Future<T> |
apply(ThrowableFunction0<T> function)
Allows for easy creation of asynchronous computations that will be executed in the future.
|
Future<T> |
filter(java.util.function.Predicate<T> predicate)
Creates a new
Future that will filter the successful value of this instance once it is completed. |
<R> Future<R> |
flatMap(java.util.function.Function<T,Future<R>> function)
Creates a new
Future that will hold the mapped successful value of this instance once it is completed. |
void |
forEach(java.util.function.Consumer<T> consumer)
Asynchronously processes the value in the Future once it is available.
|
boolean |
isCompleted()
Check if this Future is completed, with a value or an exception.
|
<R> Future<R> |
map(java.util.function.Function<T,R> function)
Creates a new
Future that will hold the mapped successful value of this instance once it is completed. |
void |
onComplete(java.util.function.Consumer<Try<T>> completeHandler)
Register a handler to be invoked if the Future gets completed with a value or a failure.
|
void |
onFailure(java.util.function.Consumer<Throwable> failureHandler)
Register a handler to be invoked if the Future gets completed with an exception.
|
void |
onSuccess(java.util.function.Consumer<T> successHandler)
Register a handler to be invoked if the Future gets completed with a value.
|
Future<T> |
recover(java.util.function.Function<Throwable,T> recoverFunction)
|
T |
result(long duration,
TimeUnit timeUnit)
Blocks and waits for this Future to complete.
|
<R> Future<R> |
transform(java.util.function.Function<T,R> onSuccess,
java.util.function.Function<Throwable,Throwable> onFailure)
Creates a new
Future that will hold the mapped successful value of this instance once it is completed. |
Option<Try<T>> |
value()
The current (completed or not) value of the future.
|
static <T> Future<T> apply(ThrowableFunction0<T> function)
Executors.getDefault() method to get hold of the default Executor to use for executing the provided job. Future<Integer> resultSuccess = Future.apply(() -> 9 / 3); // The Future will at some point contain: Success(3) Future<Integer> resultFailure = Future.apply(() -> 9 / 0); // The Future will at some point contain: Failure(ArithmeticException)
T - The type for the Futurefunction - The function to render either the value T or raise an exception.boolean isCompleted()
true if completed, false otherwise.Option<Try<T>> value()
Option with the result.void onFailure(java.util.function.Consumer<Throwable> failureHandler)
failureHandler - Consumer to invoke.void onSuccess(java.util.function.Consumer<T> successHandler)
successHandler - Consumer to invoke.void onComplete(java.util.function.Consumer<Try<T>> completeHandler)
completeHandler - Consumer to invoke.void forEach(java.util.function.Consumer<T> consumer)
onSuccess(Consumer) but is here for completion keeping a consistent look and feel.consumer - The consumer to digest the result<R> Future<R> map(java.util.function.Function<T,R> function)
Future that will hold the mapped successful value of this instance once it is completed. R - The type for the value held by the mapped futurefunction - The function to apply<R> Future<R> flatMap(java.util.function.Function<T,Future<R>> function)
Future that will hold the mapped successful value of this instance once it is completed. R - The type for the value held by the mapped futurefunction - The function to applyFuture<T> filter(java.util.function.Predicate<T> predicate)
Future that will filter the successful value of this instance once it is completed. predicate - The predicate to apply<R> Future<R> transform(java.util.function.Function<T,R> onSuccess, java.util.function.Function<Throwable,Throwable> onFailure)
Future that will hold the mapped successful value of this instance once it is completed. R - The type for the value held by the mapped futureonSuccess - The function to apply on a 'successful' resultonFailure - The function to apply on a 'failure' resultFuture<T> recover(java.util.function.Function<Throwable,T> recoverFunction)
Future that in case this Future is a 'failure' will apply the function to recover the 'failure' to a 'success'. Future be a 'success' the value is propagated as-is. In case of 'future' being successful then that value is passed on to 'recovered', in case of failure then the recover function kicks in and returns the message from the throwable.Future<String> future = ... Future<String> recovered = future.recover(t -> t.getMessage());
recoverFunction - The function to apply in case of a 'failure'T result(long duration, TimeUnit timeUnit) throws Throwable, TimeoutException
duration - The duration to blocktimeUnit - The unit for the durationThrowable - The error reported in case of a failureTimeoutException - In case the waiting time is passedCopyright © 2015, Peter Nerg Apache License v2.0