T
- The type this Promise will produce as resultpublic interface Promise<T>
success
/failure
be invoked twice. Promise
will deliver exactly one successful or failure response. Throwable
. Future
this allows a safe publication of asynchronously calculated results into another thread. Promise
. Promise<String> promise = Promise.apply();Using that instance one can get hold of the
Future
that is the container
for the value-to-be. Future<String> future = promise.future();To complete the Promise and by extension completing the Future one can use any of several methods: E.g.
promise.success("Peter was here");Note that only one of the methods may be invoked as the Promise can only be fulfilled once.
Modifier and Type | Method and Description |
---|---|
static <T> Promise<T> |
apply()
Creates an instance of Promise.
|
void |
complete(Try<T> result)
|
void |
completeWith(Future<T> future)
|
void |
failure(Throwable throwable)
Completes the
Promise with an exception. |
Future<T> |
future()
Get a
Future that will hold the value once this Promise is completed. |
boolean |
isCompleted()
Check if the
Promise have been completed, with a value or an exception. |
void |
success(T result)
Completes the
Promise with a value. |
boolean |
tryComplete(Try<T> result)
|
boolean |
tryCompleteWith(Future<T> future)
|
boolean |
tryFailure(Throwable throwable)
Tries to complete the
Promise with an exception. |
boolean |
trySuccess(T result)
Tries to complete the
Promise with a value. |
static <T> Promise<T> apply()
Promise<String> p = Promise.apply();
T
- The type this Promise will produce as resultFuture<T> future()
Future
that will hold the value once this Promise is completed. Promise
is connected to a single Future
, invoking this method multiple times will always return the same Future
instance.boolean isCompleted()
Promise
have been completed, with a value or an exception.true
if the Promise
has been completed. false
otherwise.void complete(Try<T> result)
result
- The result to complete with.IllegalStateException
- Thrown if the Promise is already completed.void completeWith(Future<T> future)
future
- The future whose value will complete this PromiseIllegalStateException
- Thrown if the Promise is already completed.void success(T result)
Promise
with a value.result
- The value to complete with.IllegalStateException
- Thrown if the Promise is already completed.void failure(Throwable throwable)
Promise
with an exception.throwable
- The Throwable to complete with.IllegalStateException
- Thrown if the Promise is already completed.boolean tryComplete(Try<T> result)
Promise
with either a Success
or a Failure
. complete(Try)
method this does not throw an exception in case the Promise is already completed.result
- The result to complete with.true
if the Promise was not completed before, false
otherwiseboolean tryCompleteWith(Future<T> future)
Promise
with the value from the provided Future
once that is completed. complete(Try)
method this does not throw an exception in case the Promise is already completed.future
- The future whose value will complete this Promisetrue
if the Promise was not completed before, false
otherwiseboolean trySuccess(T result)
Promise
with a value. success(Object)
method this does not throw an exception in case the Promise is already completed.result
- The value to complete with.true
if the Promise was not completed before, false
otherwiseboolean tryFailure(Throwable throwable)
Promise
with an exception. failure(Throwable)
method this does not throw an exception in case the Promise is already completed.throwable
- The Throwable to complete with.true
if the Promise was not completed before, false
otherwiseCopyright © 2016, Peter Nerg Apache License v2.0