/** * Like {@link #all(Future, Future)} but with 5 futures. */ static <T1, T2, T3, T4, T5> CompositeFuture all(Future<T1> f1, Future<T2> f2, Future<T3> f3, Future<T4> f4, Future<T5> f5) { return CompositeFutureImpl.all(f1, f2, f3, f4, f5); }
private static CompositeFuture join(Function<CompositeFuture, Throwable> pred, Future<?>... results) { CompositeFutureImpl composite = new CompositeFutureImpl(results); int len = results.length; for (int i = 0; i < len; i++) { results[i].setHandler(ar -> { Handler<AsyncResult<CompositeFuture>> handler = null; synchronized (composite) { composite.count++; if (!composite.isComplete() && composite.count == len) { // Take decision here Throwable failure = pred.apply(composite); handler = composite.setCompleted(failure); } } if (handler != null) { handler.handle(composite); } }); } if (len == 0) { composite.setCompleted(null); } return composite; }
/** * Like {@link #any(Future, Future)} but with 4 futures. */ static <T1, T2, T3, T4> CompositeFuture any(Future<T1> f1, Future<T2> f2, Future<T3> f3, Future<T4> f4) { return CompositeFutureImpl.any(f1, f2, f3, f4); }
/** * Like {@link #join(Future, Future)} but with 5 futures. */ static <T1, T2, T3, T4, T5> CompositeFuture join(Future<T1> f1, Future<T2> f2, Future<T3> f3, Future<T4> f4, Future<T5> f5) { return CompositeFutureImpl.join(f1, f2, f3, f4, f5); }
@Override public boolean failed(int index) { return future(index).failed(); }
@Override public boolean tryComplete() { return tryComplete(this); }
@Override public boolean tryFail(Throwable cause) { Handler<AsyncResult<CompositeFuture>> handler = setCompleted(cause); if (handler != null) { handler.handle(this); return true; } else { return false; } }
/** * Like {@link #join(Future, Future)} but with 3 futures. */ static <T1, T2, T3> CompositeFuture join(Future<T1> f1, Future<T2> f2, Future<T3> f3) { return CompositeFutureImpl.join(f1, f2, f3); }
@Override public <T> T resultAt(int index) { return this.<T>future(index).result(); }
@Override public void complete() { if (!tryComplete()) { throw new IllegalStateException("Result is already complete: " + (this.cause == null ? "succeeded" : "failed")); } }
@Override public boolean tryComplete(CompositeFuture result) { Handler<AsyncResult<CompositeFuture>> handler = setCompleted(null); if (handler != null) { handler.handle(this); return true; } else { return false; } }
public static CompositeFuture any(Future<?>... results) { CompositeFutureImpl composite = new CompositeFutureImpl(results); int len = results.length; for (int i = 0;i < len;i++) { results[i].setHandler(ar -> { Handler<AsyncResult<CompositeFuture>> handler = null; if (ar.succeeded()) { synchronized (composite) { if (!composite.isComplete()) { handler = composite.setCompleted(null); } } } else { synchronized (composite) { composite.count++; if (!composite.isComplete() && composite.count == len) { handler = composite.setCompleted(ar.cause()); } } } if (handler != null) { handler.handle(composite); } }); } if (results.length == 0) { composite.setCompleted(null); } return composite; }
/** * Return a composite future, succeeded when all futures are succeeded, failed when any future is failed. * <p/> * The returned future fails as soon as one of {@code f1} or {@code f2} fails. * * @param f1 future * @param f2 future * @return the composite future */ static <T1, T2> CompositeFuture all(Future<T1> f1, Future<T2> f2) { return CompositeFutureImpl.all(f1, f2); }
/** * Like {@link #join(Future, Future)} but with 4 futures. */ static <T1, T2, T3, T4> CompositeFuture join(Future<T1> f1, Future<T2> f2, Future<T3> f3, Future<T4> f4) { return CompositeFutureImpl.join(f1, f2, f3, f4); }
/** * Like {@link #any(Future, Future)} but with 5 futures. */ static <T1, T2, T3, T4, T5> CompositeFuture any(Future<T1> f1, Future<T2> f2, Future<T3> f3, Future<T4> f4, Future<T5> f5) { return CompositeFutureImpl.any(f1, f2, f3, f4, f5); }
@Override public boolean isComplete(int index) { return future(index).isComplete(); }
@Override public void complete(CompositeFuture result) { if (!tryComplete(result)) { throw new IllegalStateException("Result is already complete: " + (this.cause == null ? "succeeded" : "failed")); } }
@Override public boolean tryComplete(CompositeFuture result) { Handler<AsyncResult<CompositeFuture>> handler = setCompleted(null); if (handler != null) { handler.handle(this); return true; } else { return false; } }