Class AbstractPromise<D>

java.lang.Object
cn.dinodev.spring.commons.promise.AbstractPromise<D>
All Implemented Interfaces:
Promise<D>, Supplier<D>
Direct Known Subclasses:
DeferredObject

public abstract class AbstractPromise<D> extends Object implements Promise<D>
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 DoneCallback so that when a Deferred object is resolved (Deferred.resolve(Object)), DoneCallback will be triggered. If the Deferred object is already resolved then the DoneCallback is triggered immediately. You can register multiple 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 FailCallback so that when a Deferred object is rejected (Deferred#reject(Object)), FailCallback will be triggered. If the Deferred object is already rejected then the FailCallback is triggered immediately. You can register multiple 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:
      • 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 (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 DoneCallback or 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)
    • triggerDone

      protected void triggerDone(Consumer<? super D> callback, D resolved)
    • triggerFail

      protected void triggerFail(Throwable rejected)
    • triggerFail

      protected void triggerFail(Consumer<Throwable> callback, Throwable rejected)
    • triggerAlways

      protected void triggerAlways(Promise.State state, D resolve, Throwable reject)
    • triggerAlways

      protected void triggerAlways(AlwaysCallback<? super D> callback, Promise.State state, D resolve, Throwable 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>
      Returns:
    • 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>
      Returns:
    • handleException

      protected void handleException(AbstractPromise.CallbackType callbackType, Exception e)