Class DeferredPromiseImpl<T>

  • Type Parameters:
    T - The result type associated with the Promise.
    All Implemented Interfaces:
    Promise<T>

    final class DeferredPromiseImpl<T>
    extends PromiseImpl<T>
    Deferred Promise implementation.

    This class is not used directly by clients. Clients should use PromiseFactory.deferred() to create a Deferred which can be used to obtain a Promise whose resolution can be deferred.

    Since:
    1.1
    • Field Detail

      • resolved

        private final java.util.concurrent.CountDownLatch resolved
        A CountDownLatch to manage the resolved state of this Promise.

        This object is used as the synchronizing object to provide a critical section in tryResolve(Object, Throwable) so that only a single thread can write the resolved state variables and open the latch.

        The resolved state variables, value and fail, must only be written when the latch is closed (getCount() != 0) and must only be read when the latch is open (getCount() == 0). The latch state must always be checked before writing or reading since the resolved state variables' memory consistency is guarded by the latch.

      • fail

        private java.lang.Throwable fail
        The failure of this Promise if resolved with a failure or null if successfully resolved.
        See Also:
        PromiseImpl.<V>resolved(V)
    • Constructor Detail

      • DeferredPromiseImpl

        DeferredPromiseImpl​(PromiseFactory factory)
        Initialize this Promise.
        Parameters:
        factory - The factory to use for callbacks and scheduled operations.
    • Method Detail

      • isDone

        public boolean isDone()
        Returns whether this Promise has been resolved.

        This Promise may be successfully resolved or resolved with a failure.

        Returns:
        true if this Promise was resolved either successfully or with a failure; false if this Promise is unresolved.
      • orDone

        PromiseImpl<T> orDone()
        Return a resolved PromiseImpl if this DeferredPromiseImpl is resolved.
        Returns:
        A ResolvedPromiseImpl holding the value of this DeferredPromiseImpl or a FailedPromiseImpl holding the failure of this DeferredPromiseImpl or this DeferredPromiseImpl if this DeferredPromiseImpl is not resolved.
      • getValue

        public T getValue()
                   throws java.lang.reflect.InvocationTargetException,
                          java.lang.InterruptedException
        Returns the value of this Promise.

        If this Promise is not resolved, this method must block and wait for this Promise to be resolved before completing.

        If this Promise was successfully resolved, this method returns with the value of this Promise. If this Promise was resolved with a failure, this method must throw an InvocationTargetException with the failure exception as the cause.

        Returns:
        The value of this resolved Promise.
        Throws:
        java.lang.reflect.InvocationTargetException - If this Promise was resolved with a failure. The cause of the InvocationTargetException is the failure exception.
        java.lang.InterruptedException - If the current thread was interrupted while waiting.
      • getFailure

        public java.lang.Throwable getFailure()
                                       throws java.lang.InterruptedException
        Returns the failure of this Promise.

        If this Promise is not resolved, this method must block and wait for this Promise to be resolved before completing.

        If this Promise was resolved with a failure, this method returns with the failure of this Promise. If this Promise was successfully resolved, this method must return null.

        Returns:
        The failure of this resolved Promise or null if this Promise was successfully resolved.
        Throws:
        java.lang.InterruptedException - If the current thread was interrupted while waiting.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • tryResolve

        boolean tryResolve​(T v,
                           java.lang.Throwable f)
        Try to resolve this Promise.

        If this Promise was already resolved, return false. Otherwise, resolve this Promise and return true.

        Parameters:
        v - The value of this Promise.
        f - The failure of this Promise.
        Returns:
        false if this Promise was already resolved; true if this method resolved this Promise.
      • resolve

        void resolve​(T v,
                     java.lang.Throwable f)
        Resolve this Promise.

        If this Promise was already resolved, throw IllegalStateException. Otherwise, resolve this Promise.

        Parameters:
        v - The value of this Promise.
        f - The failure of this Promise.
        Throws:
        java.lang.IllegalStateException - If this Promise was already resolved.
      • resolveWith

        Promise<java.lang.Void> resolveWith​(Promise<? extends T> with)
        Resolve this Promise with the specified Promise.

        If the specified Promise is successfully resolved, this Promise is resolved with the value of the specified Promise. If the specified Promise is resolved with a failure, this Promise is resolved with the failure of the specified Promise.

        Parameters:
        with - A Promise whose value or failure must be used to resolve this Promise. Must not be null.
        Returns:
        A Promise that is resolved only when this Promise is resolved by the specified Promise. The returned Promise must be successfully resolved with the value null, if this Promise was resolved by the specified Promise. The returned Promise must be resolved with a failure of IllegalStateException, if this Promise was already resolved when the specified Promise was resolved.