Class AbstractPromise<D>

java.lang.Object
cn.dinodev.spring.commons.promise.AbstractPromise<D>
Type Parameters:
D - Promise解决时的数据类型
All Implemented Interfaces:
Promise<D>, Supplier<D>
Direct Known Subclasses:
DeferredObject

public abstract class AbstractPromise<D> extends Object implements Promise<D>
Promise的抽象实现类,提供Promise模式的基础功能
Since:
2022-03-14
Author:
Cody Lu
  • Field Details

  • Constructor Details

    • AbstractPromise

      public AbstractPromise()
  • Method Details

    • state

      public Promise.State state()
      Description copied from interface: Promise
      状态
      Specified by:
      state in interface Promise<D>
      Returns:
      the state of this promise.
    • done

      public Promise<D> done(Consumer<? super D> callback)
      Description copied from interface: Promise
      This method will register
      invalid reference
      DoneCallback
      so that when a Deferred object is resolved (Deferred.resolve(Object)),
      invalid reference
      DoneCallback
      will be triggered. If the Deferred object is already resolved then the
      invalid reference
      DoneCallback
      is triggered immediately. You can register multiple
      invalid reference
      DoneCallback
      by 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) {
           ...
         }
       });
       
       
      Specified by:
      done in interface Promise<D>
      Parameters:
      callback - the callback to be triggered
      Returns:
      this for chaining more calls
      See Also:
    • fail

      public Promise<D> fail(Consumer<Throwable> callback)
      Description copied from interface: Promise
      This method will register
      invalid reference
      FailCallback
      so that when a Deferred object is rejected (
      invalid reference
      Deferred#reject(Object)
      ),
      invalid reference
      FailCallback
      will be triggered. If the Deferred object is already rejected then the
      invalid reference
      FailCallback
      is triggered immediately. You can register multiple
      invalid reference
      FailCallback
      by 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) {
           ...
         }
       });
       
       
      Specified by:
      fail in interface Promise<D>
      Parameters:
      callback - the callback to be triggered
      Returns:
      this for chaining more calls
      See Also:
      • invalid reference
        Deferred#reject(Object)
    • always

      public Promise<D> always(AlwaysCallback<? super D> callback)
      Description copied from interface: Promise
      This method will register AlwaysCallback so that when a Deferred object is either resolved (Deferred.resolve(Object)) or rejected (
      invalid reference
      Deferred#reject(Object)
      ), AlwaysCallback will be triggered. If the Deferred object is already resolved or rejected then the AlwaysCallback is triggered immediately. You can register multiple AlwaysCallback by calling the method multiple times. The order of callback trigger is based on the order they have been registered. AlwaysCallbacks are triggered after any
      invalid reference
      DoneCallback
      or
      invalid reference
      FailCallback
      respectively.
       
       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:
      always in interface Promise<D>
      Parameters:
      callback - the callback to be triggered
      Returns:
      this for chaining more calls
      See Also:
    • triggerDone

      protected void triggerDone(D resolved)
      触发已完成回调方法
      Parameters:
      resolved - 解决的数据
    • triggerDone

      protected void triggerDone(Consumer<? super D> callback, D resolved)
      触发单个已完成回调方法
      Parameters:
      callback - 回调函数
      resolved - 解决的数据
    • triggerFail

      protected void triggerFail(Throwable rejected)
      触发失败回调方法
      Parameters:
      rejected - 拒绝的异常
    • triggerFail

      protected void triggerFail(Consumer<Throwable> callback, Throwable rejected)
      触发单个失败回调方法
      Parameters:
      callback - 回调函数
      rejected - 拒绝的异常
    • triggerAlways

      protected void triggerAlways(Promise.State state, D resolve, Throwable reject)
      触发总是执行的回调方法
      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

      public Promise<D> then(Consumer<? super D> callback)
      Description copied from interface: Promise
      Specified by:
      then in interface Promise<D>
      Parameters:
      callback - see Promise.done(DoneCallback)
      Returns:
      this for chaining more calls
    • then

      public Promise<D> then(Consumer<? super D> doneCallback, Consumer<Throwable> failCallback)
      Description copied from interface: Promise
      Specified by:
      then in interface Promise<D>
      Parameters:
      doneCallback - see Promise.done(DoneCallback)
      failCallback - see Promise.fail(FailCallback)
      Returns:
      this for chaining more calls
    • isPending

      public boolean isPending()
      Description copied from interface: Promise
      Queries the state of this promise, returning true iff it is State.PENDING.
      Specified by:
      isPending in interface Promise<D>
      Returns:
      true if the current state of this promise is State.PENDING, false otherwise.
      See Also:
    • isResolved

      public boolean isResolved()
      Description copied from interface: Promise
      Queries the state of this promise, returning true iff it is State.RESOLVED.
      Specified by:
      isResolved in interface Promise<D>
      Returns:
      true if the current state of this promise is State.RESOLVED, false otherwise.
      See Also:
    • isRejected

      public boolean isRejected()
      Description copied from interface: Promise
      Queries the state of this promise, returning true iff it is State.REJECTED.
      Specified by:
      isRejected in interface Promise<D>
      Returns:
      true if the current state of this promise is State.REJECTED, false otherwise.
      See Also:
    • waitSafely

      public void waitSafely() throws InterruptedException
      Description copied from interface: Promise
      This method will wait as long as the State is Pending. This method will return fast when State is not Pending.
      Specified by:
      waitSafely in interface Promise<D>
      Throws:
      InterruptedException - if thread is interrupted while waiting
    • waitSafely

      public void waitSafely(long timeout) throws InterruptedException
      Description copied from interface: Promise
      This 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:
      waitSafely in interface Promise<D>
      Parameters:
      timeout - the maximum time to wait in milliseconds
      Throws:
      InterruptedException - if thread is interrupted while waiting
    • get

      public D get()
      Specified by:
      get in interface Supplier<D>
    • getOrElse

      public D getOrElse(D defaultValue)
      Description copied from interface: Promise
      get the resolved value, return defaultValue for rejected
      Specified by:
      getOrElse in interface Promise<D>
      Parameters:
      defaultValue - 默认值
      Returns:
      如果Promise已解决则返回解决的值,否则返回默认值
    • getOrElse

      public D getOrElse(Supplier<D> valueSupplier)
      Description copied from interface: Promise
      get the resolved value, return defaultSupplier‘s result for rejected
      Specified by:
      getOrElse in interface Promise<D>
      Parameters:
      valueSupplier - 默认值提供者
      Returns:
      如果Promise已解决则返回解决的值,否则返回默认值
    • handleException

      protected void handleException(AbstractPromise.CallbackType callbackType, Exception exception)
      处理回调函数执行时的异常
      Parameters:
      callbackType - 回调类型
      exception - 发生的异常