Class AbstractPromise<D>
java.lang.Object
cn.dinodev.spring.commons.promise.AbstractPromise<D>
- Direct Known Subclasses:
DeferredObject
- Author:
- Cody Lu
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface cn.dinodev.spring.commons.promise.Promise
Promise.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<AlwaysCallback<? super D>>protected Throwableprotected Dprotected Promise.State -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionalways(AlwaysCallback<? super D> callback) This method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred#reject(Object)),AlwaysCallbackwill be triggered.This method will registerDoneCallbackso that when a Deferred object is resolved (Deferred.resolve(Object)),DoneCallbackwill be triggered.This method will registerFailCallbackso that when a Deferred object is rejected (Deferred#reject(Object)),FailCallbackwill be triggered.get()get the resolved value, return defaultValue for rejectedget the resolved value, return defaultSupplier‘s result for rejectedprotected voidhandleException(AbstractPromise.CallbackType callbackType, Exception e) booleanQueries the state of this promise, returningtrueiff it isState.PENDING.booleanQueries the state of this promise, returningtrueiff it isState.REJECTED.booleanQueries the state of this promise, returningtrueiff it isState.RESOLVED.state()状态Equivalent toPromise.done(DoneCallback)Equivalent toPromise.done(DoneCallback).Promise.fail(FailCallback)protected voidtriggerAlways(AlwaysCallback<? super D> callback, Promise.State state, D resolve, Throwable reject) protected voidtriggerAlways(Promise.State state, D resolve, Throwable reject) protected voidtriggerDone(D resolved) protected voidtriggerDone(Consumer<? super D> callback, D resolved) protected voidtriggerFail(Throwable rejected) protected voidtriggerFail(Consumer<Throwable> callback, Throwable rejected) voidThis method will wait as long as the State is Pending.voidwaitSafely(long timeout) This method will wait when the State is Pending, and return when timeout has reached.
-
Field Details
-
state
-
doneCallbacks
-
failCallbacks
-
alwaysCallbacks
-
resolveResult
-
rejectResult
-
-
Constructor Details
-
AbstractPromise
public AbstractPromise()
-
-
Method Details
-
state
Description copied from interface:Promise状态 -
done
Description copied from interface:PromiseThis method will registerDoneCallbackso that when a Deferred object is resolved (Deferred.resolve(Object)),DoneCallbackwill be triggered. If the Deferred object is already resolved then theDoneCallbackis triggered immediately. You can register multipleDoneCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.progress(new DoneCallback(){ public void onDone(Object done) { ... } }); -
fail
Description copied from interface:PromiseThis method will registerFailCallbackso that when a Deferred object is rejected (Deferred#reject(Object)),FailCallbackwill be triggered. If the Deferred object is already rejected then theFailCallbackis triggered immediately. You can register multipleFailCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.fail(new FailCallback(){ public void onFail(Object rejection) { ... } }); -
always
Description copied from interface:PromiseThis method will registerAlwaysCallbackso that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (Deferred#reject(Object)),AlwaysCallbackwill be triggered. If the Deferred object is already resolved or rejected then theAlwaysCallbackis triggered immediately. You can register multipleAlwaysCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.AlwaysCallbacks are triggered after anyDoneCallbackorFailCallbackrespectively.promise.always(new AlwaysCallback(){ public void onAlways(State state, Object result, Object rejection) { if (state == State.RESOLVED) { // do something with result } else { // do something with rejection } } });- Specified by:
alwaysin interfacePromise<D>- Parameters:
callback- the callback to be triggered- Returns:
thisfor chaining more calls- See Also:
-
Deferred.resolve(Object)Deferred#reject(Object)
-
triggerDone
-
triggerDone
-
triggerFail
-
triggerFail
-
triggerAlways
-
triggerAlways
protected void triggerAlways(AlwaysCallback<? super D> callback, Promise.State state, D resolve, Throwable reject) -
then
Description copied from interface:PromiseEquivalent toPromise.done(DoneCallback)- Specified by:
thenin interfacePromise<D>- Parameters:
callback- seePromise.done(DoneCallback)- Returns:
thisfor chaining more calls
-
then
Description copied from interface:PromiseEquivalent toPromise.done(DoneCallback).Promise.fail(FailCallback)- Specified by:
thenin interfacePromise<D>- Parameters:
doneCallback- seePromise.done(DoneCallback)failCallback- seePromise.fail(FailCallback)- Returns:
thisfor chaining more calls
-
isPending
public boolean isPending()Description copied from interface:PromiseQueries the state of this promise, returningtrueiff it isState.PENDING. -
isResolved
public boolean isResolved()Description copied from interface:PromiseQueries the state of this promise, returningtrueiff it isState.RESOLVED.- Specified by:
isResolvedin interfacePromise<D>- Returns:
trueif the current state of this promise isState.RESOLVED,falseotherwise.- See Also:
-
isRejected
public boolean isRejected()Description copied from interface:PromiseQueries the state of this promise, returningtrueiff it isState.REJECTED.- Specified by:
isRejectedin interfacePromise<D>- Returns:
trueif the current state of this promise isState.REJECTED,falseotherwise.- See Also:
-
waitSafely
Description copied from interface:PromiseThis method will wait as long as the State is Pending. This method will return fast when State is not Pending.- Specified by:
waitSafelyin interfacePromise<D>- Throws:
InterruptedException- if thread is interrupted while waiting
-
waitSafely
Description copied from interface:PromiseThis method will wait when the State is Pending, and return when timeout has reached. This method will return fast when State is not Pending.- Specified by:
waitSafelyin interfacePromise<D>- Parameters:
timeout- the maximum time to wait in milliseconds- Throws:
InterruptedException- if thread is interrupted while waiting
-
get
-
getOrElse
Description copied from interface:Promiseget the resolved value, return defaultValue for rejected -
getOrElse
Description copied from interface:Promiseget the resolved value, return defaultSupplier‘s result for rejected -
handleException
-