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.
|
static <T> Future<T> |
apply(ThrowableFunction0<T> function,
Executor executor)
Allows for easy creation of asynchronous computations that will be executed in the future.
|
static <T> Future<T> |
failed(Throwable throwable)
Creates a failed Future with the provided Throwable.
|
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(ThrowableFunction1<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.
|
static <T> Future<T> |
fromTry(Try<T> result)
Creates a completed Future with the provided Try.
|
boolean |
isCompleted()
Check if this Future is completed, with a value or an exception.
|
<R> Future<R> |
map(ThrowableFunction1<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.
|
default Future<T> |
ready(java.time.Duration duration)
Blocks and waits for this Future to complete.
|
Future<T> |
ready(long duration,
TimeUnit timeUnit)
Blocks and waits for this Future to complete.
|
Future<T> |
recover(ThrowableFunction1<Throwable,T> recoverFunction)
|
default T |
result(java.time.Duration duration)
Blocks and waits for this Future to complete.
|
T |
result(long duration,
TimeUnit timeUnit)
Blocks and waits for this Future to complete.
|
static <T> Future<java.util.stream.Stream<T>> |
sequence(java.util.stream.Stream<Future<T>> stream)
Turns a Stream of Futures into a single Future containing a Stream with all the results from the Futures.
|
static <T> Future<T> |
successful(T value)
Creates a successful Future with the provided value.
|
<R> Future<R> |
transform(ThrowableFunction1<T,R> onSuccess,
ThrowableFunction1<Throwable,Throwable> onFailure)
Creates a new
Future that will hold the transformed successful value of this instance once it is completed. |
static <T,R> Future<java.util.stream.Stream<R>> |
traverse(java.util.stream.Stream<T> stream,
java.util.function.Function<T,Future<R>> function)
Takes a Stream of values and applies the provided function to them in parallel resulting in a Future containing a Stream with the mapped values.
|
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.static <T> Future<T> apply(ThrowableFunction0<T> function, Executor executor)
Executor for executing the provided job. Future<Integer> resultSuccess = Future.apply(() -> 9 / 3, someExecutor); // The Future will at some point contain: Success(3) Future<Integer> resultFailure = Future.apply(() -> 9 / 0, someExecutor); // 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.executor - The executor to use to compute/execute the Future holding the provided functionstatic <T> Future<T> failed(Throwable throwable)
T - The type for the Futurethrowable - The throwable to complete the Future with.static <T> Future<T> successful(T value)
T - The type for the Futurevalue - The value to complete the Future with.static <T> Future<java.util.stream.Stream<T>> sequence(java.util.stream.Stream<Future<T>> stream)
T - The type for the Stream in the resulting Futurestream - The Stream with Futuresstatic <T,R> Future<java.util.stream.Stream<R>> traverse(java.util.stream.Stream<T> stream, java.util.function.Function<T,Future<R>> function)
import static javascalautils.FutureCompanion.Future; Stream<String> stream = ...; // Stream with strings Future<Stream<Integer>> future = Future.traverse(stream, v -> Future(() -> v.length()));
T - The type for the input StreamR - The type for the Stream in the resulting Futurestream - The Stream with valuesfunction - The function to be applied to all values of the Streamboolean 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(ThrowableFunction1<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(ThrowableFunction1<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(ThrowableFunction1<T,R> onSuccess, ThrowableFunction1<Throwable,Throwable> onFailure)
Future that will hold the transformed 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(ThrowableFunction1<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 passeddefault T result(java.time.Duration duration) throws Throwable, TimeoutException
duration - The duration to blockThrowable - The error reported in case of a failureTimeoutException - In case the waiting time is passedFuture<T> ready(long duration, TimeUnit timeUnit) throws TimeoutException, InterruptedException
result this method will not return the value or throw the exception of the Future. duration - The duration to blocktimeUnit - The unit for the durationTimeoutException - In case the waiting time is passedInterruptedException - In case the thread gets interrupted during the waitdefault Future<T> ready(java.time.Duration duration) throws TimeoutException, InterruptedException
result this method will not return the value or throw the exception of the Future. duration - The duration to blockTimeoutException - In case the waiting time is passedInterruptedException - In case the thread gets interrupted during the waitCopyright © 2016, Peter Nerg Apache License v2.0