Class AbstractPromise<D>
java.lang.Object
cn.dinodev.spring.commons.promise.AbstractPromise<D>
- Type Parameters:
D- Promise解决时的数据类型
- Direct Known Subclasses:
DeferredObject
Promise的抽象实现类,提供Promise模式的基础功能
- Since:
- 2022-03-14
- Author:
- Cody Lu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static enumPromise回调函数的类型Nested classes/interfaces inherited from interface cn.dinodev.spring.commons.promise.Promise
Promise.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<AlwaysCallback<? super D>> protected final Conditionprotected final ReentrantLockprotected Promise.Stateprotected Throwableprotected D -
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 (),invalid reference
Deferred#reject(Object)AlwaysCallbackwill be triggered.This method will registerso that when a Deferred object is resolved (invalid reference
DoneCallbackDeferred.resolve(Object)),will be triggered.invalid reference
DoneCallbackThis method will registerso that when a Deferred object is rejected (invalid reference
FailCallback),invalid reference
Deferred#reject(Object)will be triggered.invalid reference
FailCallbackget()get the resolved value, return defaultValue for rejectedget the resolved value, return defaultSupplier‘s result for rejectedprotected voidhandleException(AbstractPromise.CallbackType callbackType, Exception exception) 处理回调函数执行时的异常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
-
promiseState
-
lock
-
condition
-
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 registerso that when a Deferred object is resolved (invalid reference
DoneCallbackDeferred.resolve(Object)),will be triggered. If the Deferred object is already resolved then theinvalid reference
DoneCallbackis triggered immediately. You can register multipleinvalid reference
DoneCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.invalid reference
DoneCallbackpromise.progress(new DoneCallback(){ public void onDone(Object done) { ... } }); -
fail
Description copied from interface:PromiseThis method will registerso that when a Deferred object is rejected (invalid reference
FailCallback),invalid reference
Deferred#reject(Object)will be triggered. If the Deferred object is already rejected then theinvalid reference
FailCallbackis triggered immediately. You can register multipleinvalid reference
FailCallbackby calling the method multiple times. The order of callback trigger is based on the order they have been registered.invalid reference
FailCallbackpromise.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 (),invalid reference
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 anyorinvalid reference
DoneCallbackrespectively.invalid reference
FailCallbackpromise.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 } } }); -
triggerDone
触发已完成回调方法- Parameters:
resolved- 解决的数据
-
triggerDone
触发单个已完成回调方法- Parameters:
callback- 回调函数resolved- 解决的数据
-
triggerFail
触发失败回调方法- Parameters:
rejected- 拒绝的异常
-
triggerFail
触发单个失败回调方法- Parameters:
callback- 回调函数rejected- 拒绝的异常
-
triggerAlways
触发总是执行的回调方法- Parameters:
state- Promise状态resolve- 解决的数据reject- 拒绝的异常
-
triggerAlways
protected void triggerAlways(AlwaysCallback<? super D> callback, Promise.State state, D resolve, Throwable reject) 触发单个总是执行的回调方法- Parameters:
callback- 回调函数state- Promise状态resolve- 解决的数据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
处理回调函数执行时的异常- Parameters:
callbackType- 回调类型exception- 发生的异常
-