Top Description Fields Methods
io.vavr.concurrent

public Interface Future<T>

extends Value<T>
Known Direct Implementers
io.vavr.concurrent.FutureImpl
Annotations
@SuppressWarnings:deprecation
Type Parameters
<T>
Type of the computation result.
Imports
io.vavr.*, io.vavr.collection.Iterator, .Seq, .Stream, .List, io.vavr.control.Option, .Try, java.util.NoSuchElementException, .Objects, java.util.concurrent.*, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, java.util.concurrent.locks.Lock, .ReentrantLock, java.util.function.*

A Future is a computation result that becomes available at some point. All operations provided are non-blocking.

The underlying Executor is used to execute asynchronous handlers, e.g. via onComplete(...).

A Future has two states: pending and completed.

Callbacks may be registered on a Future at each point of time. These actions are performed as soon as the Future is completed. An action which is registered on a completed Future is immediately performed. The action may run on a separate Thread, depending on the underlying Executor. Actions which are registered on a cancelled Future are performed with the failed result.
Author
Daniel Dietrich

Field Summary

Modifier and TypeField and Description
public static final Executor
public static final ExecutorService
DEFAULT_EXECUTOR_SERVICE

Deprecated Will be removed in Vavr 1.0. Use instead.
The default executor service is ForkJoinPool#commonPool().

Method Summary

Modifier and TypeMethod and Description
public default Future<T>

Returns:

A new Future that contains this result and which is completed after the given action was performed.
andThen
(Consumer<? super Try<T>>
A side-effecting action.
action
)

Support for chaining of callbacks that are guaranteed to be executed in a specific order.

public Future<T>

Returns:

this Future instance
await
()

Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

public Future<T>

Returns:

