Class PromiseImpl<T>

java.lang.Object
dev.webfx.platform.async.impl.FutureImpl<T>
dev.webfx.platform.async.impl.PromiseImpl<T>
All Implemented Interfaces:
AsyncResult<T>, Future<T>, Handler<AsyncResult<T>>, FutureInternal<T>, Listener<T>, PromiseInternal<T>, Promise<T>

public final class PromiseImpl<T> extends FutureImpl<T> implements PromiseInternal<T>, Listener<T>
Promise implementation.
Author:
Julien Viet
  • Constructor Details

    • PromiseImpl

      public PromiseImpl()
      Create a promise that hasn't completed yet
  • Method Details

    • handle

      public void handle(AsyncResult<T> ar)
      Create a promise that hasn't completed yet
      Specified by:
      handle in interface Handler<T>
      Specified by:
      handle in interface Promise<T>
      Parameters:
      ar - the async result to handle
    • onSuccess

      public void onSuccess(T value)
      Description copied from interface: Listener
      Signal the success.
      Specified by:
      onSuccess in interface Listener<T>
      Parameters:
      value - the value
    • onFailure

      public void onFailure(Throwable failure)
      Description copied from interface: Listener
      Signal the failure
      Specified by:
      onFailure in interface Listener<T>
      Parameters:
      failure - the failure
    • future

      public Future<T> future()
      Specified by:
      future in interface Promise<T>
      Returns:
      the Future associated with this promise, it can be used to be aware of the promise completion
    • emitSuccess

      protected final void emitSuccess(T value, Listener<T> listener)
      Create a future that hasn't completed yet
    • emitFailure

      protected final void emitFailure(Throwable cause, Listener<T> listener)
    • compose

      public <U> Future<U> compose(Function<T,Future<U>> successMapper, Function<Throwable,Future<U>> failureMapper)
      Description copied from interface: Future
      Compose this future with a successMapper and failureMapper functions.

      When this future (the one on which compose is called) succeeds, the successMapper will be called with the completed value and this mapper returns another future object. This returned future completion will complete the future returned by this method call.

      When this future (the one on which compose is called) fails, the failureMapper will be called with the failure and this mapper returns another future object. This returned future completion will complete the future returned by this method call.

      If any mapper function throws an exception, the returned future will be failed with this exception.

      Specified by:
      compose in interface Future<T>
      Parameters:
      successMapper - the function mapping the success
      failureMapper - the function mapping the failure
      Returns:
      the composed future
    • transform

      public <U> Future<U> transform(Function<AsyncResult<T>,Future<U>> mapper)
      Description copied from interface: Future
      Transform this future with a mapper functions.

      When this future (the one on which transform is called) completes, the mapper will be called with the async result and this mapper returns another future object. This returned future completion will complete the future returned by this method call.

      If any mapper function throws an exception, the returned future will be failed with this exception.

      Specified by:
      transform in interface Future<T>
      Parameters:
      mapper - the function mapping the future
      Returns:
      the transformed future
    • eventually

      public <U> Future<T> eventually(Function<Void,Future<U>> mapper)
      Description copied from interface: Future
      Compose this future with a function that will be always be called.

      When this future (the one on which eventually is called) completes, the function will be called and this mapper returns another future object. This returned future completion will complete the future returned by this method call with the original result of the future.

      The outcome of the future returned by the function will not influence the nature of the returned future.

      Specified by:
      eventually in interface Future<T>
      Parameters:
      mapper - the function returning the future.
      Returns:
      the composed future
    • map

      public <U> Future<U> map(Function<T,U> mapper)
      Description copied from interface: Future
      Apply a mapper function on this future.

      When this future succeeds, the mapper will be called with the completed value and this mapper returns a value. This value will complete the future returned by this method call.

      If the mapper throws an exception, the returned future will be failed with this exception.

      When this future fails, the failure will be propagated to the returned future and the mapper will not be called.

      Specified by:
      map in interface AsyncResult<T>
      Specified by:
      map in interface Future<T>
      Parameters:
      mapper - the mapper function
      Returns:
      the mapped future
    • map

      public <V> Future<V> map(V value)
      Description copied from interface: Future
      Map the result of a future to a specific value.

      When this future succeeds, this value will complete the future returned by this method call.

      When this future fails, the failure will be propagated to the returned future.

      Specified by:
      map in interface AsyncResult<T>
      Specified by:
      map in interface Future<T>
      Parameters:
      value - the value that eventually completes the mapped future
      Returns:
      the mapped future
    • otherwise

      public Future<T> otherwise(Function<Throwable,T> mapper)
      Description copied from interface: Future
      Apply a mapper function on this future.

      When this future fails, the mapper will be called with the completed value and this mapper returns a value. This value will complete the future returned by this method call.

      If the mapper throws an exception, the returned future will be failed with this exception.

      When this future succeeds, the result will be propagated to the returned future and the mapper will not be called.

      Specified by:
      otherwise in interface AsyncResult<T>
      Specified by:
      otherwise in interface Future<T>
      Parameters:
      mapper - the mapper function
      Returns:
      the mapped future
    • otherwise

      public Future<T> otherwise(T value)
      Description copied from interface: Future
      Map the failure of a future to a specific value.

      When this future fails, this value will complete the future returned by this method call.

      When this future succeeds, the result will be propagated to the returned future.

      Specified by:
      otherwise in interface AsyncResult<T>
      Specified by:
      otherwise in interface Future<T>
      Parameters:
      value - the value that eventually completes the mapped future
      Returns:
      the mapped future