Interface Promise<T>
- Type Parameters:
T- the type of the value that completes the underlyingFuture
- All Known Implementing Classes:
PromiseImpl
public interface Promise<T>
A
Promise is a write-once container for a read-only Future, allowing the underlying Future
to be completed with a value or an exception.
The associated Executor is used to run asynchronous handlers, for example, via
promise.future().onComplete(...).
Creation
Promise provides static factory methods to create new promises:
- Unfulfilled promises:
make() - Already completed promises:
Executor, allowing finer control over
thread usage and thread pool configuration.
One-shot API
When a single Thread is responsible for completing the Promise, use one of the following methods.
Calls will throw an exception if the Promise has already been completed:
API for concurrent completion
When multiple Threads may attempt to complete the Promise, use one of the following "try" methods.
Calls will return false if the Promise is already completed:
-
Method Summary
Modifier and TypeMethodDescriptionCompletes thisPromisewith the givenvalue.completeWith(@NonNull Future<? extends T> other) Completes thisPromisewith the result of the givenFutureonce it is completed.default Executorexecutor()Deprecated.static <T> Promise<T> Creates aPromisethat is already completed with a failure, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.static <T> Promise<T> Creates aPromisethat is already completed with a failure, using the specifiedExecutorfor asynchronous operations.Completes thisPromisewith the given exception.static <T> Promise<T> Creates aPromisefrom the givenTry, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.static <T> Promise<T> future()Returns the underlyingFutureassociated with thisPromise.default booleanChecks whether thisPromisehas been completed, either successfully or with a failure.static <T> Promise<T> make()Creates a newPromisethat is not yet completed, using theFuture.DEFAULT_EXECUTOR(typicallyForkJoinPool.commonPool()) for asynchronous operations.static <T> Promise<T> Creates a newPromisethat is not yet completed, using the specifiedExecutorfor asynchronous operations.static <T> Promise<T> Narrows aPromise<? extends T>toPromise<T>through a type-safe cast.Completes thisPromisewith the given value.static <T> Promise<T> successful(@NonNull Executor executor, T result) Creates aPromisethat is already completed successfully, using the specifiedExecutorfor asynchronous operations.static <T> Promise<T> successful(T result) Creates aPromisethat is already completed successfully, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.booleantryComplete(@NonNull Try<? extends T> value) Attempts to complete thisPromisewith the givenvalue.tryCompleteWith(@NonNull Future<? extends T> other) Attempts to complete thisPromisewith the result of the givenFutureonce it is completed.default booleantryFailure(@NonNull Throwable exception) Attempts to complete thisPromisewith the given exception.default booleantrySuccess(T value) Attempts to complete thisPromisewith the given value.
-
Method Details
-
failed
Creates aPromisethat is already completed with a failure, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.- Type Parameters:
T- the type of the value that would have been returned on success- Parameters:
exception- the cause of the failure- Returns:
- a
Promisecompleted with the given failure - Throws:
NullPointerException- ifexceptionis null
-
failed
Creates aPromisethat is already completed with a failure, using the specifiedExecutorfor asynchronous operations.- Type Parameters:
T- the type of the value that would have been returned on success- Parameters:
executor- theExecutorused by the underlyingFutureexception- the cause of the failure- Returns:
- a
Promisecompleted with the given failure - Throws:
NullPointerException- ifexecutororexceptionis null
-
fromTry
Creates aPromisefrom the givenTry, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.- Type Parameters:
T- the type of the value in case of success- Parameters:
result- theTryrepresenting a success or failure- Returns:
- a
Promisealready completed with the givenTryresult - Throws:
NullPointerException- ifresultis null
-
fromTry
- Type Parameters:
T- the type of the value in case of success- Parameters:
executor- theExecutorused by the underlyingFutureresult- theTryrepresenting a success or failure- Returns:
- a
Promisealready completed with the givenTryresult - Throws:
NullPointerException- ifexecutororresultis null
-
make
Creates a newPromisethat is not yet completed, using theFuture.DEFAULT_EXECUTOR(typicallyForkJoinPool.commonPool()) for asynchronous operations.- Type Parameters:
T- the type of the value that will complete thePromise- Returns:
- a new, uncompleted
Promise
-
make
Creates a newPromisethat is not yet completed, using the specifiedExecutorfor asynchronous operations.- Type Parameters:
T- the type of the value that will complete thePromise- Parameters:
executor- theExecutorused by the underlyingFuture- Returns:
- a new, uncompleted
Promise - Throws:
NullPointerException- ifexecutoris null
-
narrow
Narrows aPromise<? extends T>toPromise<T>through a type-safe cast. This is safe because immutable or read-only collections are covariant.- Type Parameters:
T- the component type of thePromise- Parameters:
promise- thePromiseto narrow- Returns:
- the same
promiseinstance, cast toPromise<T>
-
successful
Creates aPromisethat is already completed successfully, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.- Type Parameters:
T- the type of the value- Parameters:
result- the value of the successful result- Returns:
- a
Promisealready completed with the given result
-
successful
Creates aPromisethat is already completed successfully, using the specifiedExecutorfor asynchronous operations.- Type Parameters:
T- the type of the value- Parameters:
executor- theExecutorused by the underlyingFutureresult- the value of the successful result- Returns:
- a
Promisealready completed with the given result - Throws:
NullPointerException- ifexecutoris null
-
executor
-
executorService
Deprecated.Removed starting with Vavr 0.10.0, useexecutor()instead.This method is deprecated.THE DEFAULT IMPLEMENTATION (obtained by one of the
Promisefactory methods) MIGHT THROW ANUnsupportedOperationExceptionAT RUNTIME, DEPENDING ON WHATFuture.executorService()returns.- Returns:
- (never)
- Throws:
UnsupportedOperationException- if the underlyingExecutorisn't anExecutorService.
-
future
-
isCompleted
default boolean isCompleted()Checks whether thisPromisehas been completed, either successfully or with a failure.- Returns:
trueif thePromiseis completed,falseotherwise
-
complete
Completes thisPromisewith the givenvalue.- Parameters:
value- aTry.Successcontaining the result or aTry.Failurecontaining an exception- Returns:
- this
Promise - Throws:
IllegalStateException- if thisPromisehas already been completed
-
tryComplete
Attempts to complete thisPromisewith the givenvalue.- Parameters:
value- aTry.Successcontaining the result or aTry.Failurecontaining an exception- Returns:
trueif thePromisewas completed successfully,falseif it was already completed
-
completeWith
-
tryCompleteWith
Attempts to complete thisPromisewith the result of the givenFutureonce it is completed.- Parameters:
other- theFuturewhose result or failure may complete thisPromise- Returns:
trueif thisPromisewas completed byother,falseif it was already completed
-
success
Completes thisPromisewith the given value.- Parameters:
value- the value to complete thisPromisewith- Returns:
- this
Promise - Throws:
IllegalStateException- if thisPromisehas already been completed
-
trySuccess
Attempts to complete thisPromisewith the given value.- Parameters:
value- the value to complete thisPromisewith- Returns:
trueif thePromisewas completed successfully,falseif it was already completed
-
failure
Completes thisPromisewith the given exception.- Parameters:
exception- the exception to complete thisPromisewith- Returns:
- this
Promise - Throws:
IllegalStateException- if thisPromisehas already been completed
-
tryFailure
Attempts to complete thisPromisewith the given exception.- Parameters:
exception- the exception to complete thisPromisewith- Returns:
trueif thePromisewas completed successfully,falseif it was already completed
-
executor()instead.