this Future instance
await
(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

public default boolean

Returns:

false, if this Future is already completed or could not be cancelled, otherwise true.
cancel
()

Cancels the Future.

public boolean

Returns:

false, if this Future is already completed or could not be cancelled, otherwise true.
cancel
(boolean
true if a running thread should be interrupted, otherwise a running thread is allowed to complete its computation.
mayInterruptIfRunning
)

Cancels the Future.

public default <
The new value type
R
>
Future<R>

Returns:

A new Future instance containing value of type R
collect
(PartialFunction<? super T, ? extends R>
A function that is not necessarily defined on value of this future.
partialFunction
)

Collects value that is in the domain of the given partialFunction by mapping the value to type R.

public default Executor

Returns:

The underlying Executor.
executor
()

Returns the Executor used by this Future.

public ExecutorService

Returns:

(never)
executorService
()
Deprecated Removed starting with Vavr 0.10.0, use executor() instead.

This method is deprecated.

public static <
The value type of a successful result.
T
>
Future<T>

Returns:

A failed Future.
failed
(Throwable
The reason why it failed.
exception
)

Creates a failed Future with the given exception, backed by the DEFAULT_EXECUTOR.

public static <
The value type of a successful result.
T
>
Future<T>

Returns:

A failed Future.
failed
(Executor executor, Throwable
The reason why it failed.
exception
)

Creates a failed Future with the given exception, backed by the given Executor.

public default Future<Throwable>

Returns:

A new Future which contains an exception at a point of time.
failed
()

A projection that inverses the result of this Future.

public default Future<T>

Returns:

A new Future
fallbackTo
(Future<? extends T>
A fallback future computation
that
)

Returns a Future that returns the result of this Future, if it is a success.

public default Future<T>

Returns:

A new Future
filter
(Predicate<? super T>
A predicate
predicate
)

Shortcut for filterTry(predicate::test.

public default Future<T>

Returns:

A new Future
filterTry
(CheckedPredicate<? super T>
A checked predicate
predicate
)

Filters the result of this Future by calling Try#filterTry(CheckedPredicate).

public static <
Result type of the futures.
T
>
Future<Option<T>>

Returns:

A Future of an Option of the first result of the given futures that satisfies the given predicate.
find
(Iterable<? extends Future<? extends T>>
An iterable of futures.
futures
,
Predicate<? super T>
A predicate that tests successful future results.
predicate
)

Returns a Future that eventually succeeds with the first result of the given Futures which matches the given predicate.

public static <
Result type of the futures.
T
>
Future<Option<T>>

Returns:

A Future of an Option of the first result of the given futures that satisfies the given predicate.
find
(Executor executor, Iterable<? extends Future<? extends T>>
An iterable of futures.
futures
,
Predicate<? super T>
A predicate that tests successful future results.
predicate
)

Returns a Future that eventually succeeds with the first result of the given Futures which matches the given predicate.

public static <
The result type.
T
>
Future<T>

Returns:

A new Future.
firstCompletedOf
(Iterable<? extends Future<? extends T>>
An iterable of futures.
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.

public static <
The result type.
T
>
Future<T>

Returns:

A new Future.
firstCompletedOf
(Executor executor, Iterable<? extends Future<? extends T>>
An iterable of futures.
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.

public default <U> Future<U>
flatMap(Function<? super T, ? extends Future<? extends U>> mapper)

public default <U> Future<U>
flatMapTry(CheckedFunction1<? super T, ? extends Future<? extends U>> mapper)

public static <
The result type of the given Futures.
T
,
The fold result type.
U
>
Future<U>

Returns:

A new Future that will contain the fold result.
fold
(Iterable<? extends Future<? extends T>>
An iterable of futures.
futures
,
U
The zero element of the fold.
zero
,
BiFunction<? super U, ? super T, ? extends U>
The fold operation.
f
)

Returns a Future which contains the result of the fold of the given future values.

public static <
The result type of the given Futures.
T
,
The fold result type.
U
>
Future<U>

Returns:

A new Future that will contain the fold result.
fold
(Executor executor, Iterable<? extends Future<? extends T>>
An iterable of futures.
futures
,
U
The zero element of the fold.
zero
,
BiFunction<? super U, ? super T, ? extends U>
The fold operation.
f
)

Returns a Future which contains the result of the fold of the given future values.

public default void
forEach(Consumer<? super T>
A Consumer
action
)

Overrides default io.vavr.Value.forEach.

Performs the given action asynchronously hence this Future result becomes available.

public static <
Result type of the Future
T
>
Future<T>

Returns:

A new Future wrapping the result of the java.util.concurrent.CompletableFuture
fromCompletableFuture
(CompletableFuture<T> future)

Creates a Future with the given java.util.concurrent.CompletableFuture, backed by the DEFAULT_EXECUTOR

public static <
Result type of the Future
T
>
Future<T>

Returns:

A new Future wrapping the result of the java.util.concurrent.CompletableFuture
fromCompletableFuture
(Executor executor, CompletableFuture<T> future)

Creates a Future with the given java.util.concurrent.CompletableFuture, backed by given Executor

public static <
Result type of the Future
T
>
Future<T>

Returns:

A new Future wrapping the result of the Java future
fromJavaFuture
(Future<T> future)

Creates a Future with the given java.util.concurrent.Future, backed by the DEFAULT_EXECUTOR

public static <
Result type of the Future
T
>
Future<T>

Returns:

A new Future wrapping the result of the Java future
fromJavaFuture
(Executor executor, Future<T> future)

Creates a Future with the given java.util.concurrent.Future, backed by given Executor

public static <
The value type of a successful result.
T
>
Future<T>

Returns:

A completed Future which contains either a Success or a Failure.
fromTry
(Try<? extends T>
The result.
result
)

Creates a Future from a Try, backed by the DEFAULT_EXECUTOR.

public static <
The value type of a successful result.
T
>
Future<T>

Returns:

A completed Future which contains either a Success or a Failure.
fromTry
(Executor executor, Try<? extends T>
The result.
result
)

Creates a Future from a Try, backed by the given Executor.

public default T

Returns:

The value of this Future.
get
()

Implements io.vavr.Value.get.

Gets the value if the computation result is a Success or throws if it was a Failure.

public default Option<Throwable>

Returns:

None if the Future is not completed yet. Returns Some(Throwable) if the Future was completed with a failure.
getCause
()

Returns the underlying exception of this Future, syntactic sugar for future.getValue().map(Try::getCause).

public Option<Try<T>>

Returns:

None, if the Future is not yet completed or was cancelled, otherwise Some(Try).
getValue
()

Returns the value of the Future.

public default boolean

Returns:

true
isAsync
()

Implements io.vavr.Value.isAsync.

A Futures's value is computed asynchronously.

public boolean

Returns:

true, if the computation was cancelled, false otherwise
isCancelled
()

Checks if this Future is cancelled, i.e. the thread was forced to stop before completion.

public boolean

Returns:

true, if the computation successfully finished, failed or was cancelled, false otherwise.
isCompleted
()

Checks if this Future is completed, i.e. has a value.

public default boolean

Returns:

true, if this future succeeded with a value, false otherwise.
isEmpty
()

Implements io.vavr.Value.isEmpty.

Checks, if this future has a value.

public default boolean

Returns:

true, if this Future completed and is a Failure, false otherwise.
isFailure
()

Checks if this Future completed with a failure.

public default boolean

Returns:

false
isLazy
()

Implements io.vavr.Value.isLazy.

A Future's value is computed eagerly.

public default boolean

Returns:

true
isSingleValued
()

Implements io.vavr.Value.isSingleValued.

A Future is single-valued.

public default boolean

Returns:

true, if this Future completed and is a Success, false otherwise.
isSuccess
()

Checks if this Future completed with a success.

public default Iterator<T>
iterator()

Implements io.vavr.Value.iterator.

Returns a rich io.vavr.collection.Iterator.

public default <U> Future<U>
map(Function<? super T, ? extends U>
A mapper
mapper
)

Implements io.vavr.Value.map.

Maps the underlying value to a different component type.

public default <U> Future<U>
mapTry(CheckedFunction1<? super T, ? extends U> mapper)

public static <
Component type of the Future.
T
>
Future<T>

Returns:

the given future instance as narrowed type Future<T>.
narrow
(Future<? extends T>
A Future.
future
)

Narrows a widened Future<?

public static <
Type of the computation result.
T
>
Future<T>

Returns:

A new Future instance.
of
(CheckedFunction0<? extends T>
A computation.
computation
)

Starts an asynchronous computation, backed by the DEFAULT_EXECUTOR.

public static <
Type of the computation result.
T
>
Future<T>

Returns:

A new Future instance.
of
(Executor executor, CheckedFunction0<? extends T>
A computation.
computation
)

Starts an asynchronous computation, backed by the given Executor.

public static <
Type of the computation result.
T
>
Future<T>

Returns:

A new Future instance
ofCallable
(Callable<? extends T>
A computation
computation
)
Deprecated Will be removed. Use Future.of(callable::call) instead of Future.ofCallable(callable).

Starts an asynchronous computation, backed by the DEFAULT_EXECUTOR.

public static <
Type of the computation result.
T
>
Future<T>

Returns:

A new Future instance
ofCallable
(Executor
An executor service.
executor
,
Callable<? extends T>
A computation.
computation
)
Deprecated Will be removed. Use Future.of(executor, callable::call) instead of Future.ofCallable(executor, callable).

Starts an asynchronous computation, backed by the given Executor.

public static <
Type of the computation result.
T
>
Future<T>

Returns:

A new Future instance
ofSupplier
(Supplier<? extends T>
A computation.
computation
)
Deprecated Will be removed. Use Future.of(supplier::get) instead of Future.ofSupplier(supplier).

Starts an asynchronous computation, backed by the DEFAULT_EXECUTOR.

public static <
Type of the computation result.
T
>
Future<T>

Returns:

A new Future instance
ofSupplier
(Executor
An executor service.
executor
,
Supplier<? extends T>
A computation.
computation
)
Deprecated Will be removed. Use Future.of(executor, supplier::get) instead of Future.ofSupplier(executor, supplier).

Starts an asynchronous computation, backed by the given Executor.

public Future<T>

Returns:

this Future
onComplete
(Consumer<? super Try<T>>
An action to be performed when this future is complete.
action
)

Performs the action once the Future is complete.

public default Future<T>

Returns:

this Future
onFailure
(Consumer<? super Throwable>
An action to be performed when this future failed.
action
)

Performs the action once the Future is complete and the result is a Try.Failure.

public default Future<T>

Returns:

this Future
onSuccess
(Consumer<? super T>
An action to be performed when this future succeeded.
action
)

Performs the action once the Future is complete and the result is a Try.Success.

public default Future<T>
orElse(Future<? extends T> other)

public default Future<T>
orElse(Supplier<? extends Future<? extends T>> supplier)

public default Future<T>
peek(Consumer<? super T>
The action that will be performed on the element(s).
action
)

Implements io.vavr.Value.peek.

Performs the given action on the first element if this is an eager implementation.

public default Future<T>

Returns:

A new Future.
recover
(Function<? super Throwable, ? extends T>
A function which takes the exception of a failure and returns a new value.
f
)

Handles a failure of this Future by returning another result.

public default Future<T>

Returns:

A new Future.
recoverWith
(Function<? super Throwable, ? extends Future<? extends T>>
A function which takes the exception of a failure and returns a new future.
f
)

Handles a failure of this Future by returning the result of another Future.

public static <
The result type of the given Futures.
T
>
Future<T>

Returns:

A new Future that will contain the reduce result.
reduce
(Iterable<? extends Future<? extends T>>
An iterable of futures.
futures
,
BiFunction<? super T, ? super T, ? extends T>
The reduce operation.
f
)

Returns a Future which contains the reduce result of the given future values.

public static <
The result type of the given Futures.
T
>
Future<T>

Returns:

A new Future that will contain the reduce result.
reduce
(Executor executor, Iterable<? extends Future<? extends T>>
An iterable of futures.
futures
,
BiFunction<? super T, ? super T, ? extends T>
The reduce operation.
f
)

Returns a Future which contains the reduce result of the given future values.

public static <
Type of the result
T
>
Future<T>

Returns:

a new Future instance
run
(Task<? extends T>
A computational task
task
)
Deprecated Experimental API

Creates a (possibly blocking) Future that runs the results of the given computation using a completion handler:

CheckedConsumer<Predicate<Try<T>>> computation = complete -> {
    // computation
};
public static <
Type of the result
T
>
Future<T>

Returns:

a new Future instance
run
(Executor
An Executor that runs the given computation
executor
,
Task<? extends T>
A computational task
task
)
Deprecated Experimental API

Creates a (possibly blocking) Future that runs the results of the given computation using a completion handler:

CheckedConsumer<Predicate<Try<T>>> computation = complete -> {
    // computation
};
public static Future<Void>

Returns:

A new Future instance which results in nothing.
run
(CheckedRunnable
A unit of work.
unit
)

Runs an asynchronous computation, backed by the DEFAULT_EXECUTOR.

public static Future<Void>

Returns:

A new Future instance which results in nothing.
run
(Executor executor, CheckedRunnable
A unit of work.
unit
)

Starts an asynchronous computation, backed by the given Executor.

public static Future<Void>

Returns:

A new Future instance
runRunnable
(Runnable
A computation
computation
)
Deprecated Will be removed. Use Future.of(runnable::run) instead of Future.runRunnable(runnable).

Starts an asynchronous computation, backed by the DEFAULT_EXECUTOR.

public static Future<Void>

Returns:

A new Future instance
runRunnable
(Executor
An executor service.
executor
,
Runnable
A computation.
computation
)
Deprecated Will be removed. Use Future.of(executor, runnable::run) instead of Future.runRunnable(executor, runnable).

Starts an asynchronous computation, backed by the given Executor.

public static <
Result type of the futures.
T
>
Future<Seq<T>>

Returns:

A Future of a Seq of results.
sequence
(Iterable<? extends Future<? extends T>>
An Iterable of Futures.
futures
)

Reduces many Futures into a single Future by transforming an Iterable<Future<?

public static <
Result type of the futures.
T
>
Future<Seq<T>>

Returns:

A Future of a Seq of results.
sequence
(Executor executor, Iterable<? extends Future<? extends T>>
An Iterable of Futures.
futures
)

Reduces many Futures into a single Future by transforming an Iterable<Future<?

public default String
stringPrefix()

Implements io.vavr.Value.stringPrefix.

Returns the name of this Value type, which is used by toString().

public static <
The value type of a successful result.
T
>
Future<T>

Returns:

A succeeded Future.
successful
(T
The result.
result
)

Creates a succeeded Future, backed by the DEFAULT_EXECUTOR.

public static <
The value type of a successful result.
T
>
Future<T>

Returns:

A succeeded Future.
successful
(Executor executor, T
The result.
result
)

Creates a succeeded Future, backed by the given Executor.

public default CompletableFuture<T>
public default <
Type of transformation result
U
>
U

Returns:

An instance of type U
transform
(Function<? super Future<T>, ? extends U>
A transformation
f
)

Transforms this Future.

public default <
Generic type of transformation Try result
U
>
Future<U>

Returns:

A Future of type U
transformValue
(Function<? super Try<T>, ? extends Try<? extends U>>
A transformation
f
)

Transforms the value of this Future, whether it is a success or a failure.

public static <
The type of the given values.
T
,
The mapped value type.
U
>
Future<Seq<U>>

Returns:

A Future of a Seq of results.
traverse
(Iterable<? extends T>
An Iterable of Futures.
values
,
Function<? super T, ? extends Future<? extends U>>
A mapper of values to Futures
mapper
)

Maps the values of an iterable in parallel to a sequence of mapped values into a single Future by transforming an Iterable<?

public static <
The type of the given values.
T
,
The mapped value type.
U
>
Future<Seq<U>>

Returns:

A Future of a Seq of results.
traverse
(Executor executor, Iterable<? extends T>
An Iterable of values.
values
,
Function<? super T, ? extends Future<? extends U>>
A mapper of values to Futures
mapper
)

Maps the values of an iterable in parallel to a sequence of mapped values into a single Future by transforming an Iterable<?

public default <
Result type of that
U
>
Future<Tuple2<T, U>>

Returns:

A new Future that returns both Future results.
zip
(Future<? extends U>
Another Future
that
)

Returns a tuple of this and that Future result.

public default <
Result type of that
U
,
Result type of f
R
>
Future<R>

Returns:

A new Future that returns both Future results.
zipWith
(Future<? extends U>
Another Future
that
,
BiFunction<? super T, ? super U, ? extends R>
The combinator function
combinator
)

Returns a this and that Future result combined using a given combinator function.

Inherited from io.vavr.Value:
collectcollectcontainscorrespondseqequalsexistsforAllgetOrElsegetOrElsegetOrElseThrowgetOrElseTrygetOrNullhashCodenarrowoutoutspliteratorstderrstdouttoArraytoCharSeqtoEithertoEithertoInvalidtoInvalidtoJavaArraytoJavaArraytoJavaArraytoJavaCollectiontoJavaListtoJavaListtoJavaMaptoJavaMaptoJavaMaptoJavaOptionaltoJavaParallelStreamtoJavaSettoJavaSettoJavaStreamtoLefttoLefttoLinkedMaptoLinkedMaptoLinkedSettoListtoMaptoMaptoOptiontoPriorityQueuetoPriorityQueuetoQueuetoRighttoRighttoSettoSortedMaptoSortedMaptoSortedMaptoSortedMaptoSortedSettoSortedSettoStreamtoStringtoTreetoTreetoTrytoTrytoValidtoValidtoValidationtoValidationtoVector