T - The result type of the underlying Futurepublic interface Promise<T>
The underlying Executor is used to execute asynchronous handlers, e.g. via
promise.future().onComplete(...).
Promise offers static factory methods to create new promises which hasn't been fulfilled yet:
make()Executor as
argument. This gives us more control over thread creation and thread pool sizes.
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.
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.
| Modifier and Type | Method and Description |
|---|---|
default Promise<T> |
complete(Try<? extends T> value)
Completes this
Promise with the given value. |
default Promise<T> |
completeWith(Future<? extends T> other)
Completes this
Promise with the given Future, once that Future is completed. |
default java.util.concurrent.Executor |
executor()
|
java.util.concurrent.ExecutorService |
executorService()
Deprecated.
Removed starting with Vavr 0.10.0, use
executor() instead. |
static <T> Promise<T> |
failed(java.util.concurrent.Executor executor,
java.lang.Throwable exception)
Creates a failed
Promise, backed by the given Executor. |
static <T> Promise<T> |
failed(java.lang.Throwable exception)
Creates a failed
Promise, backed by the Future.DEFAULT_EXECUTOR. |
default Promise<T> |
failure(java.lang.Throwable exception)
Completes this
Promise with the given exception. |
static <T> Promise<T> |
fromTry(java.util.concurrent.Executor executor,
Try<? extends T> result)
|
static <T> Promise<T> |
fromTry(Try<? extends T> result)
|
Future<T> |
future()
Returns the underlying
Future of this Promise. |
default boolean |
isCompleted()
Checks if this
Promise is completed, i.e. |
static <T> Promise<T> |
make()
Makes a
Promise that isn't fulfilled yet, backed by the Future.DEFAULT_EXECUTOR. |
static <T> Promise<T> |
make(java.util.concurrent.Executor executor)
Makes a
Promise that isn't fulfilled yet, backed by the given Executor. |
static <T> Promise<T> |
narrow(Promise<? extends T> promise)
Narrows a widened
Promise<? extends T> to Promise<T>
by performing a type-safe cast. |
default Promise<T> |
success(T value)
Completes this
Promise with the given value. |
static <T> Promise<T> |
successful(java.util.concurrent.Executor executor,
T result)
Creates a succeeded
Promise, backed by the given Executor. |
static <T> Promise<T> |
successful(T result)
Creates a succeeded
Promise, backed by the Future.DEFAULT_EXECUTOR. |
boolean |
tryComplete(Try<? extends T> value)
Attempts to completes this
Promise with the given value. |
default Promise<T> |
tryCompleteWith(Future<? extends T> other)
Attempts to complete this
Promise with the specified Future, once that Future is completed. |
default boolean |
tryFailure(java.lang.Throwable exception)
Completes this
Promise with the given exception. |
default boolean |
trySuccess(T value)
Completes this
Promise with the given value. |
static <T> Promise<T> failed(java.lang.Throwable exception)
Promise, backed by the Future.DEFAULT_EXECUTOR.T - The value type of a successful result.exception - The reason why it failed.Promise.java.lang.NullPointerException - if exception is nullstatic <T> Promise<T> failed(java.util.concurrent.Executor executor, java.lang.Throwable exception)
Promise, backed by the given Executor.T - The value type of a successful result.executor - An Executor passed to the underlying Future.exception - The reason why it failed.Promise.java.lang.NullPointerException - if executor or exception is nullstatic <T> Promise<T> fromTry(Try<? extends T> result)
T - The value type of a successful result.result - The result.Promise which contains either a Success or a Failure.java.lang.NullPointerException - if result is nullstatic <T> Promise<T> fromTry(java.util.concurrent.Executor executor, Try<? extends T> result)
T - The value type of a successful result.executor - An Executor passed to the underlying Future.result - The result.Promise which contains either a Success or a Failure.java.lang.NullPointerException - if executor or result is nullstatic <T> Promise<T> make()
Promise that isn't fulfilled yet, backed by the Future.DEFAULT_EXECUTOR.
ForkJoinPool.commonPool().T - Result type of the Promise.Promise.static <T> Promise<T> make(java.util.concurrent.Executor executor)
Promise that isn't fulfilled yet, backed by the given Executor.T - Result type of the Promise.executor - An Executor passed to the underlying Future.Promise.java.lang.NullPointerException - if executor is nullstatic <T> Promise<T> narrow(Promise<? extends T> promise)
Promise<? extends T> to Promise<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.T - Component type of the Promise.promise - A Promise.promise instance as narrowed type Promise<T>.static <T> Promise<T> successful(T result)
Promise, backed by the Future.DEFAULT_EXECUTOR.T - The value type of a successful result.result - The result.Promise.static <T> Promise<T> successful(java.util.concurrent.Executor executor, T result)
Promise, backed by the given Executor.T - The value type of a successful result.executor - An Executor passed to the underlying Future.result - The result.Promise.java.lang.NullPointerException - if executor is nulldefault java.util.concurrent.Executor executor()
Executor.@Deprecated java.util.concurrent.ExecutorService executorService()
executor() instead.
THE DEFAULT IMPLEMENTATION (obtained by one of the Promise factory methods) MIGHT THROW AN
UnsupportedOperationException AT RUNTIME, DEPENDING ON WHAT Future.executorService()
returns.
java.lang.UnsupportedOperationException - if the underlying Executor isn't an ExecutorService.default boolean isCompleted()
Promise is completed, i.e. has a value.default Promise<T> complete(Try<? extends T> value)
Promise with the given value.value - Either a Try.Success containing the result or a Try.Failure containing an exception.Promise.java.lang.IllegalStateException - if this Promise has already been completed.boolean tryComplete(Try<? extends T> value)
Promise with the given value.value - Either a Try.Success containing the result or a Try.Failure containing an exception.false if this Promise has already been completed, true otherwise.java.lang.IllegalStateException - if this Promise has already been completed.default Promise<T> completeWith(Future<? extends T> other)
Promise with the given Future, once that Future is completed.other - Another Future to react on.Promise.default Promise<T> tryCompleteWith(Future<? extends T> other)
Promise with the specified Future, once that Future is completed.other - Another Future to react on.Promise.default Promise<T> success(T value)
Promise with the given value.value - A value.Promise.java.lang.IllegalStateException - if this Promise has already been completed.default boolean trySuccess(T value)
Promise with the given value.value - A value.false if this Promise has already been completed, true otherwise.default Promise<T> failure(java.lang.Throwable exception)
Promise with the given exception.exception - An exception.Promise.java.lang.IllegalStateException - if this Promise has already been completed.default boolean tryFailure(java.lang.Throwable exception)
Promise with the given exception.exception - An exception.false if this Promise has already been completed, true otherwise.