Class AbstractPromise<T>

java.lang.Object
io.activej.promise.AbstractPromise<T>
All Implemented Interfaces:
AsyncComputation<T>, Promise<T>
Direct Known Subclasses:
NextPromise, SettablePromise

public abstract class AbstractPromise<T> extends Object implements Promise<T>
  • Field Details

    • result

      protected T result
    • exception

      @Nullable protected @Nullable Exception exception
    • next

      @Nullable protected @Nullable Object next
  • Constructor Details

    • AbstractPromise

      public AbstractPromise()
  • Method Details

    • reset

      public void reset()
    • resetCallbacks

      public void resetCallbacks()
    • isComplete

      public final boolean isComplete()
      Specified by:
      isComplete in interface Promise<T>
    • isResult

      public final boolean isResult()
      Specified by:
      isResult in interface Promise<T>
    • isException

      public final boolean isException()
      Specified by:
      isException in interface Promise<T>
    • getResult

      public T getResult()
      Specified by:
      getResult in interface Promise<T>
    • getException

      public Exception getException()
      Specified by:
      getException in interface Promise<T>
    • getTry

      public io.activej.common.collection.Try<T> getTry()
      Specified by:
      getTry in interface Promise<T>
    • complete

      protected void complete(@Nullable T value, @Nullable @Nullable Exception e)
    • complete

      protected void complete(@Nullable T value)
    • completeExceptionally

      protected void completeExceptionally(@Nullable @Nullable Exception e)
    • tryComplete

      protected boolean tryComplete(@Nullable T value, @Nullable @Nullable Exception e)
    • tryComplete

      protected boolean tryComplete(@Nullable T value)
    • tryCompleteExceptionally

      protected boolean tryCompleteExceptionally(Exception e)
    • map

      public <U> Promise<U> map(io.activej.common.function.FunctionEx<? super T,? extends U> fn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping a result of this promise to some other value. If this promise is completed exceptionally, a mapping function will not be applied.

      A mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      map in interface Promise<T>
      Parameters:
      fn - a function to map the result of this Promise to a new value
      Returns:
      new Promise whose result is the result of function applied to the result of this promise
      See Also:
    • map

      public <U> Promise<U> map(io.activej.common.function.BiFunctionEx<? super T,Exception,? extends U> fn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping a result and an exception of this promise to some other value. If this promise is completed exceptionally, an exception passed to a mapping function is guaranteed to be not null.

      A {bi function} may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      map in interface Promise<T>
      Parameters:
      fn - a function to map a result and an exception of this promise to some other value
      Returns:
      new Promise whose result is the result of function applied to the result and exception of this promise
    • map

      public <U> Promise<U> map(io.activej.common.function.FunctionEx<? super T,? extends U> fn, io.activej.common.function.FunctionEx<Exception,? extends U> exceptionFn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping either a result or an exception of this promise to some other values. If this promise is completed successfully, the first function will be called mapping the result to some other value. Otherwise, second function will be called mapping the exception to some other value.

      Each function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      map in interface Promise<T>
      Parameters:
      fn - a function to map a result of this promise to some other value
      exceptionFn - a function to map an exception of this promise to some other value
      Returns:
      new Promise whose result is the result of either first or second function applied either to a result or an exception of this promise.
    • mapException

      public Promise<T> mapException(io.activej.common.function.FunctionEx<Exception,Exception> exceptionFn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping an exception of this promise to some other exception. The mapping function will be called only if this promise completes exceptionally

      A mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      mapException in interface Promise<T>
      Parameters:
      exceptionFn - a function to map an exception of this promise to some other value
      Returns:
      new Promise whose result is the result of a mapping function applied either to an exception of this promise.
    • mapException

      public <E extends Exception> Promise<T> mapException(Class<E> clazz, io.activej.common.function.FunctionEx<? super E,? extends Exception> exceptionFn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping an exception of this promise to some other exception. The mapping function will be called only if this promise completes with an exception that is an instance of a given Class

      A mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      mapException in interface Promise<T>
      Parameters:
      clazz - an exception class that exception is tested against. A mapping function is applied only if an exception of this promise is an instance of the specified class
      exceptionFn - a function to map an exception of this promise to some other value
      Returns:
      new Promise whose result is the result of a mapping function applied either to an exception of this promise.
    • then

      public <U> Promise<U> then(AsyncSupplierEx<U> fn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by calling a provided supplier of a new promise. If this promise is completed exceptionally, a supplier will not be called.

      A supplier may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      then in interface Promise<T>
      Parameters:
      fn - a supplier of a new promise which will be called if this promise completes successfully
    • thenCallback

      public <U> Promise<U> thenCallback(CallbackSupplierEx<U> fn)
      Specified by:
      thenCallback in interface Promise<T>
    • then

      public <U> Promise<U> then(AsyncFunctionEx<? super T,U> fn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping a result of this promise to some other promise. If this promise is completed exceptionally, a mapping function will not be applied.

      A mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      then in interface Promise<T>
      Parameters:
      fn - a function to map the result of this Promise to a new promise
      Returns:
      new Promise which is the result of function applied to the result of this promise
      See Also:
    • thenCallback

      public <U> Promise<U> thenCallback(CallbackFunctionEx<? super T,U> fn)
      Specified by:
      thenCallback in interface Promise<T>
    • then

      public <U> Promise<U> then(AsyncBiFunctionEx<? super T,Exception,U> fn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping a result and an exception of this promise to some other promise. If this promise is completed exceptionally, an exception passed to a mapping function is guaranteed to be not null.

      A function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      then in interface Promise<T>
      Parameters:
      fn - a function to map a result and an exception of this promise to some other promise
      Returns:
      new Promise which is the result of function applied to the result and exception of this promise
    • thenCallback

      public <U> Promise<U> thenCallback(CallbackBiFunctionEx<? super T,@Nullable Exception,U> fn)
      Specified by:
      thenCallback in interface Promise<T>
    • then

      public <U> Promise<U> then(AsyncFunctionEx<? super T,U> fn, AsyncFunctionEx<Exception,U> exceptionFn)
      Description copied from interface: Promise
      Returns a new Promise which is obtained by mapping either a result or an exception of this promise to some other promises. If this promise is completed successfully, the first function will be called mapping the result to a promise of some other value. Otherwise, second function will be called mapping the exception to a promise of some other value.

      Each function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      then in interface Promise<T>
      Parameters:
      fn - a function to map a result of this promise to a dofferent promise
      exceptionFn - a function to map an exception of this promise a different promise
      Returns:
      new Promise which is a result of either first or second function applied either to a result or an exception of this promise.
    • thenCallback

      public <U> Promise<U> thenCallback(CallbackFunctionEx<? super T,U> fn, CallbackFunctionEx<Exception,U> exceptionFn)
      Specified by:
      thenCallback in interface Promise<T>
    • whenComplete

      public Promise<T> whenComplete(io.activej.common.function.BiConsumerEx<? super T,Exception> fn)
      Description copied from interface: Promise
      Subscribes given consumer to be executed after this Promise completes (either successfully or exceptionally). Returns a new Promise.

      A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenComplete in interface Promise<T>
      Parameters:
      fn - a consumer that consumes a result and an exception of this promise
    • whenComplete

      public Promise<T> whenComplete(io.activej.common.function.ConsumerEx<? super T> fn, io.activej.common.function.ConsumerEx<Exception> exceptionFn)
      Description copied from interface: Promise
      Subscribes given consumers to be executed after this Promise completes (either successfully or exceptionally). The first consumer will be executed if this Promise completes successfully, otherwise the second promise will be executed. Returns a new Promise.

      Each consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenComplete in interface Promise<T>
      Parameters:
      fn - consumer that consumes a result of this promise
      exceptionFn - consumer that consumes an exception of this promise
    • whenComplete

      public Promise<T> whenComplete(io.activej.common.function.RunnableEx action)
      Description copied from interface: Promise
      Subscribes given runnable to be executed after this Promise completes (either successfully or exceptionally). Returns a new Promise.

      A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenComplete in interface Promise<T>
      Parameters:
      action - runnable to be executed after this promise completes
    • whenResult

      public Promise<T> whenResult(io.activej.common.function.ConsumerEx<? super T> fn)
      Description copied from interface: Promise
      Subscribes given consumer to be executed after this Promise completes successfully. Returns a new Promise.

      A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenResult in interface Promise<T>
      Parameters:
      fn - consumer that consumes a result of this promise
    • whenResult

      public Promise<T> whenResult(io.activej.common.function.RunnableEx action)
      Description copied from interface: Promise
      Subscribes given runnable to be executed after this Promise completes successfully and a result of this Promise satisfy a given Predicate. Returns a new Promise.

      A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenResult in interface Promise<T>
      Parameters:
      action - runnable to be executed after this promise completes successfully
    • whenException

      public Promise<T> whenException(io.activej.common.function.ConsumerEx<Exception> fn)
      Description copied from interface: Promise
      Subscribes given consumer to be executed after this Promise completes exceptionally. Returns a new Promise.

      A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenException in interface Promise<T>
      Parameters:
      fn - consumer that consumes an exception of this promise
    • whenException

      public <E extends Exception> Promise<T> whenException(Class<E> clazz, io.activej.common.function.ConsumerEx<? super E> fn)
      Description copied from interface: Promise
      Subscribes given consumer to be executed after this Promise completes exceptionally and an exception of this Promise is an instance of a given exception Class. Returns a new Promise.

      A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenException in interface Promise<T>
      Parameters:
      clazz - an exception class that exception is tested against. A consumer is called only if an exception of this promise is an instance of the specified class
      fn - consumer that consumes an exception of this promise
    • whenException

      public Promise<T> whenException(io.activej.common.function.RunnableEx action)
      Description copied from interface: Promise
      Subscribes given runnable to be executed after this Promise completes exceptionally. Returns a new Promise.

      A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenException in interface Promise<T>
      Parameters:
      action - runnable to be executed after this promise completes exceptionally
    • whenException

      public Promise<T> whenException(Class<? extends Exception> clazz, io.activej.common.function.RunnableEx action)
      Description copied from interface: Promise
      Subscribes given runnable to be executed after this Promise completes exceptionally and an exception of this Promise is an instance of a given exception Class. Returns a new Promise.

      A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.

      Specified by:
      whenException in interface Promise<T>
      Parameters:
      clazz - an exception class that exception is tested against. A consumer is called only if an exception of this promise is an instance of the specified class
      action - runnable to be executed after this promise completes exceptionally and an exception of this Promise is an instance of a given exception Class.
    • async

      public Promise<T> async()
      Description copied from interface: Promise
      Ensures that Promise completes asynchronously: if this Promise is already completed, its completion will be posted to the next reactor tick. Otherwise, does nothing.
      Specified by:
      async in interface Promise<T>
    • combine

      public <U, V> Promise<V> combine(Promise<? extends U> other, io.activej.common.function.BiFunctionEx<? super T,? super U,? extends V> fn)
      Description copied from interface: Promise
      Returns a new Promise that, when this and the other given Promise both complete, is executed with the two results as arguments to the supplied function.
      Specified by:
      combine in interface Promise<T>
      Parameters:
      other - the other Promise
      fn - the function to use to compute the value of the returned Promise
      Returns:
      new Promise
    • both

      public Promise<Void> both(Promise<?> other)
      Description copied from interface: Promise
      Returns a new Promise when both this and provided other Promises complete.
      Specified by:
      both in interface Promise<T>
      Parameters:
      other - the other Promise
      Returns:
      Promise of null when both this and other Promise complete
    • recycleToVoid

      @Nullable protected static @Nullable Void recycleToVoid(Object item)
    • either

      public Promise<T> either(Promise<? extends T> other)
      Description copied from interface: Promise
      Returns the Promise which was completed first.
      Specified by:
      either in interface Promise<T>
      Parameters:
      other - the other Promise
      Returns:
      the first completed Promise
    • toTry

      public Promise<io.activej.common.collection.Try<T>> toTry()
      Description copied from interface: Promise
      Returns Promise that always completes successfully with result or exception wrapped in Try.
      Specified by:
      toTry in interface Promise<T>
    • toVoid

      public Promise<Void> toVoid()
      Description copied from interface: Promise
      Waits for result and discards it.
      Specified by:
      toVoid in interface Promise<T>
    • next

      public void next(NextPromise<? super T,?> cb)
      Description copied from interface: Promise
      Calls a callback after this promise is completed, passing both a result and an exception of a promise to the callback. This method is similar to Promise.whenComplete(BiConsumerEx) but does not create or return a new promise. A Callback interface also prohibits throwing checked exceptions.

      In most cases Promise.whenComplete(BiConsumerEx) is preferred. Fall back to using this method only if you expect a performance hit from constantly calling Promise.whenComplete(BiConsumerEx) and creating new promises as a result.

      Specified by:
      next in interface Promise<T>
      Parameters:
      cb - a callback to be called once a promise completes
    • subscribe

      public Promise<T> subscribe(Callback<? super T> cb)
      Specified by:
      subscribe in interface Promise<T>
    • toCompletableFuture

      public CompletableFuture<T> toCompletableFuture()
      Description copied from interface: Promise
      Wraps Promise into CompletableFuture.
      Specified by:
      toCompletableFuture in interface Promise<T>
    • describe

      protected String describe()
    • toString

      public String toString()
      Overrides:
      toString in class Object