Class Promise<T>

java.lang.Object
org.nustaq.kontraktor.Promise<T>
All Implemented Interfaces:
Serializable, Callback<T>, IPromise<T>

public class Promise<T> extends Object implements IPromise<T>
implementation of the IPromise interface. A Promise is unfulfilled or "unsettled" once it has not been set a result. Its 'rejected' once an error has been set "reject(..)". Its 'resolved' once a result has been set "resolve(..)". Its 'settled' or 'completed' once a result or error has been set.
See Also:
  • Field Details

    • result

      protected Object result
    • error

      protected Object error
    • resultReceiver

      protected Callback resultReceiver
    • hadResult

      protected volatile boolean hadResult
    • hasFired

      protected boolean hasFired
  • Constructor Details

    • Promise

      public Promise(T result, Object error)
      create a settled Promise by either providing an result or error.
      Parameters:
      result -
      error -
    • Promise

      public Promise(T result)
      create a resolved Promise by providing a result (cane be null).
      Parameters:
      error -
    • Promise

      public Promise()
      create an unfulfilled/unsettled Promise
  • Method Details

    • getId

      public String getId()
      remoting helper
    • setId

      public Promise<T> setId(String id)
      remoting helper
    • then

      public IPromise<T> then(Runnable result)
      see IPromise interface
      Specified by:
      then in interface IPromise<T>
      Returns:
      a future ressolved after this
    • onResult

      public IPromise<T> onResult(Consumer<T> resultHandler)
      see IPromise (inheriting Callback) interface
      Specified by:
      onResult in interface IPromise<T>
      Returns:
    • onError

      public IPromise<T> onError(Consumer errorHandler)
      see IPromise (inheriting Callback) interface
      Specified by:
      onError in interface IPromise<T>
      Returns:
    • onTimeout

      public IPromise<T> onTimeout(Consumer timeoutHandler)
      see IPromise (inheriting Callback) interface
      Specified by:
      onTimeout in interface IPromise<T>
      Returns:
    • thenAnd

      public <OUT> IPromise<OUT> thenAnd(Function<T,IPromise<OUT>> function)
      see IPromise (inheriting Callback) interface
      Specified by:
      thenAnd in interface IPromise<T>
      Returns:
      a future ressolved with the Function result after this
    • then

      public <OUT> IPromise<OUT> then(Consumer<T> function)
      see IPromise (inheriting Callback) interface
      Specified by:
      then in interface IPromise<T>
      Returns:
      a future ressolved empty after this
    • thenAnd

      public IPromise<T> thenAnd(Supplier<IPromise<T>> callable)
      see IPromise (inheriting Callback) interface
      Specified by:
      thenAnd in interface IPromise<T>
      Returns:
      a future ressolved with the Supüplier result after this
    • catchError

      public <OUT> IPromise<OUT> catchError(Function<Object,IPromise<OUT>> function)
      see IPromise (inheriting Callback) interface
      Specified by:
      catchError in interface IPromise<T>
    • catchError

      public <OUT> IPromise<OUT> catchError(Consumer<Object> function)
      see IPromise (inheriting Callback) interface
      Specified by:
      catchError in interface IPromise<T>
    • timedOut

      public void timedOut(Timeout to)
      see IPromise (inheriting Callback) interface
    • then

      public IPromise then(Callback resultCB)
      see IPromise (inheriting Callback) interface
      Specified by:
      then in interface IPromise<T>
      Returns:
      a future ressolved with the Callable result after this
    • getNext

      public Promise getNext()
      special method for tricky things. Creates a nextFuture or returns it. current
      Returns:
    • getLast

      public Promise getLast()
      see IPromise (inheriting Callback) interface
    • finallyDo

      public void finallyDo(Callback resultCB)
      same as then, but avoid creation of new promise
      Parameters:
      resultCB -
    • complete

      public final void complete(Object res, Object error)
      see IPromise (inheriting Callback) interface
      Specified by:
      complete in interface Callback<T>
    • get

      public T get()
      see IPromise (inheriting Callback) interface
      Specified by:
      get in interface IPromise<T>
      Returns:
      result if avaiable (no blocking no awaiting).
    • await

      public T await(long timeoutMillis)
      see IPromise (inheriting Callback) interface
      Specified by:
      await in interface IPromise<T>
      Returns:
      the futures result or throw exception in case of error
    • awaitPromise

      public IPromise<T> awaitPromise(long timeout)
      see IPromise (inheriting Callback) interface
      Specified by:
      awaitPromise in interface IPromise<T>
      Parameters:
      timeout -
      Returns:
      the settled promise. No Exception is thrown, but the exception can be obtained by IPromise.getError()
    • timeoutIn

      public IPromise timeoutIn(long millis)
      see IPromise (inheriting Callback) interface
      Specified by:
      timeoutIn in interface IPromise<T>
      Returns:
      this for chaining
    • getError

      public Object getError()
      see IPromise (inheriting Callback) interface
      Specified by:
      getError in interface IPromise<T>
      Returns:
      error if avaiable
    • isSettled

      public boolean isSettled()
      see IPromise (inheriting Callback) interface
      Specified by:
      isSettled in interface IPromise<T>
      Returns:
      wether an error or a result has been set to this future
    • _isHadResult

      public boolean _isHadResult()
    • _isHasFired

      public boolean _isHasFired()
    • toString

      public String toString()
      Overrides:
      toString in class Object