Class FutureCombiner

java.lang.Object
org.drasyl.util.FutureCombiner

public final class FutureCombiner extends Object
A future combiner monitors the outcome of a number of discrete futures, then notifies a final, aggregate future when all of the combined futures are finished. The aggregate future will succeed if and only if all of the combined futures succeed. If any of the combined futures fail, the aggregate future will fail. The cause failure for the aggregate future will be the failure for one of the failed combined futures; if more than one of the combined futures fails, exactly which cause of failure will be assigned to the aggregate future is undefined.

Callers may populate a future combiner with any number of futures to be combined via the add(CompletableFuture) and addAll(CompletableFuture[]) methods. When all futures to be combined have been added, callers must provide an aggregate future to be notified when all combined futures have finished via the combine(CompletableFuture) method.

  • Method Details

    • getInstance

      public static FutureCombiner getInstance()
      Returns:
      a new instance of the future combiner
    • add

      public FutureCombiner add(CompletableFuture<?> future)
      Adds a new future to be combined. New futures may be added until an aggregate future is added via the combine(CompletableFuture) method.
      Parameters:
      future - the future to add to this future combiner
      Returns:
      this FutureCombiner
    • addAll

      public FutureCombiner addAll(CompletableFuture<?>... futures)
      Adds a new futures to be combined. New futures may be added until an aggregate future is added via the combine(CompletableFuture) method.
      Parameters:
      futures - the futures to add to this future combiner
      Returns:
      this FutureCombiner
    • combine

      public CompletableFuture<Void> combine(CompletableFuture<Void> future)
      Completes future if all of the added futures are completed. When any of the given futures completes exceptionally, the given future will also completes exceptionally immediately. If the combiner is empty, the future is completed immediately.
      Parameters:
      future - future that should be completed
      Returns:
      the given future
      Throws:
      NullPointerException - if future