T
- Type of the computation result.public interface Future<T> extends Value<T>
The underlying Executor
is used to execute asynchronous handlers, e.g. via
onComplete(...)
.
A Future has two states: pending and completed.
Modifier and Type | Field and Description |
---|---|
static Executor |
DEFAULT_EXECUTOR
The default executor is
ForkJoinPool.commonPool() . |
static ExecutorService |
DEFAULT_EXECUTOR_SERVICE
Deprecated.
Will be removed in Vavr 1.0. Use
instead . |
Modifier and Type | Method and Description |
---|---|
default Future<T> |
andThen(Consumer<? super Try<T>> action)
Support for chaining of callbacks that are guaranteed to be executed in a specific order.
|
Future<T> |
await()
Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.
|
Future<T> |
await(long timeout,
TimeUnit unit)
Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.
|
default boolean |
cancel()
Cancels the Future.
|
boolean |
cancel(boolean mayInterruptIfRunning)
Cancels the Future.
|
default <R> Future<R> |
collect(PartialFunction<? super T,? extends R> partialFunction)
Collects value that is in the domain of the given
partialFunction by mapping the value to type R . |
default Executor |
executor()
Returns the
Executor used by this Future . |
ExecutorService |
executorService()
Deprecated.
Removed starting with Vavr 0.10.0, use
executor() instead. |
default Future<Throwable> |
failed()
A projection that inverses the result of this Future.
|
static <T> Future<T> |
failed(Executor executor,
Throwable exception)
|
static <T> Future<T> |
failed(Throwable exception)
|
default Future<T> |
fallbackTo(Future<? extends T> that)
Returns a Future that returns the result of this Future, if it is a success.
|
default Future<T> |
filter(Predicate<? super T> predicate)
Shortcut for
filterTry(predicate::test . |
default Future<T> |
filterTry(CheckedPredicate<? super T> predicate)
Filters the result of this
Future by calling Try.filterTry(CheckedPredicate) . |
static <T> Future<Option<T>> |
find(Executor executor,
Iterable<? extends Future<? extends T>> futures,
Predicate<? super T> predicate)
Returns a
Future that eventually succeeds with the first result of the given Future s which
matches the given predicate . |
static <T> Future<Option<T>> |
find(Iterable<? extends Future<? extends T>> futures,
Predicate<? super T> predicate)
Returns a
Future that eventually succeeds with the first result of the given Future s which
matches the given predicate . |
static <T> Future<T> |
firstCompletedOf(Executor executor,
Iterable<? extends Future<? extends T>> futures)
Returns a new
Future that will contain the result of the first of the given futures that is completed,
backed by the given Executor . |
static <T> Future<T> |
firstCompletedOf(Iterable<? extends Future<? extends T>> futures)
Returns a new
Future that will contain the result of the first of the given futures that is completed,
backed by the DEFAULT_EXECUTOR . |
default <U> Future<U> |
flatMap(Function<? super T,? extends Future<? extends U>> mapper) |
default <U> Future<U> |
flatMapTry(CheckedFunction1<? super T,? extends Future<? extends U>> mapper) |
static <T,U> Future<U> |
fold(Executor executor,
Iterable<? extends Future<? extends T>> futures,
U zero,
BiFunction<? super U,? super T,? extends U> f)
Returns a Future which contains the result of the fold of the given future values.
|
static <T,U> Future<U> |
fold(Iterable<? extends Future<? extends T>> futures,
U zero,
BiFunction<? super U,? super T,? extends U> f)
Returns a Future which contains the result of the fold of the given future values.
|
default void |
forEach(Consumer<? super T> action)
Performs the given
action asynchronously hence this Future result becomes available. |
static <T> Future<T> |
fromCompletableFuture(CompletableFuture<T> future)
|
static <T> Future<T> |
fromCompletableFuture(Executor executor,
CompletableFuture<T> future)
|
static <T> Future<T> |
fromJavaFuture(Executor executor,
Future<T> future)
Creates a
Future with the given java.util.concurrent.Future, backed by given Executor |
static <T> Future<T> |
fromJavaFuture(Future<T> future)
Creates a
Future with the given java.util.concurrent.Future, backed by the DEFAULT_EXECUTOR |
static <T> Future<T> |
fromTry(Executor executor,
Try<? extends T> result)
|
static <T> Future<T> |
fromTry(Try<? extends T> result)
|
default T |
get()
Gets the value if the computation result is a
Success or throws if it was a Failure . |
default Option<Throwable> |
getCause()
Returns the underlying exception of this Future, syntactic sugar for
future.getValue().map(Try::getCause) . |
Option<Try<T>> |
getValue()
Returns the value of the Future.
|
default boolean |
isAsync()
A
Futures 's value is computed asynchronously. |
boolean |
isCancelled()
Checks if this Future is cancelled, i.e.
|
boolean |
isCompleted()
Checks if this Future is completed, i.e.
|
default boolean |
isEmpty()
Checks, if this future has a value.
|
default boolean |
isFailure()
Checks if this Future completed with a failure.
|
default boolean |
isLazy()
A
Future 's value is computed eagerly. |
default boolean |
isSingleValued()
A
Future is single-valued. |
default boolean |
isSuccess()
Checks if this Future completed with a success.
|
default Iterator<T> |
iterator()
Returns a rich
io.vavr.collection.Iterator . |
default <U> Future<U> |
map(Function<? super T,? extends U> mapper)
Maps the underlying value to a different component type.
|
default <U> Future<U> |
mapTry(CheckedFunction1<? super T,? extends U> mapper) |
static <T> Future<T> |
narrow(Future<? extends T> future)
Narrows a widened
Future<? extends T> to Future<T>
by performing a type-safe cast. |
static <T> Future<T> |
of(CheckedFunction0<? extends T> computation)
Starts an asynchronous computation, backed by the
DEFAULT_EXECUTOR . |
static <T> Future<T> |
of(Executor executor,
CheckedFunction0<? extends T> computation)
Starts an asynchronous computation, backed by the given
Executor . |
static <T> Future<T> |
ofCallable(Callable<? extends T> computation)
Deprecated.
Will be removed. Use
Future.of(callable::call) instead of Future.ofCallable(callable) . |
static <T> Future<T> |
ofCallable(Executor executor,
Callable<? extends T> computation)
Deprecated.
Will be removed. Use
Future.of(executor, callable::call) instead of Future.ofCallable(executor, callable) . |
static <T> Future<T> |
ofSupplier(Executor executor,
Supplier<? extends T> computation)
Deprecated.
Will be removed. Use
Future.of(executor, supplier::get) instead of Future.ofSupplier(executor, supplier) . |
static <T> Future<T> |
ofSupplier(Supplier<? extends T> computation)
Deprecated.
Will be removed. Use
Future.of(supplier::get) instead of Future.ofSupplier(supplier) . |
Future<T> |
onComplete(Consumer<? super Try<T>> action)
Performs the action once the Future is complete.
|
default Future<T> |
onFailure(Consumer<? super Throwable> action)
Performs the action once the Future is complete and the result is a
Try.Failure . |
default Future<T> |
onSuccess(Consumer<? super T> action)
Performs the action once the Future is complete and the result is a
Try.Success . |
default Future<T> |
orElse(Future<? extends T> other) |
default Future<T> |
orElse(Supplier<? extends Future<? extends T>> supplier) |
default Future<T> |
peek(Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
default Future<T> |
recover(Function<? super Throwable,? extends T> f)
Handles a failure of this Future by returning another result.
|
default Future<T> |
recoverWith(Function<? super Throwable,? extends Future<? extends T>> f)
Handles a failure of this Future by returning the result of another Future.
|
static <T> Future<T> |
reduce(Executor executor,
Iterable<? extends Future<? extends T>> futures,
BiFunction<? super T,? super T,? extends T> f)
Returns a Future which contains the reduce result of the given future values.
|
static <T> Future<T> |
reduce(Iterable<? extends Future<? extends T>> futures,
BiFunction<? super T,? super T,? extends T> f)
Returns a Future which contains the reduce result of the given future values.
|
static Future<Void> |
run(CheckedRunnable unit)
Runs an asynchronous computation, backed by the
DEFAULT_EXECUTOR . |
static Future<Void> |
run(Executor executor,
CheckedRunnable unit)
Starts an asynchronous computation, backed by the given
Executor . |
static <T> Future<T> |
run(Executor executor,
Task<? extends T> task)
Deprecated.
Experimental API
|
static <T> Future<T> |
run(Task<? extends T> task)
Deprecated.
Experimental API
|
static Future<Void> |
runRunnable(Executor executor,
Runnable computation)
Deprecated.
Will be removed. Use
Future.of(executor, runnable::run) instead of Future.runRunnable(executor, runnable) . |
static Future<Void> |
runRunnable(Runnable computation)
Deprecated.
Will be removed. Use
Future.of(runnable::run) instead of Future.runRunnable(runnable) . |
static <T> Future<Seq<T>> |
sequence(Executor executor,
Iterable<? extends Future<? extends T>> futures)
Reduces many
Future s into a single Future by transforming an
Iterable<Future<? extends T>> into a Future<Seq<T>> . |
static <T> Future<Seq<T>> |
sequence(Iterable<? extends Future<? extends T>> futures)
Reduces many
Future s into a single Future by transforming an
Iterable<Future<? extends T>> into a Future<Seq<T>> . |
default String |
stringPrefix()
Returns the name of this Value type, which is used by toString().
|
static <T> Future<T> |
successful(Executor executor,
T result)
Creates a succeeded
Future , backed by the given Executor . |
static <T> Future<T> |
successful(T result)
Creates a succeeded
Future , backed by the DEFAULT_EXECUTOR . |
default CompletableFuture<T> |
toCompletableFuture()
Converts this to a
CompletableFuture |
default <U> U |
transform(Function<? super Future<T>,? extends U> f)
Transforms this
Future . |
default <U> Future<U> |
transformValue(Function<? super Try<T>,? extends Try<? extends U>> f)
Transforms the value of this
Future , whether it is a success or a failure. |
static <T,U> Future<Seq<U>> |
traverse(Executor executor,
Iterable<? extends T> values,
Function<? super T,? extends Future<? extends U>> mapper)
Maps the values of an iterable in parallel to a sequence of mapped values into a single
Future by
transforming an Iterable<? extends T> into a Future<Seq<U>> . |
static <T,U> Future<Seq<U>> |
traverse(Iterable<? extends T> values,
Function<? super T,? extends Future<? extends U>> mapper)
Maps the values of an iterable in parallel to a sequence of mapped values into a single
Future by
transforming an Iterable<? extends T> into a Future<Seq<U>> . |
default <U> Future<Tuple2<T,U>> |
zip(Future<? extends U> that)
Returns a tuple of this and that Future result.
|
default <U,R> Future<R> |
zipWith(Future<? extends U> that,
BiFunction<? super T,? super U,? extends R> combinator)
Returns a this and that Future result combined using a given combinator function.
|
collect, collect, contains, corresponds, eq, equals, exists, forAll, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, hashCode, narrow, out, out, spliterator, stderr, stdout, toArray, toCharSeq, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
@Deprecated static final ExecutorService DEFAULT_EXECUTOR_SERVICE
instead
.ForkJoinPool.commonPool()
.
Facts about ForkJoinPool:
BaseStream.parallel()
and CompletableFuture
}
IMPORTANT: Invoke ForkJoinPool.commonPool().awaitQuiescence(long, TimeUnit)
before exit in order to
ensure that all running async tasks complete before program termination.
static final Executor DEFAULT_EXECUTOR
ForkJoinPool.commonPool()
.
Facts about ForkJoinPool:
BaseStream.parallel()
and CompletableFuture
}
IMPORTANT: Invoke ForkJoinPool.commonPool().awaitQuiescence(long, TimeUnit)
before exit in order to
ensure that all running async tasks complete before program termination.
static <T> Future<T> failed(Throwable exception)
T
- The value type of a successful result.exception
- The reason why it failed.Future
.NullPointerException
- if exception is nullstatic <T> Future<T> failed(Executor executor, Throwable exception)
T
- The value type of a successful result.executor
- An Executor
.exception
- The reason why it failed.Future
.NullPointerException
- if executor or exception is nullstatic <T> Future<Option<T>> find(Iterable<? extends Future<? extends T>> futures, Predicate<? super T> predicate)
Future
that eventually succeeds with the first result of the given Future
s which
matches the given predicate
. If no result matches, the Future
will contain Option.None
.
The returned Future
is backed by the DEFAULT_EXECUTOR
.
T
- Result type of the futures.futures
- An iterable of futures.predicate
- A predicate that tests successful future results.Option
of the first result of the given futures
that satisfies the given predicate
.NullPointerException
- if one of the arguments is nullstatic <T> Future<Option<T>> find(Executor executor, Iterable<? extends Future<? extends T>> futures, Predicate<? super T> predicate)
Future
that eventually succeeds with the first result of the given Future
s which
matches the given predicate
. If no result matches, the Future
will contain Option.None
.
The returned Future
is backed by the given Executor
.
T
- Result type of the futures.executor
- An Executor
.futures
- An iterable of futures.predicate
- A predicate that tests successful future results.Option
of the first result of the given futures
that satisfies the given predicate
.NullPointerException
- if one of the arguments is nullstatic <T> Future<T> firstCompletedOf(Iterable<? extends Future<? extends T>> futures)
Future
that will contain the result of the first of the given futures that is completed,
backed by the DEFAULT_EXECUTOR
.T
- The result type.futures
- An iterable of futures.Future
.NullPointerException
- if futures is nullstatic <T> Future<T> firstCompletedOf(Executor executor, Iterable<? extends Future<? extends T>> futures)
Future
that will contain the result of the first of the given futures that is completed,
backed by the given Executor
.T
- The result type.executor
- An Executor
.futures
- An iterable of futures.Future
.NullPointerException
- if executor or futures is nullstatic <T,U> Future<U> fold(Iterable<? extends Future<? extends T>> futures, U zero, BiFunction<? super U,? super T,? extends U> f)
The resulting Future
is backed by the DEFAULT_EXECUTOR
.
T
- The result type of the given Futures
.U
- The fold result type.futures
- An iterable of futures.zero
- The zero element of the fold.f
- The fold operation.Future
that will contain the fold result.NullPointerException
- if futures or f is null.static <T,U> Future<U> fold(Executor executor, Iterable<? extends Future<? extends T>> futures, U zero, BiFunction<? super U,? super T,? extends U> f)
The resulting Future
is backed by the given Executor
.
T
- The result type of the given Futures
.U
- The fold result type.executor
- An Executor
.futures
- An iterable of futures.zero
- The zero element of the fold.f
- The fold operation.Future
that will contain the fold result.NullPointerException
- if executor, futures or f is null.static <T> Future<T> fromJavaFuture(Future<T> future)
Future
with the given java.util.concurrent.Future, backed by the DEFAULT_EXECUTOR
T
- Result type of the Futurefuture
- A Future
Future
wrapping the result of the Java futureNullPointerException
- if future is nullstatic <T> Future<T> fromJavaFuture(Executor executor, Future<T> future)
Future
with the given java.util.concurrent.Future, backed by given Executor
T
- Result type of the Futureexecutor
- An Executor
.future
- A Future
.Future
wrapping the result of the Java futureNullPointerException
- if executor or future is null@GwtIncompatible static <T> Future<T> fromCompletableFuture(CompletableFuture<T> future)
T
- Result type of the Futurefuture
- A CompletableFuture
Future
wrapping the result of the CompletableFuture
NullPointerException
- if future is null@GwtIncompatible static <T> Future<T> fromCompletableFuture(Executor executor, CompletableFuture<T> future)
T
- Result type of the Futureexecutor
- An Executor
.future
- A CompletableFuture
.Future
wrapping the result of the CompletableFuture
NullPointerException
- if executor or future is nullstatic <T> Future<T> fromTry(Try<? extends T> result)
T
- The value type of a successful result.result
- The result.Future
which contains either a Success
or a Failure
.NullPointerException
- if result is nullstatic <T> Future<T> fromTry(Executor executor, Try<? extends T> result)
T
- The value type of a successful result.executor
- An Executor
.result
- The result.Future
which contains either a Success
or a Failure
.NullPointerException
- if executor or result is nullstatic <T> Future<T> narrow(Future<? extends T> future)
Future<? extends T>
to Future<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Future
.future
- A Future
.future
instance as narrowed type Future<T>
.@Deprecated static <T> Future<T> ofSupplier(Supplier<? extends T> computation)
Future.of(supplier::get)
instead of Future.ofSupplier(supplier)
.DEFAULT_EXECUTOR
.T
- Type of the computation result.computation
- A computation.NullPointerException
- if computation is null.@Deprecated static <T> Future<T> ofSupplier(Executor executor, Supplier<? extends T> computation)
Future.of(executor, supplier::get)
instead of Future.ofSupplier(executor, supplier)
.Executor
.T
- Type of the computation result.executor
- An executor service.computation
- A computation.NullPointerException
- if one of executor or computation is null.@Deprecated static <T> Future<T> ofCallable(Callable<? extends T> computation)
Future.of(callable::call)
instead of Future.ofCallable(callable)
.DEFAULT_EXECUTOR
.T
- Type of the computation result.computation
- A computationNullPointerException
- if computation is null.@Deprecated static <T> Future<T> ofCallable(Executor executor, Callable<? extends T> computation)
Future.of(executor, callable::call)
instead of Future.ofCallable(executor, callable)
.Executor
.T
- Type of the computation result.executor
- An executor service.computation
- A computation.NullPointerException
- if one of executor or computation is null.@Deprecated static Future<Void> runRunnable(Runnable computation)
Future.of(runnable::run)
instead of Future.runRunnable(runnable)
.DEFAULT_EXECUTOR
.computation
- A computationNullPointerException
- if computation is null.@Deprecated static Future<Void> runRunnable(Executor executor, Runnable computation)
Future.of(executor, runnable::run)
instead of Future.runRunnable(executor, runnable)
.Executor
.executor
- An executor service.computation
- A computation.NullPointerException
- if one of executor or computation is null.static <T> Future<T> of(CheckedFunction0<? extends T> computation)
DEFAULT_EXECUTOR
.T
- Type of the computation result.computation
- A computation.NullPointerException
- if computation is null.static <T> Future<T> of(Executor executor, CheckedFunction0<? extends T> computation)
Executor
.T
- Type of the computation result.executor
- An Executor
.computation
- A computation.NullPointerException
- if one of executor or computation is null.@Deprecated static <T> Future<T> run(Task<? extends T> task)
computation
using a completion handler:
CheckedConsumer<Predicate<Try<T>>> computation = complete -> {
// computation
};
The computation
is executed synchronously. It requires to complete the returned Future.
A common use-case is to hand over the complete
predicate to another Future
in order to prevent blocking:
Future<String> greeting(Future<String> nameFuture) {
return Future.run(complete -> {
nameFuture.onComplete(name -> complete.test("Hi " + name));
});
}
The computation receives a Predicate
, named complete
by convention,
that takes a result of type Try<T>
and returns a boolean that states whether the
Future was completed.
Future completion is an idempotent operation in the way that the first call of complete
will return true, successive calls will return false.
T
- Type of the resulttask
- A computational taskFuture
instance@Deprecated static <T> Future<T> run(Executor executor, Task<? extends T> task)
computation
using a completion handler:
CheckedConsumer<Predicate<Try<T>>> computation = complete -> {
// computation
};
The computation
is executed synchronously. It requires to complete the returned Future.
A common use-case is to hand over the complete
predicate to another Future
in order to prevent blocking:
Future<String> greeting(Future<String> nameFuture) {
return Future.run(complete -> {
nameFuture.onComplete(name -> complete.with("Hi " + name));
});
}
The computation receives a Predicate
, named complete
by convention,
that takes a result of type Try<T>
and returns a boolean that states whether the
Future was completed.
Future completion is an idempotent operation in the way that the first call of complete
will return true, successive calls will return false.
T
- Type of the resultexecutor
- An Executor
that runs the given computation
task
- A computational taskFuture
instancestatic <T> Future<T> reduce(Iterable<? extends Future<? extends T>> futures, BiFunction<? super T,? super T,? extends T> f)
The resulting Future
is backed by the DEFAULT_EXECUTOR
.
T
- The result type of the given Futures
.futures
- An iterable of futures.f
- The reduce operation.Future
that will contain the reduce result.NullPointerException
- if executor, futures or f is null.static <T> Future<T> reduce(Executor executor, Iterable<? extends Future<? extends T>> futures, BiFunction<? super T,? super T,? extends T> f)
The resulting Future
is backed by the given Executor
.
T
- The result type of the given Futures
.executor
- An Executor
.futures
- An iterable of futures.f
- The reduce operation.Future
that will contain the reduce result.NullPointerException
- if executor, futures or f is null.static Future<Void> run(CheckedRunnable unit)
DEFAULT_EXECUTOR
.unit
- A unit of work.NullPointerException
- if unit is null.static Future<Void> run(Executor executor, CheckedRunnable unit)
Executor
.executor
- An Executor
.unit
- A unit of work.NullPointerException
- if one of executor or unit is null.static <T> Future<Seq<T>> sequence(Iterable<? extends Future<? extends T>> futures)
Future
s into a single Future
by transforming an
Iterable<Future<? extends T>>
into a Future<Seq<T>>
.
The resulting Future
is backed by the DEFAULT_EXECUTOR
.
// = Future(Success(Seq(1, 2)))
sequence(
List.of(
Future.of(() -> 1),
Future.of(() -> 2)
)
);
// = Future(Failure(Error)))
sequence(
List.of(
Future.of(() -> 1),
Future.of(() -> { throw new Error(); }
)
);
T
- Result type of the futures.futures
- An Iterable
of Future
s.Future
of a Seq
of results.NullPointerException
- if futures is null.static <T> Future<Seq<T>> sequence(Executor executor, Iterable<? extends Future<? extends T>> futures)
Future
s into a single Future
by transforming an
Iterable<Future<? extends T>>
into a Future<Seq<T>>
.
The resulting Future
is backed by the given Executor
.
T
- Result type of the futures.executor
- An Executor
.futures
- An Iterable
of Future
s.Future
of a Seq
of results.NullPointerException
- if executor or futures is null.static <T> Future<T> successful(T result)
Future
, backed by the DEFAULT_EXECUTOR
.T
- The value type of a successful result.result
- The result.Future
.static <T> Future<T> successful(Executor executor, T result)
Future
, backed by the given Executor
.T
- The value type of a successful result.executor
- An Executor
.result
- The result.Future
.NullPointerException
- if executor is null@GwtIncompatible default CompletableFuture<T> toCompletableFuture()
Value
CompletableFuture
toCompletableFuture
in interface Value<T>
CompletableFuture
containing the valuestatic <T,U> Future<Seq<U>> traverse(Iterable<? extends T> values, Function<? super T,? extends Future<? extends U>> mapper)
Future
by
transforming an Iterable<? extends T>
into a Future<Seq<U>>
.
The resulting Future
is backed by the DEFAULT_EXECUTOR
.
T
- The type of the given values.U
- The mapped value type.values
- An Iterable
of Future
s.mapper
- A mapper of values to FuturesFuture
of a Seq
of results.NullPointerException
- if values or f is null.static <T,U> Future<Seq<U>> traverse(Executor executor, Iterable<? extends T> values, Function<? super T,? extends Future<? extends U>> mapper)
Future
by
transforming an Iterable<? extends T>
into a Future<Seq<U>>
.
The resulting Future
is backed by the given Executor
.
T
- The type of the given values.U
- The mapped value type.executor
- An Executor
.values
- An Iterable
of values.mapper
- A mapper of values to FuturesFuture
of a Seq
of results.NullPointerException
- if executor, values or f is null.default Future<T> andThen(Consumer<? super Try<T>> action)
An exception, which occurs when performing the given action
, is not propagated to the outside.
In other words, subsequent actions are performed based on the value of the original Future.
Example:
// prints Success(1)
Future.of(() -> 1)
.andThen(t -> { throw new Error(""); })
.andThen(System.out::println);
action
- A side-effecting action.NullPointerException
- if action is nullFuture<T> await()
In the case the current thread was interrupted while waiting, a failed Future
is returned containing
the corresponding InterruptedException
.
Future
instanceFuture<T> await(long timeout, TimeUnit unit)
In the case the current thread was interrupted while waiting, a failed Future
is returned containing
the corresponding InterruptedException
.
If the deadline wasn't met, a failed Future
is returned containing a TimeoutException
.
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentFuture
instanceIllegalArgumentException
- if timeout
is negativeNullPointerException
- if unit
is nulldefault boolean cancel()
If the Future was successfully cancelled, the result is a Failure(CancellationException)
.
false
, if this Future
is already completed or could not be cancelled, otherwise true
.SecurityException
- if the current thread cannot modify the Future's threadisCancelled()
boolean cancel(boolean mayInterruptIfRunning)
Executor
.
If the Future was successfully cancelled, the result is a Failure(CancellationException)
.
mayInterruptIfRunning
- true
if a running thread should be interrupted, otherwise a running thread
is allowed to complete its computation.false
, if this Future
is already completed or could not be cancelled, otherwise true
.SecurityException
- if the current thread cannot modify the Future's threadisCancelled()
,
Future.cancel(boolean)
default <R> Future<R> collect(PartialFunction<? super T,? extends R> partialFunction)
partialFunction
by mapping the value to type R
.
partialFunction.isDefinedAt(value)
If the element makes it through that filter, the mapped instance is wrapped in Future
R newValue = partialFunction.apply(value)
R
- The new value typepartialFunction
- A function that is not necessarily defined on value of this future.Future
instance containing value of type R
NullPointerException
- if partialFunction
is nulldefault Executor executor()
Executor
used by this Future
.Executor
.@Deprecated ExecutorService executorService() throws UnsupportedOperationException
executor()
instead.
THE DEFAULT IMPLEMENTATION (obtained by one of the Future
factory methods) MIGHT THROW AN
UnsupportedOperationException
AT RUNTIME.
UnsupportedOperationException
- if the underlying Executor
isn't an ExecutorService
.default Future<Throwable> failed()
If this Future succeeds, the failed projection returns a failure containing a NoSuchElementException
.
If this Future fails, the failed projection returns a success containing the exception.
default Future<T> fallbackTo(Future<? extends T> that)
that
Future is returned, if that is a success. If both Futures fail, the failure
of this Future is returned.
Example:
Future<Integer> future = Future.of(() -> { throw new Error(); });
Future<Integer> that = Future.of(() -> 1);
Future<Integer> result = future.fallbackTo(that);
// prints Some(1)
result.onComplete(System.out::println);
that
- A fallback future computationNullPointerException
- if that is nulldefault Future<T> filter(Predicate<? super T> predicate)
filterTry(predicate::test
.predicate
- A predicateFuture
NullPointerException
- if predicate
is nulldefault Future<T> filterTry(CheckedPredicate<? super T> predicate)
Future
by calling Try.filterTry(CheckedPredicate)
.predicate
- A checked predicateFuture
NullPointerException
- if predicate
is nulldefault Option<Throwable> getCause()
future.getValue().map(Try::getCause)
.UnsupportedOperationException
- if the Future was successfully completed with a valueOption<Try<T>> getValue()
None
, if the Future is not yet completed or was cancelled, otherwise Some(Try)
.boolean isCancelled()
boolean isCompleted()
default boolean isSuccess()
default boolean isFailure()
Future<T> onComplete(Consumer<? super Try<T>> action)
action
- An action to be performed when this future is complete.NullPointerException
- if action
is null.default Future<T> onFailure(Consumer<? super Throwable> action)
Try.Failure
. Please note that the
future is also a failure when it was cancelled.action
- An action to be performed when this future failed.NullPointerException
- if action
is null.default Future<T> onSuccess(Consumer<? super T> action)
Try.Success
.action
- An action to be performed when this future succeeded.NullPointerException
- if action
is null.default Future<T> recover(Function<? super Throwable,? extends T> f)
Example:
// = "oh!"
Future.of(() -> new Error("oh!")).recover(Throwable::getMessage);
f
- A function which takes the exception of a failure and returns a new value.NullPointerException
- if f
is nulldefault Future<T> recoverWith(Function<? super Throwable,? extends Future<? extends T>> f)
Example:
// = "oh!"
Future.of(() -> { throw new Error("oh!"); }).recoverWith(x -> Future.of(x::getMessage));
f
- A function which takes the exception of a failure and returns a new future.NullPointerException
- if f
is nulldefault <U> U transform(Function<? super Future<T>,? extends U> f)
Future
.U
- Type of transformation resultf
- A transformationU
NullPointerException
- if f
is nulldefault <U> Future<U> transformValue(Function<? super Try<T>,? extends Try<? extends U>> f)
Future
, whether it is a success or a failure.U
- Generic type of transformation Try
resultf
- A transformationFuture
of type U
NullPointerException
- if f
is nulldefault <U> Future<Tuple2<T,U>> zip(Future<? extends U> that)
If this Future failed the result contains this failure. Otherwise the result contains that failure or a tuple of both successful Future results.
U
- Result type of that
that
- Another FutureNullPointerException
- if that
is nulldefault <U,R> Future<R> zipWith(Future<? extends U> that, BiFunction<? super T,? super U,? extends R> combinator)
If this Future failed the result contains this failure. Otherwise the result contains that failure or a combination of both successful Future results.
U
- Result type of that
R
- Result type of f
that
- Another Futurecombinator
- The combinator functionNullPointerException
- if that
is nulldefault <U> Future<U> flatMapTry(CheckedFunction1<? super T,? extends Future<? extends U>> mapper)
default void forEach(Consumer<? super T> action)
action
asynchronously hence this Future result becomes available.
The action
is not performed, if the result is a failure.default T get()
Success
or throws if it was a Failure
.
Waits for the result if necessary by blocking the current thread.
IMPORTANT! If the computation result is a Try.Failure
, the underlying cause
of type Throwable
is thrown.
default boolean isAsync()
Futures
's value is computed asynchronously.default boolean isEmpty()
default boolean isLazy()
Future
's value is computed eagerly.default boolean isSingleValued()
Future
is single-valued.isSingleValued
in interface Value<T>
true
default Iterator<T> iterator()
Value
io.vavr.collection.Iterator
.default <U> Future<U> map(Function<? super T,? extends U> mapper)
Value
default <U> Future<U> mapTry(CheckedFunction1<? super T,? extends U> mapper)
default Future<T> peek(Consumer<? super T> action)
Value
action
on the first element if this is an eager implementation.
Performs the given action
on all elements (the first immediately, successive deferred),
if this is a lazy implementation.default String stringPrefix()
Value
stringPrefix
in interface Value<T>
Copyright © 2019. All Rights Reserved.