Class PromiseSource<T>

  • Type Parameters:
    T - The type of value represented by the Promise produced by an instance of this class.

    public class PromiseSource<T>
    extends Object
    Instances of this class are used to create and complete Promise instances. Instances of this class are produced with a reference to an AsyncScheduler, which is used to invoke continuations on the returned Promise. By default, the current scheduler is captured when calling the constructor.
    • Constructor Detail

      • PromiseSource

        public PromiseSource()
        Creates an instance based on the current schedule as returned by AsyncSchedulers#current().
      • PromiseSource

        public PromiseSource​(AsyncScheduler scheduler)
        Creates an instance based on the given scheduler.
        Parameters:
        scheduler -
    • Method Detail

      • getPromise

        public Promise<T> getPromise()
        Returns the Promise associated with this instance.
        Returns:
        the Promise associated with this instance.
      • isCompleted

        public boolean isCompleted()
        Returns whether this instance and the returned Promise are in completed state, regardless of whether completed successfully or exceptionally.
        Returns:
        whether this instance and the returned Promise are in completed state.
      • setResult

        public void setResult​(T result)
        Completes the Promise associated with this instance with the given value. Throws if it has already been completed.
        Parameters:
        result - the result value the associated Promise should be resolved to.
        Throws:
        IllegalStateException - if already in completed state.
      • trySetResult

        public boolean trySetResult​(T result)
        Tries to complete the Promise associated with this instance with the given value. Ignores the given value if already in completed state.
        Parameters:
        result - the result value the associated Promise should be resolved to.
        Returns:
        true if the Promise associated with this instance was completed successfully, false if already completed.
      • setException

        public void setException​(Exception e)
        Exceptionally completes the Promise associated with this instance with the given Exception. Throws if already in completed state.
        Parameters:
        e - the Exception the associated Promise should be resolved to.
        Throws:
        IllegalStateException - if already in completed state.
      • trySetException

        public boolean trySetException​(Exception e)
        Tries to exceptionally complete the Promise associated with this instance with the given Exception. Ignores the given value if already in completed state.
        Parameters:
        e - the Exception the associated Promise should be resolved to.
        Returns:
        true if the Promise associated with this instance was completed successfully, false if already completed.
      • setAsyncResult

        public void setAsyncResult​(AsyncResult<? extends T> result)
        Completes the Promise associated with this instance with the given AsyncResult. Throws if it has already been completed.
        Parameters:
        result - the async result the associated Promise should be resolved to.
        Throws:
        IllegalStateException - if already in completed state.
      • trySetAsyncResult

        public boolean trySetAsyncResult​(AsyncResult<? extends T> result)
        Tries to complete the Promise associated with this instance with the given AsyncResult. Ignores the given value if already in completed state.
        Parameters:
        result - the async result the associated Promise should be resolved to.
        Returns:
        true if the Promise associated with this instance was completed successfully, false if already completed.
      • getCallerStackTrace

        public net.tpky.mc.concurrent.AsyncStackTrace getCallerStackTrace()
        for internal use
      • getResult

        public AsyncResult<? extends T> getResult()
        Returns the result this instance has been set to or null, if not in completed state yet.
        Returns:
        the result this instance has been set to or null, if not in completed state yet.