Interface Promise<T>
-
- Type Parameters:
T
- The result type of the underlyingFuture
public interface Promise<T>
A Promise is a write-once wrapper around a read-only Future which can complete the underlying Future with a value or an exception.The underlying
Executor
is used to execute asynchronous handlers, e.g. viapromise.future().onComplete(...)
.Creation
Promise offers static factory methods to create new promises which hasn't been fulfilled yet:
- create new promises:
make()
Executor
as argument. This gives us more control over thread creation and thread pool sizes.One-shot API
The main purpose of a
Promise
is to complete its underlyingFuture
. When only a singleThread
will eventually complete thePromise
, we use one of these methods. Calls will throw if thePromise
is already completed.API for competing threads
When multiple
Thread
s may complete ourPromise
, we typically use one of these methods. Calls will gracefully returnfalse
if thePromise
is already completed.
-
-
Method Summary
Modifier and Type Method Description default Promise<T>
complete(Try<? extends T> value)
Completes thisPromise
with the givenvalue
.default Promise<T>
completeWith(Future<? extends T> other)
Completes thisPromise
with the givenFuture
, once thatFuture
is completed.default java.util.concurrent.Executor
executor()
java.util.concurrent.ExecutorService
executorService()
Deprecated.Removed starting with Vavr 0.10.0, useexecutor()
instead.static <T> Promise<T>
failed(java.lang.Throwable exception)
Creates a failedPromise
, backed by theFuture.DEFAULT_EXECUTOR
.static <T> Promise<T>
failed(java.util.concurrent.Executor executor, java.lang.Throwable exception)
Creates a failedPromise
, backed by the givenExecutor
.default Promise<T>
failure(java.lang.Throwable exception)
Completes thisPromise
with the givenexception
.static <T> Promise<T>
fromTry(Try<? extends T> result)
static <T> Promise<T>
fromTry(java.util.concurrent.Executor executor, Try<? extends T> result)
Future<T>
future()
Returns the underlyingFuture
of thisPromise
.default boolean
isCompleted()
Checks if thisPromise
is completed, i.e.static <T> Promise<T>
make()
Makes aPromise
that isn't fulfilled yet, backed by theFuture.DEFAULT_EXECUTOR
.static <T> Promise<T>
make(java.util.concurrent.Executor executor)
Makes aPromise
that isn't fulfilled yet, backed by the givenExecutor
.static <T> Promise<T>
narrow(Promise<? extends T> promise)
Narrows a widenedPromise<? extends T>
toPromise<T>
by performing a type-safe cast.default Promise<T>
success(T value)
Completes thisPromise
with the givenvalue
.static <T> Promise<T>
successful(java.util.concurrent.Executor executor, T result)
Creates a succeededPromise
, backed by the givenExecutor
.static <T> Promise<T>
successful(T result)
Creates a succeededPromise
, backed by theFuture.DEFAULT_EXECUTOR
.boolean
tryComplete(Try<? extends T> value)
Attempts to completes thisPromise
with the givenvalue
.default Promise<T>
tryCompleteWith(Future<? extends T> other)
Attempts to complete thisPromise
with the specifiedFuture
, once thatFuture
is completed.default boolean
tryFailure(java.lang.Throwable exception)
Completes thisPromise
with the givenexception
.default boolean
trySuccess(T value)
Completes thisPromise
with the givenvalue
.
-
-
-
Method Detail
-
failed
static <T> Promise<T> failed(java.lang.Throwable exception)
Creates a failedPromise
, backed by theFuture.DEFAULT_EXECUTOR
.- Type Parameters:
T
- The value type of a successful result.- Parameters:
exception
- The reason why it failed.- Returns:
- A failed
Promise
. - Throws:
java.lang.NullPointerException
- if exception is null
-
failed
static <T> Promise<T> failed(java.util.concurrent.Executor executor, java.lang.Throwable exception)
Creates a failedPromise
, backed by the givenExecutor
.- Type Parameters:
T
- The value type of a successful result.- Parameters:
executor
- AnExecutor
passed to the underlyingFuture
.exception
- The reason why it failed.- Returns:
- A failed
Promise
. - Throws:
java.lang.NullPointerException
- if executor or exception is null
-
fromTry
static <T> Promise<T> fromTry(Try<? extends T> result)
- Type Parameters:
T
- The value type of a successful result.- Parameters:
result
- The result.- Returns:
- A completed
Promise
which contains either aSuccess
or aFailure
. - Throws:
java.lang.NullPointerException
- if result is null
-
fromTry
static <T> Promise<T> fromTry(java.util.concurrent.Executor executor, Try<? extends T> result)
- Type Parameters:
T
- The value type of a successful result.- Parameters:
executor
- AnExecutor
passed to the underlyingFuture
.result
- The result.- Returns:
- A completed
Promise
which contains either aSuccess
or aFailure
. - Throws:
java.lang.NullPointerException
- if executor or result is null
-
make
static <T> Promise<T> make()
Makes aPromise
that isn't fulfilled yet, backed by theFuture.DEFAULT_EXECUTOR
.ForkJoinPool.commonPool()
.- Type Parameters:
T
- Result type of thePromise
.- Returns:
- A new
Promise
.
-
make
static <T> Promise<T> make(java.util.concurrent.Executor executor)
Makes aPromise
that isn't fulfilled yet, backed by the givenExecutor
.- Type Parameters:
T
- Result type of thePromise
.- Parameters:
executor
- AnExecutor
passed to the underlyingFuture
.- Returns:
- A new
Promise
. - Throws:
java.lang.NullPointerException
- if executor is null
-
narrow
static <T> Promise<T> narrow(Promise<? extends T> promise)
Narrows a widenedPromise<? extends T>
toPromise<T>
by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T
- Component type of thePromise
.- Parameters:
promise
- APromise
.- Returns:
- the given
promise
instance as narrowed typePromise<T>
.
-
successful
static <T> Promise<T> successful(T result)
Creates a succeededPromise
, backed by theFuture.DEFAULT_EXECUTOR
.- Type Parameters:
T
- The value type of a successful result.- Parameters:
result
- The result.- Returns:
- A succeeded
Promise
.
-
successful
static <T> Promise<T> successful(java.util.concurrent.Executor executor, T result)
Creates a succeededPromise
, backed by the givenExecutor
.- Type Parameters:
T
- The value type of a successful result.- Parameters:
executor
- AnExecutor
passed to the underlyingFuture
.result
- The result.- Returns:
- A succeeded
Promise
. - Throws:
java.lang.NullPointerException
- if executor is null
-
executor
default java.util.concurrent.Executor executor()
- Returns:
- The underlying
Executor
.
-
executorService
@Deprecated java.util.concurrent.ExecutorService executorService()
Deprecated.Removed starting with Vavr 0.10.0, useexecutor()
instead.This method is deprecated.THE DEFAULT IMPLEMENTATION (obtained by one of the
Promise
factory methods) MIGHT THROW ANUnsupportedOperationException
AT RUNTIME, DEPENDING ON WHATFuture.executorService()
returns.- Returns:
- (never)
- Throws:
java.lang.UnsupportedOperationException
- if the underlyingExecutor
isn't anExecutorService
.
-
isCompleted
default boolean isCompleted()
Checks if thisPromise
is completed, i.e. has a value.- Returns:
- true, if the computation successfully finished or failed, false otherwise.
-
complete
default Promise<T> complete(Try<? extends T> value)
Completes thisPromise
with the givenvalue
.- Parameters:
value
- Either aTry.Success
containing the result or aTry.Failure
containing an exception.- Returns:
- This
Promise
. - Throws:
java.lang.IllegalStateException
- if thisPromise
has already been completed.
-
tryComplete
boolean tryComplete(Try<? extends T> value)
Attempts to completes thisPromise
with the givenvalue
.- Parameters:
value
- Either aTry.Success
containing the result or aTry.Failure
containing an exception.- Returns:
false
if thisPromise
has already been completed,true
otherwise.- Throws:
java.lang.IllegalStateException
- if thisPromise
has already been completed.
-
completeWith
default Promise<T> completeWith(Future<? extends T> other)
Completes thisPromise
with the givenFuture
, once thatFuture
is completed.- Parameters:
other
- AnotherFuture
to react on.- Returns:
- This
Promise
.
-
tryCompleteWith
default Promise<T> tryCompleteWith(Future<? extends T> other)
Attempts to complete thisPromise
with the specifiedFuture
, once thatFuture
is completed.- Parameters:
other
- AnotherFuture
to react on.- Returns:
- This
Promise
.
-
success
default Promise<T> success(T value)
Completes thisPromise
with the givenvalue
.- Parameters:
value
- A value.- Returns:
- This
Promise
. - Throws:
java.lang.IllegalStateException
- if thisPromise
has already been completed.
-
trySuccess
default boolean trySuccess(T value)
Completes thisPromise
with the givenvalue
.- Parameters:
value
- A value.- Returns:
false
if thisPromise
has already been completed,true
otherwise.
-
failure
default Promise<T> failure(java.lang.Throwable exception)
Completes thisPromise
with the givenexception
.- Parameters:
exception
- An exception.- Returns:
- This
Promise
. - Throws:
java.lang.IllegalStateException
- if thisPromise
has already been completed.
-
tryFailure
default boolean tryFailure(java.lang.Throwable exception)
Completes thisPromise
with the givenexception
.- Parameters:
exception
- An exception.- Returns:
false
if thisPromise
has already been completed,true
otherwise.
-
-