Class SucceededFuture<T>

java.lang.Object
dev.webfx.platform.async.impl.SucceededFuture<T>
All Implemented Interfaces:
AsyncResult<T>, Future<T>, FutureInternal<T>

public final class SucceededFuture<T> extends Object
Succeeded future implementation.
Author:
Julien Viet
  • Field Details

    • EMPTY

      public static final SucceededFuture EMPTY
      Stateless instance of empty results that can be shared safely.
  • Constructor Details

    • SucceededFuture

      public SucceededFuture(T result)
      Create a future that has already succeeded
      Parameters:
      result - the result
  • Method Details

    • isComplete

      public boolean isComplete()
      Create a future that has already succeeded
      Parameters:
      context - the context
      result - the result
      Returns:
      true if completed, false if not
    • onSuccess

      public Future<T> onSuccess(Handler<T> handler)
      Description copied from interface: Future
      Add a handler to be notified of the succeeded result.

      WARNING: this is a terminal operation. If several handlers are registered, there is no guarantee that they will be invoked in order of registration.

      Parameters:
      handler - the handler that will be called with the succeeded result
      Returns:
      a reference to this, so it can be used fluently
    • onFailure

      public Future<T> onFailure(Handler<Throwable> handler)
      Description copied from interface: Future
      Add a handler to be notified of the failed result.

      WARNING: this is a terminal operation. If several handlers are registered, there is no guarantee that they will be invoked in order of registration.

      Parameters:
      handler - the handler that will be called with the failed result
      Returns:
      a reference to this, so it can be used fluently
    • onComplete

      public Future<T> onComplete(Handler<AsyncResult<T>> handler)
      Description copied from interface: Future
      Add a handler to be notified of the result.

      WARNING: this is a terminal operation. If several handlers are registered, there is no guarantee that they will be invoked in order of registration.

      Parameters:
      handler - the handler that will be called with the result
      Returns:
      a reference to this, so it can be used fluently
    • addListener

      public void addListener(Listener<T> listener)
      Description copied from interface: FutureInternal
      Add a listener to the future result.
      Parameters:
      listener - the listener
    • removeListener

      public void removeListener(Listener<T> listener)
      Description copied from interface: FutureInternal
      Remove a listener to the future result.
      Parameters:
      listener - the listener
    • result

      public T result()
      Description copied from interface: Future
      The result of the operation. This will be null if the operation failed.
      Returns:
      the result or null if the operation failed.
    • cause

      public Throwable cause()
      Description copied from interface: Future
      A Throwable describing failure. This will be null if the operation succeeded.
      Returns:
      the cause or null if the operation succeeded.
    • succeeded

      public boolean succeeded()
      Description copied from interface: Future
      Did it succeed?
      Returns:
      true if it succeded or false otherwise
    • failed

      public boolean failed()
      Description copied from interface: Future
      Did it fail?
      Returns:
      true if it failed or false otherwise
    • 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
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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