public interface Promise<P>
Future
, represents the result
of an asynchronous computation. However, Promises are designed to work in
an entirely asynchronous work flow. Except where specifically
mentioned, none of the methods in the Promise will block the current thread
while waiting for a response.Modifier and Type | Method and Description |
---|---|
void |
addListener(PromiseListener<P> listener)
Adds a listener to this promise that will be notified when the promise is
resolved.
|
void |
await()
Blocks the current thread for an unbounded amount of time until the promise
has be resolved.
|
boolean |
await(long time,
TimeUnit unit)
Blocks the current thread for up to the specified amount of time or until
the promise has been resolved.
|
P |
get()
If the promise's value is set, then this method returns the value.
|
Throwable |
getError()
Returns the error in this promise, if there is one.
|
P |
getOrDefault(P defaultValue)
Gets the value in this promise or, if the promise contains an error,
returns the given default value.
|
boolean |
isDone()
Returns
true if this promise is resolved. |
boolean |
isFailed()
Returns
true if the promise has an error. |
default CompletionStage<P> |
toCompletionStage()
Converts {@code Promise
|
P get() throws PromiseException
PromiseException
. If the promise has not yet been resolved (i.e.
it has no value and no error) then this method raises a
PromiseUnresolvedException
.PromiseException
- if the promise has an errorPromiseUnresolvedException
- if the promise has not yet been resolvedThrowable getError() throws PromiseUnresolvedException
null
. If this
promise has no value and no error (i.e. it is unresolved) then a
PromiseUnresolvedException
is thrown.null
if the
promise contains a value.PromiseUnresolvedException
- if the promise has not yet been resolvedP getOrDefault(P defaultValue) throws PromiseUnresolvedException
If the promise has not been resolved then this method will throw
PromiseUnresolvedException
.
defaultValue
- the default value to return if this promise contains an
error.PromiseUnresolvedException
- if the promise has not yet been resolvedvoid await() throws InterruptedException
For asynchronous workflows, use addListener(PromiseListener)
,
which will notify the user when the promise is resolved instead of blocking
the current thread.
InterruptedException
- if the current thread is interruptedboolean await(long time, TimeUnit unit) throws InterruptedException
true
.
For asynchronous workflows, use addListener(PromiseListener)
,
which will notify the user when the promise is resolved instead of blocking
the current thread.
time
- the amount of time to waitunit
- the units for the wait timetrue
if the promise is resolved within the specified timeInterruptedException
- if the current thread is interruptedvoid addListener(PromiseListener<P> listener)
Listener will be called on a thread that completed this promise.
The instance passed in to PromiseListener.onResolved(Promise)
method does not have to
be the exact same instance the PromiseListener
was registered with.
listener
- the listener to add to this promiseboolean isDone()
true
if this promise is resolved. A promise is resolved
when it has a value or an error.true
if this promise has been resolved.boolean isFailed()
true
if the promise has an error. Errors can retrieved
using getError()
.true
if the promise has en error.default CompletionStage<P> toCompletionStage()
Promise<P>
into CompletionStage<P>
.Copyright © 2018. All rights reserved.