Class CompoundFuture<R>

java.lang.Object
org.apache.sis.internal.processing.image.CompoundFuture<R>
All Implemented Interfaces:
Future<R>

final class CompoundFuture<R> extends Object implements Future<R>
The result of multiple asynchronous computations. This Future is considered completed when all components are completed.
Since:
1.1
Version:
1.1
  • Nested Class Summary

    Nested classes/interfaces inherited from interface java.util.concurrent.Future

    Future.State
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Future<R>[]
    The elements making this computation.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    CompoundFuture(Future<R>[] components)
    Creates a new future with the given components.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    cancel(boolean mayInterruptIfRunning)
    Attempts to cancel execution of this task.
    static <R> Future<R>
    create(Future<R>[] components)
    Returns a future waiting for all given tasks to complete.
    get()
    Waits if necessary for all computations to complete, and then retrieves the result.
    private R
    get(long timeout, boolean noTimeOut)
    Implementation of public get(…) methods.
    get(long timeout, TimeUnit unit)
    Same as get() but with a timeout.
    boolean
    Returns true if this task was cancelled before it completed normally.
    boolean
    Returns true if this task completed.
    protected R
    merge(Set<R> results)
    Invoked by get(…) if there is more than one non-null instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.concurrent.Future

    exceptionNow, resultNow, state
  • Field Details

    • components

      private final Future<R>[] components
      The elements making this computation.
  • Constructor Details

    • CompoundFuture

      private CompoundFuture(Future<R>[] components)
      Creates a new future with the given components.
  • Method Details

    • create

      public static <R> Future<R> create(Future<R>[] components)
      Returns a future waiting for all given tasks to complete. If the array length is 1, then this method returns directly its singleton element.
      Type Parameters:
      R - type if result computed by tasks.
      Parameters:
      components - the sub-tasks to execute. This array is not cloned; do not modify.
      Returns:
      a future containing all given tasks.
    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)
      Attempts to cancel execution of this task. After this method return, subsequent calls to isCancelled() return true if this method returned true.

      Departure from specification

      Future specification requires that after this method returns, subsequent calls to isDone() return true. This is not guaranteed in this implementation.
      Specified by:
      cancel in interface Future<R>
      Parameters:
      mayInterruptIfRunning - whether the thread executing tasks should be interrupted.
      Returns:
      true if at least one component task could be interrupted.
    • isCancelled

      public boolean isCancelled()
      Returns true if this task was cancelled before it completed normally. This task is considered cancelled if at least one component has been cancelled.
      Specified by:
      isCancelled in interface Future<R>
      Returns:
      true if at least one component task was cancelled before it completed.
    • isDone

      public boolean isDone()
      Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation.
      Specified by:
      isDone in interface Future<R>
      Returns:
      true if all component tasks completed.
    • get

      Waits if necessary for all computations to complete, and then retrieves the result. If all task components return either null or the same <R> value, then that result is returned. Otherwise the various <R> values are given to merge(Collection) for obtaining a single result.
      Specified by:
      get in interface Future<R>
      Returns:
      the computed result.
      Throws:
      CancellationException - if at least one computation was cancelled.
      ExecutionException - if at least one computation threw an exception.
      InterruptedException - if the current thread was interrupted while waiting.
    • get

      public R get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Same as get() but with a timeout. The given timeout is the total timeout; each component task may have a smaller timeout for keeping the total equal to the given value.
      Specified by:
      get in interface Future<R>
      Parameters:
      timeout - the maximum time to wait.
      unit - the time unit of the timeout argument.
      Throws:
      CancellationException - if at least one computation was cancelled.
      ExecutionException - if at least one computation threw an exception.
      InterruptedException - if the current thread was interrupted while waiting.
      TimeoutException - if the wait timed out.
    • get

      private R get(long timeout, boolean noTimeOut) throws InterruptedException, ExecutionException, TimeoutException
      Implementation of public get(…) methods. The timeout given to this method, if not ignored, is an absolute timeout.
      Parameters:
      timeout - System.nanoTime() value when to stop waiting.
      noTimeOut - true if timeout should be ignored.
      Throws:
      InterruptedException
      ExecutionException
      TimeoutException
    • merge

      protected R merge(Set<R> results)
      Invoked by get(…) if there is more than one non-null instance. The default implementation throws an exception.
      Parameters:
      results - all non-null instances found.
      Returns:
      the unique instance to return.