T - Type of the computation result.public interface Future<T> extends java.lang.Iterable<T>, 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 java.util.concurrent.Executor |
DEFAULT_EXECUTOR
The default executor is
ForkJoinPool.commonPool(). |
static java.util.concurrent.ExecutorService |
DEFAULT_EXECUTOR_SERVICE
Deprecated.
Will be removed in Vavr 1.0. Use
instead. |
| Modifier and Type | Method and Description |
|---|---|
default Future<T> |
andThen(java.util.function.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,
java.util.concurrent.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 java.util.concurrent.Executor |
executor()
Returns the
Executor used by this Future. |
java.util.concurrent.ExecutorService |
executorService()
Deprecated.
Removed starting with Vavr 0.10.0, use
executor() instead. |
default Future<java.lang.Throwable> |
failed()
A projection that inverses the result of this Future.
|
static <T> Future<T> |
failed(java.util.concurrent.Executor executor,
java.lang.Throwable exception)
Creates a failed
Future with the given exception, backed by the given Executor. |
static <T> Future<T> |
failed(java.lang.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(java.util.function.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(java.util.concurrent.Executor executor,
java.lang.Iterable<? extends Future<? extends T>> futures,
java.util.function.Predicate<? super T> predicate)
Returns a
Future that eventually succeeds with the first result of the given Futures which
matches the given predicate. |
static <T> Future<Option<T>> |
find(java.lang.Iterable<? extends Future<? extends T>> futures,
java.util.function.Predicate<? super T> predicate)
Returns a
Future that eventually succeeds with the first result of the given Futures which
matches the given predicate. |
static <T> Future<T> |
firstCompletedOf(java.util.concurrent.Executor executor,
java.lang.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(java.lang.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(java.util.function.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(java.util.concurrent.Executor executor,
java.lang.Iterable<? extends Future<? extends T>> futures,
U zero,
java.util.function.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(java.lang.Iterable<? extends Future<? extends T>> futures,
U zero,
java.util.function.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(java.util.function.Consumer<? super T> action)
Performs the given
action asynchronously hence this Future result becomes available. |
static <T> Future<T> |
fromCompletableFuture(java.util.concurrent.CompletableFuture<T> future)
|
static <T> Future<T> |
fromCompletableFuture(java.util.concurrent.Executor executor,
java.util.concurrent.CompletableFuture<T> future)
Creates a
Future with the given CompletableFuture, backed by given Executor |
static <T> Future<T> |
fromJavaFuture(java.util.concurrent.Executor executor,
java.util.concurrent.Future<T> future)
Creates a
Future with the given java.util.concurrent.Future, backed by given Executor |
static <T> Future<T> |
fromJavaFuture(java.util.concurrent.Future<T> future)
Creates a
Future with the given java.util.concurrent.Future, backed by the DEFAULT_EXECUTOR |
static <T> Future<T> |
fromTry(java.util.concurrent.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<java.lang.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(java.util.function.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(java.util.concurrent.Executor executor,
CheckedFunction0<? extends T> computation)
Starts an asynchronous computation, backed by the given
Executor. |
static <T> Future<T> |
ofCallable(java.util.concurrent.Callable<? extends T> computation)
Deprecated.
Will be removed. Use
Future.of(callable::call) instead of Future.ofCallable(callable). |
static <T> Future<T> |
ofCallable(java.util.concurrent.Executor executor,
java.util.concurrent.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(java.util.concurrent.Executor executor,
java.util.function.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(java.util.function.Supplier<? extends T> computation)
Deprecated.
Will be removed. Use
Future.of(supplier::get) instead of Future.ofSupplier(supplier). |
Future<T> |
onComplete(java.util.function.Consumer<? super Try<T>> action)
Performs the action once the Future is complete.
|
default Future<T> |
onFailure(java.util.function.Consumer<? super java.lang.Throwable> action)
Performs the action once the Future is complete and the result is a
Try.Failure. |
default Future<T> |
onSuccess(java.util.function.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(java.util.function.Supplier<? extends Future<? extends T>> supplier) |
default Future<T> |
peek(java.util.function.Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
default <X extends java.lang.Throwable> |
recover(java.lang.Class<X> exceptionType,
java.util.function.Function<? super X,? extends T> f)
Handles a failure of this Future by returning another result.
|
default Future<T> |
recover(java.util.function.Function<? super java.lang.Throwable,? extends T> f)
Handles a failure of this Future by returning another result.
|
default Future<T> |
recoverWith(java.util.function.Function<? super java.lang.Throwable,? extends Future<? extends T>> f)
Handles a failure of this Future by returning the result of another Future.
|
static <T> Future<T> |
reduce(java.util.concurrent.Executor executor,
java.lang.Iterable<? extends Future<? extends T>> futures,
java.util.function.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(java.lang.Iterable<? extends Future<? extends T>> futures,
java.util.function.BiFunction<? super T,? super T,? extends T> f)
Returns a Future which contains the reduce result of the given future values.
|
static Future<java.lang.Void> |
run(CheckedRunnable unit)
Runs an asynchronous computation, backed by the
DEFAULT_EXECUTOR. |
static Future<java.lang.Void> |
run(java.util.concurrent.Executor executor,
CheckedRunnable unit)
Starts an asynchronous computation, backed by the given
Executor. |
static <T> Future<T> |
run(java.util.concurrent.Executor executor,
Task<? extends T> task)
Deprecated.
Experimental API
|
static <T> Future<T> |
run(Task<? extends T> task)
Deprecated.
Experimental API
|
static Future<java.lang.Void> |
runRunnable(java.util.concurrent.Executor executor,
java.lang.Runnable computation)
Deprecated.
Will be removed. Use
Future.of(executor, runnable::run) instead of Future.runRunnable(executor, runnable). |
static Future<java.lang.Void> |
runRunnable(java.lang.Runnable computation)
Deprecated.
Will be removed. Use
Future.of(runnable::run) instead of Future.runRunnable(runnable). |
static <T> Future<Seq<T>> |
sequence(java.util.concurrent.Executor executor,
java.lang.Iterable<? extends Future<? extends T>> futures)
Reduces many
Futures into a single Future by transforming an
Iterable<Future<? extends T>> into a Future<Seq<T>>. |
static <T> Future<Seq<T>> |
sequence(java.lang.Iterable<? extends Future<? extends T>> futures)
Reduces many
Futures into a single Future by transforming an
Iterable<Future<? extends T>> into a Future<Seq<T>>. |
default java.lang.String |
stringPrefix()
Returns the name of this Value type, which is used by toString().
|
static <T> Future<T> |
successful(java.util.concurrent.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 java.util.concurrent.CompletableFuture<T> |
toCompletableFuture()
Converts this to a
CompletableFuture |
default <U> U |
transform(java.util.function.Function<? super Future<T>,? extends U> f)
Transforms this
Future. |
default <U> Future<U> |
transformValue(java.util.function.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(java.util.concurrent.Executor executor,
java.lang.Iterable<? extends T> values,
java.util.function.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(java.lang.Iterable<? extends T> values,
java.util.function.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,
java.util.function.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 java.util.concurrent.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.
ForkJoinPool.awaitQuiescence(long, TimeUnit)static final java.util.concurrent.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.
ForkJoinPool.awaitQuiescence(long, TimeUnit)static <T> Future<T> failed(java.lang.Throwable exception)
T - The value type of a successful result.exception - The reason why it failed.Future.java.lang.NullPointerException - if exception is nullstatic <T> Future<T> failed(java.util.concurrent.Executor executor, java.lang.Throwable exception)
Future with the given exception, backed by the given Executor.T - The value type of a successful result.executor - An Executor.exception - The reason why it failed.Future.java.lang.NullPointerException - if executor or exception is nullstatic <T> Future<Option<T>> find(java.lang.Iterable<? extends Future<? extends T>> futures, java.util.function.Predicate<? super T> predicate)
Future that eventually succeeds with the first result of the given Futures 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.java.lang.NullPointerException - if one of the arguments is nullstatic <T> Future<Option<T>> find(java.util.concurrent.Executor executor, java.lang.Iterable<? extends Future<? extends T>> futures, java.util.function.Predicate<? super T> predicate)
Future that eventually succeeds with the first result of the given Futures 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.java.lang.NullPointerException - if one of the arguments is nullstatic <T> Future<T> firstCompletedOf(java.lang.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.java.lang.NullPointerException - if futures is nullstatic <T> Future<T> firstCompletedOf(java.util.concurrent.Executor executor, java.lang.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.java.lang.NullPointerException - if executor or futures is nullstatic <T,U> Future<U> fold(java.lang.Iterable<? extends Future<? extends T>> futures, U zero, java.util.function.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.java.lang.NullPointerException - if futures or f is null.static <T,U> Future<U> fold(java.util.concurrent.Executor executor, java.lang.Iterable<? extends Future<? extends T>> futures, U zero, java.util.function.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.java.lang.NullPointerException - if executor, futures or f is null.static <T> Future<T> fromJavaFuture(java.util.concurrent.Future<T> future)
Future with the given java.util.concurrent.Future, backed by the DEFAULT_EXECUTORT - Result type of the Futurefuture - A FutureFuture wrapping the result of the Java futurejava.lang.NullPointerException - if future is nullstatic <T> Future<T> fromJavaFuture(java.util.concurrent.Executor executor, java.util.concurrent.Future<T> future)
Future with the given java.util.concurrent.Future, backed by given ExecutorT - Result type of the Futureexecutor - An Executor.future - A Future.Future wrapping the result of the Java futurejava.lang.NullPointerException - if executor or future is nullstatic <T> Future<T> fromCompletableFuture(java.util.concurrent.CompletableFuture<T> future)
T - Result type of the Futurefuture - A CompletableFutureFuture wrapping the result of the CompletableFuturejava.lang.NullPointerException - if future is nullstatic <T> Future<T> fromCompletableFuture(java.util.concurrent.Executor executor, java.util.concurrent.CompletableFuture<T> future)
Future with the given CompletableFuture, backed by given ExecutorT - Result type of the Futureexecutor - An Executor.future - A CompletableFuture.Future wrapping the result of the CompletableFuturejava.lang.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.java.lang.NullPointerException - if result is nullstatic <T> Future<T> fromTry(java.util.concurrent.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.java.lang.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(java.util.function.Supplier<? extends T> computation)
Future.of(supplier::get) instead of Future.ofSupplier(supplier).DEFAULT_EXECUTOR.T - Type of the computation result.computation - A computation.java.lang.NullPointerException - if computation is null.@Deprecated static <T> Future<T> ofSupplier(java.util.concurrent.Executor executor, java.util.function.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.java.lang.NullPointerException - if one of executor or computation is null.@Deprecated static <T> Future<T> ofCallable(java.util.concurrent.Callable<? extends T> computation)
Future.of(callable::call) instead of Future.ofCallable(callable).DEFAULT_EXECUTOR.T - Type of the computation result.computation - A computationjava.lang.NullPointerException - if computation is null.@Deprecated static <T> Future<T> ofCallable(java.util.concurrent.Executor executor, java.util.concurrent.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.java.lang.NullPointerException - if one of executor or computation is null.@Deprecated static Future<java.lang.Void> runRunnable(java.lang.Runnable computation)
Future.of(runnable::run) instead of Future.runRunnable(runnable).DEFAULT_EXECUTOR.computation - A computationjava.lang.NullPointerException - if computation is null.@Deprecated static Future<java.lang.Void> runRunnable(java.util.concurrent.Executor executor, java.lang.Runnable computation)
Future.of(executor, runnable::run) instead of Future.runRunnable(executor, runnable).Executor.executor - An executor service.computation - A computation.java.lang.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.java.lang.NullPointerException - if computation is null.static <T> Future<T> of(java.util.concurrent.Executor executor, CheckedFunction0<? extends T> computation)
Executor.T - Type of the computation result.executor - An Executor.computation - A computation.java.lang.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(java.util.concurrent.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 computationtask - A computational taskFuture instancestatic <T> Future<T> reduce(java.lang.Iterable<? extends Future<? extends T>> futures, java.util.function.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.java.lang.NullPointerException - if executor, futures or f is null.static <T> Future<T> reduce(java.util.concurrent.Executor executor, java.lang.Iterable<? extends Future<? extends T>> futures, java.util.function.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.java.lang.NullPointerException - if executor, futures or f is null.static Future<java.lang.Void> run(CheckedRunnable unit)
DEFAULT_EXECUTOR.unit - A unit of work.java.lang.NullPointerException - if unit is null.static Future<java.lang.Void> run(java.util.concurrent.Executor executor, CheckedRunnable unit)
Executor.executor - An Executor.unit - A unit of work.java.lang.NullPointerException - if one of executor or unit is null.static <T> Future<Seq<T>> sequence(java.lang.Iterable<? extends Future<? extends T>> futures)
Futures 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 Futures.Future of a Seq of results.java.lang.NullPointerException - if futures is null.static <T> Future<Seq<T>> sequence(java.util.concurrent.Executor executor, java.lang.Iterable<? extends Future<? extends T>> futures)
Futures 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 Futures.Future of a Seq of results.java.lang.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(java.util.concurrent.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.java.lang.NullPointerException - if executor is nulldefault java.util.concurrent.CompletableFuture<T> toCompletableFuture()
ValueCompletableFuturetoCompletableFuture in interface Value<T>CompletableFuture containing the valuestatic <T,U> Future<Seq<U>> traverse(java.lang.Iterable<? extends T> values, java.util.function.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 Futures.mapper - A mapper of values to FuturesFuture of a Seq of results.java.lang.NullPointerException - if values or f is null.static <T,U> Future<Seq<U>> traverse(java.util.concurrent.Executor executor, java.lang.Iterable<? extends T> values, java.util.function.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.java.lang.NullPointerException - if executor, values or f is null.default Future<T> andThen(java.util.function.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.java.lang.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, java.util.concurrent.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 instancejava.lang.IllegalArgumentException - if timeout is negativejava.lang.NullPointerException - 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.java.lang.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.java.lang.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 Rjava.lang.NullPointerException - if partialFunction is nulldefault java.util.concurrent.Executor executor()
Executor used by this Future.Executor.@Deprecated
java.util.concurrent.ExecutorService executorService()
throws java.lang.UnsupportedOperationException
executor() instead.
THE DEFAULT IMPLEMENTATION (obtained by one of the Future factory methods) MIGHT THROW AN
UnsupportedOperationException AT RUNTIME.
java.lang.UnsupportedOperationException - if the underlying Executor isn't an ExecutorService.default Future<java.lang.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 computationjava.lang.NullPointerException - if that is nulldefault Future<T> filter(java.util.function.Predicate<? super T> predicate)
filterTry(predicate::test.predicate - A predicateFuturejava.lang.NullPointerException - if predicate is nulldefault Future<T> filterTry(CheckedPredicate<? super T> predicate)
Future by calling Try.filterTry(CheckedPredicate).predicate - A checked predicateFuturejava.lang.NullPointerException - if predicate is nulldefault Option<java.lang.Throwable> getCause()
future.getValue().map(Try::getCause).java.lang.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(java.util.function.Consumer<? super Try<T>> action)
action - An action to be performed when this future is complete.java.lang.NullPointerException - if action is null.default Future<T> onFailure(java.util.function.Consumer<? super java.lang.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.java.lang.NullPointerException - if action is null.default Future<T> onSuccess(java.util.function.Consumer<? super T> action)
Try.Success.action - An action to be performed when this future succeeded.java.lang.NullPointerException - if action is null.default Future<T> recover(java.util.function.Function<? super java.lang.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.java.lang.NullPointerException - if f is nulldefault <X extends java.lang.Throwable> Future<T> recover(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,? extends T> f)
Returns this, if this is a Success or this is a Failure and the cause is not assignable from exceptionType.
Otherwise tries to recover the exception of the failure with f.
Example:
// = "oh!"
Future.of(() -> new Error("oh!")).recover(Error.class, Throwable::getMessage);
X - The type of exception type to recoverexceptionType - Class<X> object defining exception class.f - A function which takes the exception of a failure and returns a new value.java.lang.NullPointerException - if f is nulldefault Future<T> recoverWith(java.util.function.Function<? super java.lang.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.java.lang.NullPointerException - if f is nulldefault <U> U transform(java.util.function.Function<? super Future<T>,? extends U> f)
Future.U - Type of transformation resultf - A transformationUjava.lang.NullPointerException - if f is nulldefault <U> Future<U> transformValue(java.util.function.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 Ujava.lang.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 thatthat - Another Futurejava.lang.NullPointerException - if that is nulldefault <U,R> Future<R> zipWith(Future<? extends U> that, java.util.function.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 thatR - Result type of fthat - Another Futurecombinator - The combinator functionjava.lang.NullPointerException - if that is nulldefault <U> Future<U> flatMap(java.util.function.Function<? super T,? extends Future<? extends U>> mapper)
default <U> Future<U> flatMapTry(CheckedFunction1<? super T,? extends Future<? extends U>> mapper)
default void forEach(java.util.function.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>truedefault Iterator<T> iterator()
Valueio.vavr.collection.Iterator.default <U> Future<U> map(java.util.function.Function<? super T,? extends U> mapper)
Valuedefault <U> Future<U> mapTry(CheckedFunction1<? super T,? extends U> mapper)
default Future<T> orElse(java.util.function.Supplier<? extends Future<? extends T>> supplier)
default Future<T> peek(java.util.function.Consumer<? super T> action)
Valueaction 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 java.lang.String stringPrefix()
ValuestringPrefix in interface Value<T>