Class AbstractFactoryOfTheFuture

java.lang.Object
space.arim.omnibus.util.concurrent.impl.AbstractFactoryOfTheFuture
All Implemented Interfaces:
Executor, FactoryOfTheFuture, SynchronousExecutor
Direct Known Subclasses:
IndifferentFactoryOfTheFuture

public abstract class AbstractFactoryOfTheFuture extends Object implements FactoryOfTheFuture
Abstract base class for FactoryOfTheFuture using protected method newIncompleteFuture() to generate futures as determined by subclasses.

Requirements to be implemented by subclasses are newIncompleteFuture() and FactoryOfTheFuture.executeSync(Runnable).

execute(Runnable) may be optionally overridden to change the default executor for asynchronous work.
Author:
A248
  • Constructor Details

    • AbstractFactoryOfTheFuture

      public AbstractFactoryOfTheFuture()
  • Method Details

    • newIncompleteFuture

      public abstract <U> CentralisedFuture<U> newIncompleteFuture()
      Creates a new, incomplete future. The result of this method is used in implementing various FactoryOfTheFuture specifications.
      Specified by:
      newIncompleteFuture in interface FactoryOfTheFuture
      Type Parameters:
      U - the result type of the future
      Returns:
      a new incomplete future
    • execute

      public void execute(Runnable command)
      Description copied from interface: FactoryOfTheFuture
      Executes a Runnable asynchronously.
      Specified by:
      execute in interface Executor
      Specified by:
      execute in interface FactoryOfTheFuture
      Parameters:
      command - the command
    • runAsync

      public CentralisedFuture<?> runAsync(Runnable command)
      Description copied from interface: FactoryOfTheFuture
      Runs a Runnable asynchronously and returns a future indicating its progress.
      Specified by:
      runAsync in interface FactoryOfTheFuture
      Parameters:
      command - the runnable to run
      Returns:
      a centralised future representing the runnable's progress
    • runAsync

      public CentralisedFuture<?> runAsync(Runnable command, Executor executor)
      Description copied from interface: FactoryOfTheFuture
      Runs a Runnable asynchronously using the provided Executor and returns a future indicating its progress.
      Specified by:
      runAsync in interface FactoryOfTheFuture
      Parameters:
      command - the runnable to run
      executor - the executor on which to run the runnable
      Returns:
      a centralised future representing the runnable's progress
    • runSync

      public CentralisedFuture<?> runSync(Runnable command)
      Description copied from interface: FactoryOfTheFuture
      Runs a Runnable on the main thread if a main thread exists, else appropriately, per the specifications of SynchronousExecutor.executeSync(Runnable).
      Specified by:
      runSync in interface FactoryOfTheFuture
      Parameters:
      command - the runnable to run
      Returns:
      a centralised future representing the runnable's progress
    • supplyAsync

      public <T> CentralisedFuture<T> supplyAsync(Supplier<T> supplier)
      Description copied from interface: FactoryOfTheFuture
      Supplies a value asynchronously and returns a future indicating its progress.
      Specified by:
      supplyAsync in interface FactoryOfTheFuture
      Type Parameters:
      T - the return type of the supplier
      Parameters:
      supplier - the supplier to run
      Returns:
      a centralised future representing the supplier's progress
    • supplyAsync

      public <T> CentralisedFuture<T> supplyAsync(Supplier<T> supplier, Executor executor)
      Description copied from interface: FactoryOfTheFuture
      Supplies a value asynchronously using the provided Executor and returns a future indicating its progress.
      Specified by:
      supplyAsync in interface FactoryOfTheFuture
      Type Parameters:
      T - the return type of the supplier
      Parameters:
      supplier - the supplier to run
      executor - the executor on which to run the supplier
      Returns:
      a centralised future representing the supplier's progress
    • supplySync

      public <T> CentralisedFuture<T> supplySync(Supplier<T> supplier)
      Description copied from interface: FactoryOfTheFuture
      Supplies a value on the main thread if a main thread exists, else appropriately, per the specifications of SynchronousExecutor.executeSync(Runnable).
      Specified by:
      supplySync in interface FactoryOfTheFuture
      Type Parameters:
      T - the return type of the supplier
      Parameters:
      supplier - the supplier to run
      Returns:
      a centralised future representing the supplier's progress
    • completedFuture

      public <T> CentralisedFuture<T> completedFuture(T value)
      Description copied from interface: FactoryOfTheFuture
      Creates a precompleted future which has completed normally with the given value.
      Specified by:
      completedFuture in interface FactoryOfTheFuture
      Type Parameters:
      T - the result type of the future
      Parameters:
      value - the value with which to complete the future
      Returns:
      a future completed normally with the given value
    • completedStage

      public <T> ReactionStage<T> completedStage(T value)
      Description copied from interface: FactoryOfTheFuture
      Creates a precompleted stage which has completed normally with the given value.
      Specified by:
      completedStage in interface FactoryOfTheFuture
      Type Parameters:
      T - the result type of the stage
      Parameters:
      value - the value with which to complete the stage
      Returns:
      a stage completed normally with the given value
    • failedFuture

      public <T> CentralisedFuture<T> failedFuture(Throwable ex)
      Description copied from interface: FactoryOfTheFuture
      Creates a precompleted future which has completed exceptionally with the given exception.
      Specified by:
      failedFuture in interface FactoryOfTheFuture
      Type Parameters:
      T - the result type of the future
      Parameters:
      ex - the exception with which to complete the future
      Returns:
      a future completeld exceptionally with the given exception
    • failedStage

      public <T> ReactionStage<T> failedStage(Throwable ex)
      Description copied from interface: FactoryOfTheFuture
      Creates a precompleted stage which has completed exceptionally with the given exception.
      Specified by:
      failedStage in interface FactoryOfTheFuture
      Type Parameters:
      T - the result type of the stage
      Parameters:
      ex - the exception with which to complete the stage
      Returns:
      a stage completeld exceptionally with the given exception
    • copyFuture

      public <T> CentralisedFuture<T> copyFuture(CompletableFuture<T> completableFuture)
      Description copied from interface: FactoryOfTheFuture
      Copies a CompletableFuture to a CentralisedFuture.
      When the completable future completes, if it does so normally, the centralised future is also completed normally with the same result. Else, if the former completes exceptionally, the latter is also completed exceptionally with the same exception.
      Specified by:
      copyFuture in interface FactoryOfTheFuture
      Type Parameters:
      T - the result type of the future
      Parameters:
      completableFuture - the completable future
      Returns:
      a centralised future completed in the same way as the original completable future
    • copyStage

      public <T> ReactionStage<T> copyStage(CompletionStage<T> completionStage)
      Description copied from interface: FactoryOfTheFuture
      Copies a CompletionStage to a ReactionStage.
      When the completable future completes, if it does so normally, the reaction stage is also completed normally with the same result. Else, if the former completes exceptionally, the latter is also completed exceptionally with the same exception.
      Specified by:
      copyStage in interface FactoryOfTheFuture
      Type Parameters:
      T - the result type of the stage
      Parameters:
      completionStage - the completion stage
      Returns:
      a reaction stage completed in the same way as the original completion stage
    • allOf

      public CentralisedFuture<?> allOf(CentralisedFuture<?>... futures)
      Description copied from interface: FactoryOfTheFuture
      Creates a future which completes when all of the specified futures complete. If all do so normally, the combined future completes normally, else exceptionally with the exception.

      If no futures are provided, returns an already completed future.

      Note that the completed value of the combined future, if it completes normally, is not meaningful.
      Specified by:
      allOf in interface FactoryOfTheFuture
      Parameters:
      futures - the futures to combine
      Returns:
      a future completed combining the specified futures
    • allOf

      public <T> CentralisedFuture<?> allOf(Collection<? extends CentralisedFuture<T>> futures)
      Description copied from interface: FactoryOfTheFuture
      Creates a future which completes when all of the specified futures complete. If all do so normally, the combined future completes normally, else exceptionally with the exception.

      If no futures are provided, returns an already completed future.

      Note that the completed value of the combined future, if it completes normally, is not meaningful.
      Specified by:
      allOf in interface FactoryOfTheFuture
      Type Parameters:
      T - the type of object yielded by the futures
      Parameters:
      futures - the futures to combine
      Returns:
      a future completed combining the specified futures