blob: b1fa6a9438d908b9d66d8ef88a511d369da293df (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Helper for {@link java.util.concurrent.CompletableFuture} / {@link java.util.concurrent.CompletionStage}.
*
* @author bjorncs
*/
public class CompletableFutures {
private CompletableFutures() {}
/**
* Returns a new completable future that is either
* - completed when any of the provided futures complete without exception
* - completed exceptionally once all provided futures complete exceptionally
*/
public static <T> CompletableFuture<T> firstOf(List<CompletableFuture<T>> futures) {
class Combiner {
final Object monitor = new Object();
final CompletableFuture<T> combined = new CompletableFuture<>();
final int futuresCount;
Throwable error = null;
int exceptionCount = 0;
Combiner(int futuresCount) { this.futuresCount = futuresCount; }
void onCompletion(T value, Throwable error) {
if (combined.isDone()) return;
T valueToComplete = null;
Throwable exceptionToComplete = null;
synchronized (monitor) {
if (value != null) {
valueToComplete = value;
} else {
if (this.error == null) {
this.error = error;
} else {
this.error.addSuppressed(error);
}
if (++exceptionCount == futuresCount) {
exceptionToComplete = this.error;
}
}
}
if (valueToComplete != null) {
combined.complete(value);
} else if (exceptionToComplete != null) {
combined.completeExceptionally(exceptionToComplete);
}
}
}
int size = futures.size();
if (size == 0) throw new IllegalArgumentException();
if (size == 1) return futures.get(0);
Combiner combiner = new Combiner(size);
futures.forEach(future -> future.whenComplete(combiner::onCompletion));
return combiner.combined;
}
}
|