3826 |
|
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3827 |
|
} |
3828 |
|
|
3829 |
< |
// Monadic "plus" |
3829 |
> |
/** Implements "monadic plus". */ |
3830 |
|
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3831 |
|
CompletableFuture<? extends T> g) { |
3832 |
|
PlusFuture<T> plus = new PlusFuture<T>(); |
3833 |
|
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3834 |
< |
if (ex == null) { |
3835 |
< |
if (plus.complete(result)) |
3836 |
< |
if (plus.firstFailure.get() != null) |
3834 |
> |
try { |
3835 |
> |
if (ex == null) { |
3836 |
> |
if (plus.complete(result)) |
3837 |
> |
if (plus.firstFailure.get() != null) |
3838 |
> |
plus.firstFailure.set(null); |
3839 |
> |
} |
3840 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3841 |
> |
if (plus.isDone()) |
3842 |
|
plus.firstFailure.set(null); |
3843 |
< |
} |
3844 |
< |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3845 |
< |
if (plus.isDone()) |
3846 |
< |
plus.firstFailure.set(null); |
3842 |
< |
} |
3843 |
< |
else { |
3844 |
< |
// first failure has precedence |
3845 |
< |
Throwable first = plus.firstFailure.getAndSet(null); |
3843 |
> |
} |
3844 |
> |
else { |
3845 |
> |
// first failure has precedence |
3846 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
3847 |
|
|
3848 |
< |
// may fail with "Self-suppression not permitted" |
3849 |
< |
try { first.addSuppressed(ex); } |
3850 |
< |
catch (Exception ignored) {} |
3848 |
> |
// may fail with "Self-suppression not permitted" |
3849 |
> |
try { first.addSuppressed(ex); } |
3850 |
> |
catch (Exception ignored) {} |
3851 |
|
|
3852 |
< |
plus.completeExceptionally(first); |
3852 |
> |
plus.completeExceptionally(first); |
3853 |
> |
} |
3854 |
> |
} catch (Throwable unexpected) { |
3855 |
> |
plus.completeExceptionally(unexpected); |
3856 |
|
} |
3857 |
|
}; |
3858 |
|
f.whenComplete(action); |