public interface Promise<T,F extends Throwable> extends OnResolve<T>, OnReject<F>, OnCancel, Future<T>
A representation of a computation allowing listeners to be notified of state changes. This representation allows
for four main states: PENDING, RESOLVED, REJECTED and CANCELLED. Of these
RESOLVED, REJECTED and CANCELLED will be referred to as 'terminal states', that is a
state when the isDone() method will return true. A promise reaches one of these states by a call
to a 'terminal method', one of resolve(Object), reject(Throwable) and cancel(boolean)
respectively.
The javadoc here only considers these three terminal states however this definition does not preclude inheritors from adding further terminal states and associated methods.
| Modifier and Type | Field and Description |
|---|---|
static byte |
CANCELLED |
static byte |
PENDING |
static byte |
REJECTED |
static byte |
RESOLVED |
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean interrupt)
Called to attempt to stop the computation.
|
boolean |
isCancelled() |
boolean |
isDone() |
boolean |
isRejected() |
boolean |
isResolved() |
Promise<T,F> |
onCancel(OnCancel then)
Triggered on any event after which
isCancelled() will return true. |
Promise<T,F> |
onComplete(OnComplete then)
Triggered on any event after which
isDone() will return true;
Will be fired in addition to the callback for the specific event. |
Promise<T,F> |
onGet(Future<?> then)
Triggered when
Future.get(long, TimeUnit) or Future.get() is called. |
Promise<T,F> |
onReject(OnReject<F> then)
Triggered on any event after which
isRejected() will return true. |
Promise<T,F> |
onResolve(OnResolve<T> then)
Triggered on any event after which
isResolved() will return true. |
void |
reject(F that)
Called to indicate the failure of the computation this promise represents.
|
void |
resolve(T that)
Called to indicate the successful completion of the computation this promise represents.
|
static final byte PENDING
static final byte RESOLVED
static final byte REJECTED
static final byte CANCELLED
void resolve(T that) throws ListenerException, ResolvedException, RejectedException, CancelledException
isDone() will return true. If this was the first terminal method to be called
isResolved() will also return true.resolve in interface OnResolve<T>that - The result of the computation.ListenerException - MAY be thrown if a listener throws an exception.ResolvedException - MAY be thrown by an implementation if resolve has previously called.RejectedException - MAY be thrown by an implementation if reject(Throwable) has previously called.CancelledException - MAY be thrown by an implementation if cancel(boolean) has previously called.void reject(F that) throws ListenerException, ResolvedException, RejectedException, CancelledException
isDone() will return true. If this was the first terminal method to be called
isRejected() will also return true.reject in interface OnReject<F extends Throwable>that - The exception that caused the computation to terminate.ListenerException - MAY be thrown if a listener throws an exception.ResolvedException - MAY be thrown by an implementation if resolve(Object) has previously called.RejectedException - MAY be thrown by an implementation if reject has previously called.CancelledException - MAY be thrown by an implementation if cancel(boolean) has previously called.boolean cancel(boolean interrupt)
throws ListenerException
isDone() will return true. If this was the first terminal method to be called
isCancelled() will also return true.cancel in interface Future<T>cancel in interface OnCancelinterrupt - true If the computation should be interrupted in the case that it has already commenced.true If the promise was cancelled, false if it had already reached another terminal state.ListenerException - MAY be thrown if a listener throws an exception.Future.cancel(boolean)boolean isDone()
boolean isResolved()
true if resolve(Object) was the first terminal method called.boolean isRejected()
true if reject(Throwable) was the first terminal method called.boolean isCancelled()
isCancelled in interface Future<T>true if cancel(boolean) was the first terminal method called.Promise<T,F> onResolve(OnResolve<T> then)
isResolved() will return true.then - Callback to be executedPromise<T,F> onReject(OnReject<F> then)
isRejected() will return true.then - Callback to be executedPromise<T,F> onCancel(OnCancel then)
isCancelled() will return true.then - Callback to be executedPromise<T,F> onComplete(OnComplete then)
isDone() will return true;
Will be fired in addition to the callback for the specific event.then - Callback to be executedPromise<T,F> onGet(Future<?> then)
Future.get(long, TimeUnit) or Future.get() is called.
It will be called after this promise has transitioned into a state
where isDone() will return true.
Each get method will call the corresponding get method on the Future
in the thread that called either Future.get() or Future.get(long, TimeUnit).then - Callback to be executedCopyright © 2014 MachineCode. All rights reserved.