OpenJDK / lambda / lambda / jdk
changeset 8351:4202bdab3c2d
Use TL version
author | mduigou |
---|---|
date | Mon, 22 Apr 2013 15:00:28 -0700 |
parents | 88874f4dccae |
children | eed652e89edf |
files | src/share/classes/java/util/concurrent/CompletableFuture.java |
diffstat | 1 files changed, 384 insertions(+), 282 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/util/concurrent/CompletableFuture.java Mon Apr 22 14:48:07 2013 -0700 +++ b/src/share/classes/java/util/concurrent/CompletableFuture.java Mon Apr 22 15:00:28 2013 -0700 @@ -112,6 +112,10 @@ * methods {@link #join()} and {@link #getNow} throw the * CompletionException, which simplifies usage. * + * <p>Arguments used to pass a completion result (that is, for parameters + * of type {@code T}) may be null, but passing a null value for any other + * parameter will result in a {@link NullPointerException} being thrown. + * * @author Doug Lea * @since 1.8 */ @@ -207,7 +211,7 @@ * CompletionException unless it is one already. Otherwise uses * the given result, boxed as NIL if null. */ - final void internalComplete(Object v, Throwable ex) { + final void internalComplete(T v, Throwable ex) { if (result == null) UNSAFE.compareAndSwapObject (this, RESULT, null, @@ -504,12 +508,12 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class AsyncBiApply<T,U,V> extends Async { + static final class AsyncCombine<T,U,V> extends Async { final T arg1; final U arg2; final BiFunction<? super T,? super U,? extends V> fn; final CompletableFuture<V> dst; - AsyncBiApply(T arg1, U arg2, + AsyncCombine(T arg1, U arg2, BiFunction<? super T,? super U,? extends V> fn, CompletableFuture<V> dst) { this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst; @@ -555,14 +559,14 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class AsyncBiAccept<T,U> extends Async { + static final class AsyncAcceptBoth<T,U> extends Async { final T arg1; final U arg2; final BiConsumer<? super T,? super U> fn; final CompletableFuture<Void> dst; - AsyncBiAccept(T arg1, U arg2, - BiConsumer<? super T,? super U> fn, - CompletableFuture<Void> dst) { + AsyncAcceptBoth(T arg1, U arg2, + BiConsumer<? super T,? super U> fn, + CompletableFuture<Void> dst) { this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst; } public final boolean exec() { @@ -640,14 +644,15 @@ abstract static class Completion extends AtomicInteger implements Runnable { } - static final class ApplyCompletion<T,U> extends Completion { + static final class ThenApply<T,U> extends Completion { final CompletableFuture<? extends T> src; final Function<? super T,? extends U> fn; final CompletableFuture<U> dst; final Executor executor; - ApplyCompletion(CompletableFuture<? extends T> src, - Function<? super T,? extends U> fn, - CompletableFuture<U> dst, Executor executor) { + ThenApply(CompletableFuture<? extends T> src, + Function<? super T,? extends U> fn, + CompletableFuture<U> dst, + Executor executor) { this.src = src; this.fn = fn; this.dst = dst; this.executor = executor; } @@ -689,14 +694,15 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class AcceptCompletion<T> extends Completion { + static final class ThenAccept<T> extends Completion { final CompletableFuture<? extends T> src; final Consumer<? super T> fn; final CompletableFuture<Void> dst; final Executor executor; - AcceptCompletion(CompletableFuture<? extends T> src, - Consumer<? super T> fn, - CompletableFuture<Void> dst, Executor executor) { + ThenAccept(CompletableFuture<? extends T> src, + Consumer<? super T> fn, + CompletableFuture<Void> dst, + Executor executor) { this.src = src; this.fn = fn; this.dst = dst; this.executor = executor; } @@ -737,20 +743,20 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class RunCompletion<T> extends Completion { - final CompletableFuture<? extends T> src; + static final class ThenRun extends Completion { + final CompletableFuture<?> src; final Runnable fn; final CompletableFuture<Void> dst; final Executor executor; - RunCompletion(CompletableFuture<? extends T> src, - Runnable fn, - CompletableFuture<Void> dst, - Executor executor) { + ThenRun(CompletableFuture<?> src, + Runnable fn, + CompletableFuture<Void> dst, + Executor executor) { this.src = src; this.fn = fn; this.dst = dst; this.executor = executor; } public final void run() { - final CompletableFuture<? extends T> a; + final CompletableFuture<?> a; final Runnable fn; final CompletableFuture<Void> dst; Object r; Throwable ex; @@ -781,16 +787,17 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class BiApplyCompletion<T,U,V> extends Completion { + static final class ThenCombine<T,U,V> extends Completion { final CompletableFuture<? extends T> src; final CompletableFuture<? extends U> snd; final BiFunction<? super T,? super U,? extends V> fn; final CompletableFuture<V> dst; final Executor executor; - BiApplyCompletion(CompletableFuture<? extends T> src, - CompletableFuture<? extends U> snd, - BiFunction<? super T,? super U,? extends V> fn, - CompletableFuture<V> dst, Executor executor) { + ThenCombine(CompletableFuture<? extends T> src, + CompletableFuture<? extends U> snd, + BiFunction<? super T,? super U,? extends V> fn, + CompletableFuture<V> dst, + Executor executor) { this.src = src; this.snd = snd; this.fn = fn; this.dst = dst; this.executor = executor; @@ -832,7 +839,7 @@ if (ex == null) { try { if (e != null) - e.execute(new AsyncBiApply<T,U,V>(t, u, fn, dst)); + e.execute(new AsyncCombine<T,U,V>(t, u, fn, dst)); else v = fn.apply(t, u); } catch (Throwable rex) { @@ -846,16 +853,17 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class BiAcceptCompletion<T,U> extends Completion { + static final class ThenAcceptBoth<T,U> extends Completion { final CompletableFuture<? extends T> src; final CompletableFuture<? extends U> snd; final BiConsumer<? super T,? super U> fn; final CompletableFuture<Void> dst; final Executor executor; - BiAcceptCompletion(CompletableFuture<? extends T> src, - CompletableFuture<? extends U> snd, - BiConsumer<? super T,? super U> fn, - CompletableFuture<Void> dst, Executor executor) { + ThenAcceptBoth(CompletableFuture<? extends T> src, + CompletableFuture<? extends U> snd, + BiConsumer<? super T,? super U> fn, + CompletableFuture<Void> dst, + Executor executor) { this.src = src; this.snd = snd; this.fn = fn; this.dst = dst; this.executor = executor; @@ -896,7 +904,7 @@ if (ex == null) { try { if (e != null) - e.execute(new AsyncBiAccept<T,U>(t, u, fn, dst)); + e.execute(new AsyncAcceptBoth<T,U>(t, u, fn, dst)); else fn.accept(t, u); } catch (Throwable rex) { @@ -910,22 +918,23 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class BiRunCompletion<T> extends Completion { - final CompletableFuture<? extends T> src; + static final class RunAfterBoth extends Completion { + final CompletableFuture<?> src; final CompletableFuture<?> snd; final Runnable fn; final CompletableFuture<Void> dst; final Executor executor; - BiRunCompletion(CompletableFuture<? extends T> src, - CompletableFuture<?> snd, - Runnable fn, - CompletableFuture<Void> dst, Executor executor) { + RunAfterBoth(CompletableFuture<?> src, + CompletableFuture<?> snd, + Runnable fn, + CompletableFuture<Void> dst, + Executor executor) { this.src = src; this.snd = snd; this.fn = fn; this.dst = dst; this.executor = executor; } public final void run() { - final CompletableFuture<? extends T> a; + final CompletableFuture<?> a; final CompletableFuture<?> b; final Runnable fn; final CompletableFuture<Void> dst; @@ -993,16 +1002,17 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class OrApplyCompletion<T,U> extends Completion { + static final class ApplyToEither<T,U> extends Completion { final CompletableFuture<? extends T> src; final CompletableFuture<? extends T> snd; final Function<? super T,? extends U> fn; final CompletableFuture<U> dst; final Executor executor; - OrApplyCompletion(CompletableFuture<? extends T> src, - CompletableFuture<? extends T> snd, - Function<? super T,? extends U> fn, - CompletableFuture<U> dst, Executor executor) { + ApplyToEither(CompletableFuture<? extends T> src, + CompletableFuture<? extends T> snd, + Function<? super T,? extends U> fn, + CompletableFuture<U> dst, + Executor executor) { this.src = src; this.snd = snd; this.fn = fn; this.dst = dst; this.executor = executor; @@ -1046,16 +1056,17 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class OrAcceptCompletion<T> extends Completion { + static final class AcceptEither<T> extends Completion { final CompletableFuture<? extends T> src; final CompletableFuture<? extends T> snd; final Consumer<? super T> fn; final CompletableFuture<Void> dst; final Executor executor; - OrAcceptCompletion(CompletableFuture<? extends T> src, - CompletableFuture<? extends T> snd, - Consumer<? super T> fn, - CompletableFuture<Void> dst, Executor executor) { + AcceptEither(CompletableFuture<? extends T> src, + CompletableFuture<? extends T> snd, + Consumer<? super T> fn, + CompletableFuture<Void> dst, + Executor executor) { this.src = src; this.snd = snd; this.fn = fn; this.dst = dst; this.executor = executor; @@ -1098,22 +1109,23 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class OrRunCompletion<T> extends Completion { - final CompletableFuture<? extends T> src; + static final class RunAfterEither extends Completion { + final CompletableFuture<?> src; final CompletableFuture<?> snd; final Runnable fn; final CompletableFuture<Void> dst; final Executor executor; - OrRunCompletion(CompletableFuture<? extends T> src, - CompletableFuture<?> snd, - Runnable fn, - CompletableFuture<Void> dst, Executor executor) { + RunAfterEither(CompletableFuture<?> src, + CompletableFuture<?> snd, + Runnable fn, + CompletableFuture<Void> dst, + Executor executor) { this.src = src; this.snd = snd; this.fn = fn; this.dst = dst; this.executor = executor; } public final void run() { - final CompletableFuture<? extends T> a; + final CompletableFuture<?> a; final CompletableFuture<?> b; final Runnable fn; final CompletableFuture<Void> dst; @@ -1148,16 +1160,16 @@ static final class OrCompletion extends Completion { final CompletableFuture<?> src; final CompletableFuture<?> snd; - final CompletableFuture<?> dst; + final CompletableFuture<Object> dst; OrCompletion(CompletableFuture<?> src, CompletableFuture<?> snd, - CompletableFuture<?> dst) { + CompletableFuture<Object> dst) { this.src = src; this.snd = snd; this.dst = dst; } public final void run() { final CompletableFuture<?> a; final CompletableFuture<?> b; - final CompletableFuture<?> dst; + final CompletableFuture<Object> dst; Object r, t; Throwable ex; if ((dst = this.dst) != null && (((a = this.src) != null && (r = a.result) != null) || @@ -1214,17 +1226,17 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class ThenCopy extends Completion { + static final class ThenCopy<T> extends Completion { final CompletableFuture<?> src; - final CompletableFuture<?> dst; + final CompletableFuture<T> dst; ThenCopy(CompletableFuture<?> src, - CompletableFuture<?> dst) { + CompletableFuture<T> dst) { this.src = src; this.dst = dst; } public final void run() { final CompletableFuture<?> a; - final CompletableFuture<?> dst; - Object r; Object t; Throwable ex; + final CompletableFuture<T> dst; + Object r; T t; Throwable ex; if ((dst = this.dst) != null && (a = this.src) != null && (r = a.result) != null && @@ -1235,7 +1247,8 @@ } else { ex = null; - t = r; + @SuppressWarnings("unchecked") T tr = (T) r; + t = tr; } dst.internalComplete(t, ex); } @@ -1243,6 +1256,32 @@ private static final long serialVersionUID = 5232453952276885070L; } + // version of ThenCopy for CompletableFuture<Void> dst + static final class ThenPropagate extends Completion { + final CompletableFuture<?> src; + final CompletableFuture<Void> dst; + ThenPropagate(CompletableFuture<?> src, + CompletableFuture<Void> dst) { + this.src = src; this.dst = dst; + } + public final void run() { + final CompletableFuture<?> a; + final CompletableFuture<Void> dst; + Object r; Throwable ex; + if ((dst = this.dst) != null && + (a = this.src) != null && + (r = a.result) != null && + compareAndSet(0, 1)) { + if (r instanceof AltResult) + ex = ((AltResult)r).ex; + else + ex = null; + dst.internalComplete(null, ex); + } + } + private static final long serialVersionUID = 5232453952276885070L; + } + static final class HandleCompletion<T,U> extends Completion { final CompletableFuture<? extends T> src; final BiFunction<? super T, Throwable, ? extends U> fn; @@ -1283,14 +1322,15 @@ private static final long serialVersionUID = 5232453952276885070L; } - static final class ComposeCompletion<T,U> extends Completion { + static final class ThenCompose<T,U> extends Completion { final CompletableFuture<? extends T> src; final Function<? super T, CompletableFuture<U>> fn; final CompletableFuture<U> dst; final Executor executor; - ComposeCompletion(CompletableFuture<? extends T> src, - Function<? super T, CompletableFuture<U>> fn, - CompletableFuture<U> dst, Executor executor) { + ThenCompose(CompletableFuture<? extends T> src, + Function<? super T, CompletableFuture<U>> fn, + CompletableFuture<U> dst, + Executor executor) { this.src = src; this.fn = fn; this.dst = dst; this.executor = executor; } @@ -1329,11 +1369,11 @@ } } if (c != null) { - ThenCopy d = null; + ThenCopy<U> d = null; Object s; if ((s = c.result) == null) { CompletionNode p = new CompletionNode - (d = new ThenCopy(c, dst)); + (d = new ThenCopy<U>(c, dst)); while ((s = c.result) == null) { if (UNSAFE.compareAndSwapObject (c, COMPLETIONS, p.next = c.completions, p)) @@ -1442,6 +1482,19 @@ } /** + * Returns a new CompletableFuture that is already completed with + * the given value. + * + * @param value the value + * @return the completed CompletableFuture + */ + public static <U> CompletableFuture<U> completedFuture(U value) { + CompletableFuture<U> f = new CompletableFuture<U>(); + f.result = (value == null) ? NIL : value; + return f; + } + + /** * Returns {@code true} if completed in any fashion: normally, * exceptionally, or via cancellation. * @@ -1609,9 +1662,10 @@ * when this CompletableFuture completes, with the result of the * given function of this CompletableFuture's result. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied function throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param fn the function to use to compute the value of * the returned CompletableFuture @@ -1624,12 +1678,13 @@ /** * Returns a new CompletableFuture that is asynchronously completed * when this CompletableFuture completes, with the result of the - * given function of this CompletableFuture's result, called from a + * given function of this CompletableFuture's result from a * task running in the {@link ForkJoinPool#commonPool()}. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied function throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param fn the function to use to compute the value of * the returned CompletableFuture @@ -1643,12 +1698,13 @@ /** * Returns a new CompletableFuture that is asynchronously completed * when this CompletableFuture completes, with the result of the - * given function of this CompletableFuture's result, called from a + * given function of this CompletableFuture's result from a * task running in the given executor. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied function throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param fn the function to use to compute the value of * the returned CompletableFuture @@ -1667,11 +1723,11 @@ Executor e) { if (fn == null) throw new NullPointerException(); CompletableFuture<U> dst = new CompletableFuture<U>(); - ApplyCompletion<T,U> d = null; + ThenApply<T,U> d = null; Object r; if ((r = result) == null) { CompletionNode p = new CompletionNode - (d = new ApplyCompletion<T,U>(this, fn, dst, e)); + (d = new ThenApply<T,U>(this, fn, dst, e)); while ((r = result) == null) { if (UNSAFE.compareAndSwapObject (this, COMPLETIONS, p.next = completions, p)) @@ -1712,9 +1768,10 @@ * when this CompletableFuture completes, after performing the given * action with this CompletableFuture's result. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied action throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param block the action to perform before completing the * returned CompletableFuture @@ -1730,9 +1787,10 @@ * action with this CompletableFuture's result from a task running * in the {@link ForkJoinPool#commonPool()}. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied action throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param block the action to perform before completing the * returned CompletableFuture @@ -1748,9 +1806,10 @@ * action with this CompletableFuture's result from a task running * in the given executor. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied action throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param block the action to perform before completing the * returned CompletableFuture @@ -1767,11 +1826,11 @@ Executor e) { if (fn == null) throw new NullPointerException(); CompletableFuture<Void> dst = new CompletableFuture<Void>(); - AcceptCompletion<T> d = null; + ThenAccept<T> d = null; Object r; if ((r = result) == null) { CompletionNode p = new CompletionNode - (d = new AcceptCompletion<T>(this, fn, dst, e)); + (d = new ThenAccept<T>(this, fn, dst, e)); while ((r = result) == null) { if (UNSAFE.compareAndSwapObject (this, COMPLETIONS, p.next = completions, p)) @@ -1811,9 +1870,10 @@ * when this CompletableFuture completes, after performing the given * action. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied action throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param action the action to perform before completing the * returned CompletableFuture @@ -1828,9 +1888,10 @@ * when this CompletableFuture completes, after performing the given * action from a task running in the {@link ForkJoinPool#commonPool()}. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied action throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param action the action to perform before completing the * returned CompletableFuture @@ -1845,9 +1906,10 @@ * when this CompletableFuture completes, after performing the given * action from a task running in the given executor. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding this exception as its cause. + * <p>If this CompletableFuture completes exceptionally, or the + * supplied action throws an exception, then the returned + * CompletableFuture completes exceptionally with a + * CompletionException holding the exception as its cause. * * @param action the action to perform before completing the * returned CompletableFuture @@ -1864,11 +1926,11 @@ Executor e) { if (action == null) throw new NullPointerException(); CompletableFuture<Void> dst = new CompletableFuture<Void>(); - RunCompletion<T> d = null; + ThenRun d = null; Object r; if ((r = result) == null) { CompletionNode p = new CompletionNode - (d = new RunCompletion<T>(this, action, dst, e)); + (d = new ThenRun(this, action, dst, e)); while ((r = result) == null) { if (UNSAFE.compareAndSwapObject (this, COMPLETIONS, p.next = completions, p)) @@ -1904,9 +1966,10 @@ * with the result of the given function of the results of the two * CompletableFutures. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding the exception as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied function throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param fn the function to use to compute the value of @@ -1916,19 +1979,20 @@ public <U,V> CompletableFuture<V> thenCombine (CompletableFuture<? extends U> other, BiFunction<? super T,? super U,? extends V> fn) { - return doThenBiApply(other, fn, null); + return doThenCombine(other, fn, null); } /** * Returns a new CompletableFuture that is asynchronously completed * when both this and the other given CompletableFuture complete, * with the result of the given function of the results of the two - * CompletableFutures, called from a task running in the + * CompletableFutures from a task running in the * {@link ForkJoinPool#commonPool()}. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding the exception as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied function throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param fn the function to use to compute the value of @@ -1938,19 +2002,19 @@ public <U,V> CompletableFuture<V> thenCombineAsync (CompletableFuture<? extends U> other, BiFunction<? super T,? super U,? extends V> fn) { - return doThenBiApply(other, fn, ForkJoinPool.commonPool()); + return doThenCombine(other, fn, ForkJoinPool.commonPool()); } /** * Returns a new CompletableFuture that is asynchronously completed * when both this and the other given CompletableFuture complete, * with the result of the given function of the results of the two - * CompletableFutures, called from a task running in the - * given executor. + * CompletableFutures from a task running in the given executor. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding the exception as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied function throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param fn the function to use to compute the value of @@ -1963,19 +2027,19 @@ BiFunction<? super T,? super U,? extends V> fn, Executor executor) { if (executor == null) throw new NullPointerException(); - return doThenBiApply(other, fn, executor); + return doThenCombine(other, fn, executor); } - private <U,V> CompletableFuture<V> doThenBiApply + private <U,V> CompletableFuture<V> doThenCombine (CompletableFuture<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor e) { if (other == null || fn == null) throw new NullPointerException(); CompletableFuture<V> dst = new CompletableFuture<V>(); - BiApplyCompletion<T,U,V> d = null; + ThenCombine<T,U,V> d = null; Object r, s = null; if ((r = result) == null || (s = other.result) == null) { - d = new BiApplyCompletion<T,U,V>(this, other, fn, dst, e); + d = new ThenCombine<T,U,V>(this, other, fn, dst, e); CompletionNode q = null, p = new CompletionNode(d); while ((r == null && (r = result) == null) || (s == null && (s = other.result) == null)) { @@ -2019,7 +2083,7 @@ if (ex == null) { try { if (e != null) - e.execute(new AsyncBiApply<T,U,V>(t, u, fn, dst)); + e.execute(new AsyncCombine<T,U,V>(t, u, fn, dst)); else v = fn.apply(t, u); } catch (Throwable rex) { @@ -2040,9 +2104,10 @@ * after performing the given action with the results of the two * CompletableFutures. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding one of these exceptions as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied action throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param block the action to perform before completing the @@ -2052,7 +2117,7 @@ public <U> CompletableFuture<Void> thenAcceptBoth (CompletableFuture<? extends U> other, BiConsumer<? super T, ? super U> block) { - return doThenBiAccept(other, block, null); + return doThenAcceptBoth(other, block, null); } /** @@ -2062,9 +2127,10 @@ * CompletableFutures from a task running in the {@link * ForkJoinPool#commonPool()}. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding one of these exceptions as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied action throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param block the action to perform before completing the @@ -2074,7 +2140,7 @@ public <U> CompletableFuture<Void> thenAcceptBothAsync (CompletableFuture<? extends U> other, BiConsumer<? super T, ? super U> block) { - return doThenBiAccept(other, block, ForkJoinPool.commonPool()); + return doThenAcceptBoth(other, block, ForkJoinPool.commonPool()); } /** @@ -2083,9 +2149,10 @@ * after performing the given action with the results of the two * CompletableFutures from a task running in the given executor. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding one of these exceptions as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied action throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param block the action to perform before completing the @@ -2098,19 +2165,19 @@ BiConsumer<? super T, ? super U> block, Executor executor) { if (executor == null) throw new NullPointerException(); - return doThenBiAccept(other, block, executor); + return doThenAcceptBoth(other, block, executor); } - private <U> CompletableFuture<Void> doThenBiAccept + private <U> CompletableFuture<Void> doThenAcceptBoth (CompletableFuture<? extends U> other, BiConsumer<? super T,? super U> fn, Executor e) { if (other == null || fn == null) throw new NullPointerException(); CompletableFuture<Void> dst = new CompletableFuture<Void>(); - BiAcceptCompletion<T,U> d = null; + ThenAcceptBoth<T,U> d = null; Object r, s = null; if ((r = result) == null || (s = other.result) == null) { - d = new BiAcceptCompletion<T,U>(this, other, fn, dst, e); + d = new ThenAcceptBoth<T,U>(this, other, fn, dst, e); CompletionNode q = null, p = new CompletionNode(d); while ((r == null && (r = result) == null) || (s == null && (s = other.result) == null)) { @@ -2153,7 +2220,7 @@ if (ex == null) { try { if (e != null) - e.execute(new AsyncBiAccept<T,U>(t, u, fn, dst)); + e.execute(new AsyncAcceptBoth<T,U>(t, u, fn, dst)); else fn.accept(t, u); } catch (Throwable rex) { @@ -2173,9 +2240,10 @@ * when both this and the other given CompletableFuture complete, * after performing the given action. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding one of these exceptions as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied action throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param action the action to perform before completing the @@ -2184,7 +2252,7 @@ */ public CompletableFuture<Void> runAfterBoth(CompletableFuture<?> other, Runnable action) { - return doThenBiRun(other, action, null); + return doRunAfterBoth(other, action, null); } /** @@ -2193,9 +2261,10 @@ * after performing the given action from a task running in the * {@link ForkJoinPool#commonPool()}. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding one of these exceptions as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied action throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param action the action to perform before completing the @@ -2204,7 +2273,7 @@ */ public CompletableFuture<Void> runAfterBothAsync(CompletableFuture<?> other, Runnable action) { - return doThenBiRun(other, action, ForkJoinPool.commonPool()); + return doRunAfterBoth(other, action, ForkJoinPool.commonPool()); } /** @@ -2213,9 +2282,10 @@ * after performing the given action from a task running in the * given executor. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture also does so, with a - * CompletionException holding one of these exceptions as its cause. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, or the supplied action throws an exception, + * then the returned CompletableFuture completes exceptionally + * with a CompletionException holding the exception as its cause. * * @param other the other CompletableFuture * @param action the action to perform before completing the @@ -2227,18 +2297,18 @@ Runnable action, Executor executor) { if (executor == null) throw new NullPointerException(); - return doThenBiRun(other, action, executor); + return doRunAfterBoth(other, action, executor); } - private CompletableFuture<Void> doThenBiRun(CompletableFuture<?> other, - Runnable action, - Executor e) { + private CompletableFuture<Void> doRunAfterBoth(CompletableFuture<?> other, + Runnable action, + Executor e) { if (other == null || action == null) throw new NullPointerException(); CompletableFuture<Void> dst = new CompletableFuture<Void>(); - BiRunCompletion<T> d = null; + RunAfterBoth d = null; Object r, s = null; if ((r = result) == null || (s = other.result) == null) { - d = new BiRunCompletion<T>(this, other, action, dst, e); + d = new RunAfterBoth(this, other, action, dst, e); CompletionNode q = null, p = new CompletionNode(d); while ((r == null && (r = result) == null) || (s == null && (s = other.result) == null)) { @@ -2289,11 +2359,14 @@ * with the result of the given function of either this or the other * CompletableFuture's result. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied function + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param fn the function to use to compute the value of @@ -2303,21 +2376,24 @@ public <U> CompletableFuture<U> applyToEither (CompletableFuture<? extends T> other, Function<? super T, U> fn) { - return doOrApply(other, fn, null); + return doApplyToEither(other, fn, null); } /** * Returns a new CompletableFuture that is asynchronously completed * when either this or the other given CompletableFuture completes, * with the result of the given function of either this or the other - * CompletableFuture's result, called from a task running in the + * CompletableFuture's result from a task running in the * {@link ForkJoinPool#commonPool()}. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied function + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param fn the function to use to compute the value of @@ -2327,21 +2403,24 @@ public <U> CompletableFuture<U> applyToEitherAsync (CompletableFuture<? extends T> other, Function<? super T, U> fn) { - return doOrApply(other, fn, ForkJoinPool.commonPool()); + return doApplyToEither(other, fn, ForkJoinPool.commonPool()); } /** * Returns a new CompletableFuture that is asynchronously completed * when either this or the other given CompletableFuture completes, * with the result of the given function of either this or the other - * CompletableFuture's result, called from a task running in the + * CompletableFuture's result from a task running in the * given executor. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied function + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param fn the function to use to compute the value of @@ -2354,19 +2433,19 @@ Function<? super T, U> fn, Executor executor) { if (executor == null) throw new NullPointerException(); - return doOrApply(other, fn, executor); + return doApplyToEither(other, fn, executor); } - private <U> CompletableFuture<U> doOrApply + private <U> CompletableFuture<U> doApplyToEither (CompletableFuture<? extends T> other, Function<? super T, U> fn, Executor e) { if (other == null || fn == null) throw new NullPointerException(); CompletableFuture<U> dst = new CompletableFuture<U>(); - OrApplyCompletion<T,U> d = null; + ApplyToEither<T,U> d = null; Object r; if ((r = result) == null && (r = other.result) == null) { - d = new OrApplyCompletion<T,U>(this, other, fn, dst, e); + d = new ApplyToEither<T,U>(this, other, fn, dst, e); CompletionNode q = null, p = new CompletionNode(d); while ((r = result) == null && (r = other.result) == null) { if (q != null) { @@ -2415,11 +2494,14 @@ * after performing the given action with the result of either this * or the other CompletableFuture's result. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied action + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param block the action to perform before completing the @@ -2429,7 +2511,7 @@ public CompletableFuture<Void> acceptEither (CompletableFuture<? extends T> other, Consumer<? super T> block) { - return doOrAccept(other, block, null); + return doAcceptEither(other, block, null); } /** @@ -2439,11 +2521,14 @@ * or the other CompletableFuture's result from a task running in * the {@link ForkJoinPool#commonPool()}. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied action + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param block the action to perform before completing the @@ -2453,7 +2538,7 @@ public CompletableFuture<Void> acceptEitherAsync (CompletableFuture<? extends T> other, Consumer<? super T> block) { - return doOrAccept(other, block, ForkJoinPool.commonPool()); + return doAcceptEither(other, block, ForkJoinPool.commonPool()); } /** @@ -2463,11 +2548,14 @@ * or the other CompletableFuture's result from a task running in * the given executor. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied action + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param block the action to perform before completing the @@ -2480,19 +2568,19 @@ Consumer<? super T> block, Executor executor) { if (executor == null) throw new NullPointerException(); - return doOrAccept(other, block, executor); + return doAcceptEither(other, block, executor); } - private CompletableFuture<Void> doOrAccept + private CompletableFuture<Void> doAcceptEither (CompletableFuture<? extends T> other, Consumer<? super T> fn, Executor e) { if (other == null || fn == null) throw new NullPointerException(); CompletableFuture<Void> dst = new CompletableFuture<Void>(); - OrAcceptCompletion<T> d = null; + AcceptEither<T> d = null; Object r; if ((r = result) == null && (r = other.result) == null) { - d = new OrAcceptCompletion<T>(this, other, fn, dst, e); + d = new AcceptEither<T>(this, other, fn, dst, e); CompletionNode q = null, p = new CompletionNode(d); while ((r = result) == null && (r = other.result) == null) { if (q != null) { @@ -2539,11 +2627,14 @@ * when either this or the other given CompletableFuture completes, * after performing the given action. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied action + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param action the action to perform before completing the @@ -2552,7 +2643,7 @@ */ public CompletableFuture<Void> runAfterEither(CompletableFuture<?> other, Runnable action) { - return doOrRun(other, action, null); + return doRunAfterEither(other, action, null); } /** @@ -2561,11 +2652,14 @@ * after performing the given action from a task running in the * {@link ForkJoinPool#commonPool()}. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied action + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param action the action to perform before completing the @@ -2575,7 +2669,7 @@ public CompletableFuture<Void> runAfterEitherAsync (CompletableFuture<?> other, Runnable action) { - return doOrRun(other, action, ForkJoinPool.commonPool()); + return doRunAfterEither(other, action, ForkJoinPool.commonPool()); } /** @@ -2584,11 +2678,14 @@ * after performing the given action from a task running in the * given executor. * - * <p>If this and/or the other CompletableFuture complete exceptionally, - * then the returned CompletableFuture may also do so, with a - * CompletionException holding one of these exceptions as its cause. - * No guarantees are made about which result or exception is used in - * the returned CompletableFuture. + * <p>If this and/or the other CompletableFuture complete + * exceptionally, then the returned CompletableFuture may also do so, + * with a CompletionException holding one of these exceptions as its + * cause. No guarantees are made about which result or exception is + * used in the returned CompletableFuture. If the supplied action + * throws an exception, then the returned CompletableFuture completes + * exceptionally with a CompletionException holding the exception as + * its cause. * * @param other the other CompletableFuture * @param action the action to perform before completing the @@ -2601,18 +2698,19 @@ Runnable action, Executor executor) { if (executor == null) throw new NullPointerException(); - return doOrRun(other, action, executor); + return doRunAfterEither(other, action, executor); } - private CompletableFuture<Void> doOrRun(CompletableFuture<?> other, - Runnable action, - Executor e) { + private CompletableFuture<Void> doRunAfterEither + (CompletableFuture<?> other, + Runnable action, + Executor e) { if (other == null || action == null) throw new NullPointerException(); CompletableFuture<Void> dst = new CompletableFuture<Void>(); - OrRunCompletion<T> d = null; + RunAfterEither d = null; Object r; if ((r = result) == null && (r = other.result) == null) { - d = new OrRunCompletion<T>(this, other, action, dst, e); + d = new RunAfterEither(this, other, action, dst, e); CompletionNode q = null, p = new CompletionNode(d); while ((r = result) == null && (r = other.result) == null) { if (q != null) { @@ -2650,76 +2748,77 @@ } /** - * Returns a CompletableFuture (or an equivalent one) produced by the - * given function of the result of this CompletableFuture when completed. + * Returns a CompletableFuture that upon completion, has the same + * value as produced by the given function of the result of this + * CompletableFuture. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a + * <p>If this CompletableFuture completes exceptionally, then the + * returned CompletableFuture also does so, with a * CompletionException holding this exception as its cause. + * Similarly, if the computed CompletableFuture completes + * exceptionally, then so does the returned CompletableFuture. * * @param fn the function returning a new CompletableFuture - * @return the CompletableFuture, that {@code isDone()} upon - * return if completed by the given function, or an exception - * occurs + * @return the CompletableFuture */ public <U> CompletableFuture<U> thenCompose (Function<? super T, CompletableFuture<U>> fn) { - return doCompose(fn, null); + return doThenCompose(fn, null); } /** - * Returns a CompletableFuture (or an equivalent one) produced - * asynchronously using the {@link ForkJoinPool#commonPool()} by - * the given function of the result of this CompletableFuture when - * completed. + * Returns a CompletableFuture that upon completion, has the same + * value as that produced asynchronously using the {@link + * ForkJoinPool#commonPool()} by the given function of the result + * of this CompletableFuture. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a + * <p>If this CompletableFuture completes exceptionally, then the + * returned CompletableFuture also does so, with a * CompletionException holding this exception as its cause. + * Similarly, if the computed CompletableFuture completes + * exceptionally, then so does the returned CompletableFuture. * * @param fn the function returning a new CompletableFuture - * @return the CompletableFuture, that {@code isDone()} upon - * return if completed by the given function, or an exception - * occurs + * @return the CompletableFuture */ public <U> CompletableFuture<U> thenComposeAsync (Function<? super T, CompletableFuture<U>> fn) { - return doCompose(fn, ForkJoinPool.commonPool()); + return doThenCompose(fn, ForkJoinPool.commonPool()); } /** - * Returns a CompletableFuture (or an equivalent one) produced - * asynchronously using the given executor by the given function - * of the result of this CompletableFuture when completed. + * Returns a CompletableFuture that upon completion, has the same + * value as that produced asynchronously using the given executor + * by the given function of this CompletableFuture. * - * <p>If this CompletableFuture completes exceptionally, - * then the returned CompletableFuture also does so, with a + * <p>If this CompletableFuture completes exceptionally, then the + * returned CompletableFuture also does so, with a * CompletionException holding this exception as its cause. + * Similarly, if the computed CompletableFuture completes + * exceptionally, then so does the returned CompletableFuture. * * @param fn the function returning a new CompletableFuture * @param executor the executor to use for asynchronous execution - * @return the CompletableFuture, that {@code isDone()} upon - * return if completed by the given function, or an exception - * occurs + * @return the CompletableFuture */ public <U> CompletableFuture<U> thenComposeAsync (Function<? super T, CompletableFuture<U>> fn, Executor executor) { if (executor == null) throw new NullPointerException(); - return doCompose(fn, executor); + return doThenCompose(fn, executor); } - private <U> CompletableFuture<U> doCompose + private <U> CompletableFuture<U> doThenCompose (Function<? super T, CompletableFuture<U>> fn, Executor e) { if (fn == null) throw new NullPointerException(); CompletableFuture<U> dst = null; - ComposeCompletion<T,U> d = null; + ThenCompose<T,U> d = null; Object r; if ((r = result) == null) { dst = new CompletableFuture<U>(); CompletionNode p = new CompletionNode - (d = new ComposeCompletion<T,U>(this, fn, dst, e)); + (d = new ThenCompose<T,U>(this, fn, dst, e)); while ((r = result) == null) { if (UNSAFE.compareAndSwapObject (this, COMPLETIONS, p.next = completions, p)) @@ -2750,11 +2849,11 @@ } catch (Throwable rex) { ex = rex; } - if (dst == null) - dst = new CompletableFuture<U>(); } } - if (e == null && ex != null) + if (dst == null) + dst = new CompletableFuture<U>(); + if (e == null || ex != null) dst.internalComplete(null, ex); } helpPostComplete(); @@ -2876,12 +2975,14 @@ /** * Returns a new CompletableFuture that is completed when all of * the given CompletableFutures complete. If any of the given - * CompletableFutures complete exceptionally, then so does the - * returned CompletableFuture. Otherwise, the results, if any, of - * the given CompletableFutures are not reflected in the returned - * CompletableFuture, but may be obtained by inspecting them - * individually. If no CompletableFutures are provided, returns a - * CompletableFuture completed with the value {@code null}. + * CompletableFutures complete exceptionally, then the returned + * CompletableFuture also does so, with a CompletionException + * holding this exception as its cause. Otherwise, the results, + * if any, of the given CompletableFutures are not reflected in + * the returned CompletableFuture, but may be obtained by + * inspecting them individually. If no CompletableFutures are + * provided, returns a CompletableFuture completed with the value + * {@code null}. * * <p>Among the applications of this method is to await completion * of a set of independent CompletableFutures before continuing a @@ -2906,12 +3007,12 @@ else if ((f = cfs[0]) == null) throw new NullPointerException(); else { - ThenCopy d = null; + ThenPropagate d = null; CompletionNode p = null; Object r; while ((r = f.result) == null) { if (d == null) - d = new ThenCopy(f, dst); + d = new ThenPropagate(f, dst); else if (p == null) p = new CompletionNode(d); else if (UNSAFE.compareAndSwapObject @@ -2975,35 +3076,37 @@ /** * Returns a new CompletableFuture that is completed when any of - * the given CompletableFutures complete; with the same result if - * it completed normally, otherwise exceptionally. If no - * CompletableFutures are provided, returns an incomplete - * CompletableFuture. + * the given CompletableFutures complete, with the same result. + * Otherwise, if it completed exceptionally, the returned + * CompletableFuture also does so, with a CompletionException + * holding this exception as its cause. If no CompletableFutures + * are provided, returns an incomplete CompletableFuture. * * @param cfs the CompletableFutures - * @return a new CompletableFuture that is completed when any of the - * given CompletableFutures complete + * @return a new CompletableFuture that is completed with the + * result or exception of any of the given CompletableFutures when + * one completes * @throws NullPointerException if the array or any of its elements are * {@code null} */ - public static CompletableFuture<?> anyOf(CompletableFuture<?>... cfs) { + public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { int len = cfs.length; // Same idea as allOf if (len > 1) return anyTree(cfs, 0, len - 1); else { - CompletableFuture<?> dst = new CompletableFuture<Object>(); + CompletableFuture<Object> dst = new CompletableFuture<Object>(); CompletableFuture<?> f; if (len == 0) ; // skip else if ((f = cfs[0]) == null) throw new NullPointerException(); else { - ThenCopy d = null; + ThenCopy<Object> d = null; CompletionNode p = null; Object r; while ((r = f.result) == null) { if (d == null) - d = new ThenCopy(f, dst); + d = new ThenCopy<Object>(f, dst); else if (p == null) p = new CompletionNode(d); else if (UNSAFE.compareAndSwapObject @@ -3031,14 +3134,14 @@ /** * Recursively constructs an Or'ed tree of CompletableFutures. */ - private static CompletableFuture<?> anyTree(CompletableFuture<?>[] cfs, - int lo, int hi) { + private static CompletableFuture<Object> anyTree(CompletableFuture<?>[] cfs, + int lo, int hi) { CompletableFuture<?> fst, snd; int mid = (lo + hi) >>> 1; if ((fst = (lo == mid ? cfs[lo] : anyTree(cfs, lo, mid))) == null || (snd = (hi == mid+1 ? cfs[hi] : anyTree(cfs, mid+1, hi))) == null) throw new NullPointerException(); - CompletableFuture<?> dst = new CompletableFuture<Object>(); + CompletableFuture<Object> dst = new CompletableFuture<Object>(); OrCompletion d = null; CompletionNode p = null, q = null; Object r; @@ -3075,7 +3178,6 @@ return dst; } - /* ------------- Control and status methods -------------- */ /**