T
- The result type of the underlying Future
.public interface Promise<T>
The underlying ExecutorService
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()
ExecutorService
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 Thread
s 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. |
ExecutorService |
executorService()
Returns the
ExecutorService used by this Future . |
static <T> Promise<T> |
failed(ExecutorService executorService,
Throwable exception)
Creates a failed
Promise , backed by the given ExecutorService . |
static <T> Promise<T> |
failed(Throwable exception)
Creates a failed
Promise , backed by the Future.DEFAULT_EXECUTOR_SERVICE . |
default Promise<T> |
failure(Throwable exception)
Completes this
Promise with the given exception . |
static <T> Promise<T> |
fromTry(ExecutorService executorService,
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_SERVICE . |
static <T> Promise<T> |
make(ExecutorService executorService)
Makes a
Promise that isn't fulfilled yet, backed by the given ExecutorService . |
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(ExecutorService executorService,
T result)
Creates a succeeded
Promise , backed by the given ExecutorService . |
static <T> Promise<T> |
successful(T result)
Creates a succeeded
Promise , backed by the Future.DEFAULT_EXECUTOR_SERVICE . |
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(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(Throwable exception)
Promise
, backed by the Future.DEFAULT_EXECUTOR_SERVICE
.T
- The value type of a successful result.exception
- The reason why it failed.Promise
.NullPointerException
- if exception is nullstatic <T> Promise<T> failed(ExecutorService executorService, Throwable exception)
Promise
, backed by the given ExecutorService
.T
- The value type of a successful result.executorService
- An ExecutorService
passed to the underlying Future
.exception
- The reason why it failed.Promise
.NullPointerException
- if executorService 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
.NullPointerException
- if result is nullstatic <T> Promise<T> fromTry(ExecutorService executorService, Try<? extends T> result)
T
- The value type of a successful result.executorService
- An ExecutorService
passed to the underlying Future
.result
- The result.Promise
which contains either a Success
or a Failure
.NullPointerException
- if executorService or result is nullstatic <T> Promise<T> make()
Promise
that isn't fulfilled yet, backed by the Future.DEFAULT_EXECUTOR_SERVICE
.
ForkJoinPool.commonPool()
.T
- Result type of the Promise
.Promise
.static <T> Promise<T> make(ExecutorService executorService)
Promise
that isn't fulfilled yet, backed by the given ExecutorService
.T
- Result type of the Promise
.executorService
- An ExecutorService
passed to the underlying Future
.Promise
.NullPointerException
- if executorService 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_SERVICE
.T
- The value type of a successful result.result
- The result.Promise
.static <T> Promise<T> successful(ExecutorService executorService, T result)
Promise
, backed by the given ExecutorService
.T
- The value type of a successful result.executorService
- An ExecutorService
passed to the underlying Future
.result
- The result.Promise
.NullPointerException
- if executorService is nullExecutorService executorService()
ExecutorService
used by this Future
.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
.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.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
.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(Throwable exception)
Promise
with the given exception
.exception
- An exception.Promise
.IllegalStateException
- if this Promise
has already been completed.default boolean tryFailure(Throwable exception)
Promise
with the given exception
.exception
- An exception.false
if this Promise
has already been completed, true
otherwise.Copyright © 2017. All Rights Reserved.