Class Promise<T>

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

public class Promise<T>
extends java.lang.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:
Serialized Form
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.lang.Object error  
    protected boolean hadResult  
    protected boolean hasFired  
    protected java.lang.Object result  
    protected Callback resultReceiver  

    Fields inherited from interface org.nustaq.kontraktor.Callback

    CONT
  • Constructor Summary

    Constructors 
    Constructor Description
    Promise()
    create an unfulfilled/unsettled Promise
    Promise​(T result)
    create a resolved Promise by providing a result (cane be null).
    Promise​(T result, java.lang.Object error)
    create a settled Promise by either providing an result or error.
  • Method Summary

    Modifier and Type Method Description
    boolean _isHadResult()  
    boolean _isHasFired()  
    T await​(long timeoutMillis)
    see IPromise (inheriting Callback) interface
    IPromise<T> awaitPromise​(long timeout)
    see IPromise (inheriting Callback) interface
    <OUT> IPromise<OUT> catchError​(java.util.function.Consumer<java.lang.Object> function)
    see IPromise (inheriting Callback) interface
    <OUT> IPromise<OUT> catchError​(java.util.function.Function<java.lang.Object,​IPromise<OUT>> function)
    see IPromise (inheriting Callback) interface
    void complete​(java.lang.Object res, java.lang.Object error)
    see IPromise (inheriting Callback) interface
    void finallyDo​(Callback resultCB)
    same as then, but avoid creation of new promise
    T get()
    see IPromise (inheriting Callback) interface
    java.lang.Object getError()
    see IPromise (inheriting Callback) interface
    java.lang.String getId()
    remoting helper
    Promise getLast()
    see IPromise (inheriting Callback) interface
    Promise getNext()
    special method for tricky things.
    boolean isSettled()
    see IPromise (inheriting Callback) interface
    IPromise<T> onError​(java.util.function.Consumer errorHandler)
    see IPromise (inheriting Callback) interface
    IPromise<T> onResult​(java.util.function.Consumer<T> resultHandler)
    see IPromise (inheriting Callback) interface
    IPromise<T> onTimeout​(java.util.function.Consumer timeoutHandler)
    see IPromise (inheriting Callback) interface
    Promise<T> setId​(java.lang.String id)
    remoting helper
    IPromise<T> then​(java.lang.Runnable result)
    see IPromise interface
    <OUT> IPromise<OUT> then​(java.util.function.Consumer<T> function)
    see IPromise (inheriting Callback) interface
    IPromise then​(Callback resultCB)
    see IPromise (inheriting Callback) interface
    <OUT> IPromise<OUT> thenAnd​(java.util.function.Function<T,​IPromise<OUT>> function)
    see IPromise (inheriting Callback) interface
    IPromise<T> thenAnd​(java.util.function.Supplier<IPromise<T>> callable)
    see IPromise (inheriting Callback) interface
    void timedOut​(Timeout to)
    see IPromise (inheriting Callback) interface
    IPromise timeoutIn​(long millis)
    see IPromise (inheriting Callback) interface
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.nustaq.kontraktor.Callback

    complete, finish, isTerminated, pipe, reject, resolve, resolve

    Methods inherited from interface org.nustaq.kontraktor.IPromise

    await, awaitPromise, onTimeout
  • Field Details

  • Constructor Details

    • Promise

      public Promise​(T result, java.lang.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 java.lang.String getId()
      remoting helper
    • setId

      public Promise<T> setId​(java.lang.String id)
      remoting helper
    • then

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

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

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

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

      public <OUT> IPromise<OUT> thenAnd​(java.util.function.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​(java.util.function.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​(java.util.function.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​(java.util.function.Function<java.lang.Object,​IPromise<OUT>> function)
      see IPromise (inheriting Callback) interface
      Specified by:
      catchError in interface IPromise<T>
    • catchError

      public <OUT> IPromise<OUT> catchError​(java.util.function.Consumer<java.lang.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​(java.lang.Object res, java.lang.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 java.lang.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 java.lang.String toString()
      Overrides:
      toString in class java.lang.Object