Interface Promise<T>
- Type Parameters:
T- The result type of the underlyingFuture.
- All Known Implementing Classes:
PromiseImpl
The underlying Executor is used to execute asynchronous handlers, e.g. via
promise.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 underlying Future. When only a single Thread
will eventually complete the Promise, we use one of these methods. Calls will throw if the Promise is already
completed.
API for competing threads
When multiple Threads may complete our Promise, we typically use one of these methods. Calls will
gracefully return false if the Promise is already completed.
-
Method Summary
Modifier and TypeMethodDescriptionCompletes thisPromisewith the givenvalue.completeWith(Future<? extends T> other) Completes thisPromisewith the givenFuture, once thatFutureis completed.default Executorexecutor()Deprecated.static <T> Promise<T> Creates a failedPromise, backed by theFuture.DEFAULT_EXECUTOR.static <T> Promise<T> Creates a failedPromise, backed by the givenExecutor.Completes thisPromisewith the givenexception.static <T> Promise<T> static <T> Promise<T> future()Returns the underlyingFutureof thisPromise.default booleanChecks if thisPromiseis completed, i.e.static <T> Promise<T> make()Makes aPromisethat isn't fulfilled yet, backed by theFuture.DEFAULT_EXECUTOR.static <T> Promise<T> Makes aPromisethat isn't fulfilled yet, backed by the givenExecutor.static <T> Promise<T> Narrows a widenedPromise<? extends T>toPromise<T>by performing a type-safe cast.Completes thisPromisewith the givenvalue.static <T> Promise<T> successful(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.booleantryComplete(Try<? extends T> value) Attempts to completes thisPromisewith the givenvalue.tryCompleteWith(Future<? extends T> other) Attempts to complete thisPromisewith the specifiedFuture, once thatFutureis completed.default booleantryFailure(Throwable exception) Completes thisPromisewith the givenexception.default booleantrySuccess(T value) Completes thisPromisewith the givenvalue.
-
Method Details
-
failed
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:
NullPointerException- if exception is null
-
failed
Creates a failedPromise, backed by the givenExecutor.- Type Parameters:
T- The value type of a successful result.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.exception- The reason why it failed.- Returns:
- A failed
Promise. - Throws:
NullPointerException- if executor or exception is null
-
fromTry
- Type Parameters:
T- The value type of a successful result.- Parameters:
result- The result.- Returns:
- A completed
Promisewhich contains either aSuccessor aFailure. - Throws:
NullPointerException- if result is null
-
fromTry
- Type Parameters:
T- The value type of a successful result.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.result- The result.- Returns:
- A completed
Promisewhich contains either aSuccessor aFailure. - Throws:
NullPointerException- if executor or result is null
-
make
Makes aPromisethat isn't fulfilled yet, backed by theFuture.DEFAULT_EXECUTOR.ForkJoinPool.commonPool().- Type Parameters:
T- Result type of thePromise.- Returns:
- A new
Promise.
-
make
Makes aPromisethat isn't fulfilled yet, backed by the givenExecutor.- Type Parameters:
T- Result type of thePromise.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.- Returns:
- A new
Promise. - Throws:
NullPointerException- if executor is null
-
narrow
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
promiseinstance as narrowed typePromise<T>.
-
successful
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
Creates a succeededPromise, backed by the givenExecutor.- Type Parameters:
T- The value type of a successful result.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.result- The result.- Returns:
- A succeeded
Promise. - Throws:
NullPointerException- if executor is 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 if thisPromiseis completed, i.e. has a value.- Returns:
- true, if the computation successfully finished or failed, false otherwise.
-
complete
Completes thisPromisewith the givenvalue.- Parameters:
value- Either aTry.Successcontaining the result or aTry.Failurecontaining an exception.- Returns:
- This
Promise. - Throws:
IllegalStateException- if thisPromisehas already been completed.
-
tryComplete
Attempts to completes thisPromisewith the givenvalue.- Parameters:
value- Either aTry.Successcontaining the result or aTry.Failurecontaining an exception.- Returns:
falseif thisPromisehas already been completed,trueotherwise.- Throws:
IllegalStateException- if thisPromisehas already been completed.
-
completeWith
-
tryCompleteWith
-
success
Completes thisPromisewith the givenvalue.- Parameters:
value- A value.- Returns:
- This
Promise. - Throws:
IllegalStateException- if thisPromisehas already been completed.
-
trySuccess
Completes thisPromisewith the givenvalue.- Parameters:
value- A value.- Returns:
falseif thisPromisehas already been completed,trueotherwise.
-
failure
Completes thisPromisewith the givenexception.- Parameters:
exception- An exception.- Returns:
- This
Promise. - Throws:
IllegalStateException- if thisPromisehas already been completed.
-
tryFailure
Completes thisPromisewith the givenexception.- Parameters:
exception- An exception.- Returns:
falseif thisPromisehas already been completed,trueotherwise.
-
executor()instead.