All Downloads are FREE. Search and download functionalities are using the official Maven repository.

legolas.async.api.interfaces.Promises Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package legolas.async.api.interfaces;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public class Promises {
  private final List> promises;

  private Promises() {
    this.promises = new ArrayList<>();
  }

  public static Promises create() {
    return new Promises();
  }

  public  Promise add() {
    return this.add(() -> null);
  }

  public  Promise add(Supplier supplier) {
    Promise promise = Promise.create(supplier);
    this.promises.add(promise.asFuture());
    return promise;
  }

  public Promise all() {
    CompletableFuture future = CompletableFuture.allOf(this.promises.toArray(new CompletableFuture[this.promises.size()]));
    return new Promise<>(future);
  }

  public Promise any() {
    CompletableFuture future = CompletableFuture.anyOf(this.promises.toArray(new CompletableFuture[this.promises.size()]));
    return new Promise(future);
  }
}