forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsync.java
More file actions
188 lines (164 loc) · 7.02 KB
/
Async.java
File metadata and controls
188 lines (164 loc) · 7.02 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package graphql.execution;
import graphql.Assert;
import graphql.Internal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@Internal
@SuppressWarnings("FutureReturnValueIgnored")
public class Async {
@FunctionalInterface
public interface CFFactory<T, U> {
CompletableFuture<U> apply(T input, int index, List<U> previousResults);
}
public static <U> CompletableFuture<List<U>> each(List<CompletableFuture<U>> futures) {
CompletableFuture<List<U>> overallResult = new CompletableFuture<>();
CompletableFuture
.allOf(futures.toArray(new CompletableFuture[0]))
.whenComplete((noUsed, exception) -> {
if (exception != null) {
overallResult.completeExceptionally(exception);
return;
}
List<U> results = new ArrayList<>();
for (CompletableFuture<U> future : futures) {
results.add(future.join());
}
overallResult.complete(results);
});
return overallResult;
}
public static <T, U> CompletableFuture<List<U>> each(Iterable<T> list, BiFunction<T, Integer, CompletableFuture<U>> cfFactory) {
List<CompletableFuture<U>> futures = new ArrayList<>();
int index = 0;
for (T t : list) {
CompletableFuture<U> cf;
try {
cf = cfFactory.apply(t, index++);
Assert.assertNotNull(cf, "cfFactory must return a non null value");
} catch (Exception e) {
cf = new CompletableFuture<>();
// Async.each makes sure that it is not a CompletionException inside a CompletionException
cf.completeExceptionally(new CompletionException(e));
}
futures.add(cf);
}
return each(futures);
}
public static <T, U> CompletableFuture<List<U>> eachSequentially(Iterable<T> list, CFFactory<T, U> cfFactory) {
CompletableFuture<List<U>> result = new CompletableFuture<>();
eachSequentiallyImpl(list.iterator(), cfFactory, 0, new ArrayList<>(), result);
return result;
}
private static <T, U> void eachSequentiallyImpl(Iterator<T> iterator, CFFactory<T, U> cfFactory, int index, List<U> tmpResult, CompletableFuture<List<U>> overallResult) {
if (!iterator.hasNext()) {
overallResult.complete(tmpResult);
return;
}
CompletableFuture<U> cf;
try {
cf = cfFactory.apply(iterator.next(), index, tmpResult);
Assert.assertNotNull(cf, "cfFactory must return a non null value");
} catch (Exception e) {
cf = new CompletableFuture<>();
cf.completeExceptionally(new CompletionException(e));
}
cf.whenComplete((cfResult, exception) -> {
if (exception != null) {
overallResult.completeExceptionally(exception);
return;
}
tmpResult.add(cfResult);
eachSequentiallyImpl(iterator, cfFactory, index + 1, tmpResult, overallResult);
});
}
/**
* Turns an object T into a CompletableFuture if its not already
*
* @param t - the object to check
* @param <T> for two
*
* @return a CompletableFuture
*/
public static <T> CompletableFuture<T> toCompletableFuture(T t) {
if (t instanceof CompletionStage) {
//noinspection unchecked
return ((CompletionStage<T>) t).toCompletableFuture();
} else {
return CompletableFuture.completedFuture(t);
}
}
public static <T> CompletableFuture<T> tryCatch(Supplier<CompletableFuture<T>> supplier) {
try {
return supplier.get();
} catch (Exception e) {
CompletableFuture<T> result = new CompletableFuture<>();
result.completeExceptionally(e);
return result;
}
}
public static <T> CompletableFuture<T> exceptionallyCompletedFuture(Throwable exception) {
CompletableFuture<T> result = new CompletableFuture<>();
result.completeExceptionally(exception);
return result;
}
public static <T> void copyResults(CompletableFuture<T> source, CompletableFuture<T> target) {
source.whenComplete((o, throwable) -> {
if (throwable != null) {
target.completeExceptionally(throwable);
return;
}
target.complete(o);
});
}
public static <U, T> CompletableFuture<U> reduce(List<CompletableFuture<T>> values, U initialValue, BiFunction<U, T, U> aggregator) {
CompletableFuture<U> result = new CompletableFuture<>();
reduceImpl(values, 0, initialValue, aggregator, result);
return result;
}
public static <U, T> CompletableFuture<U> reduce(CompletableFuture<List<T>> values, U initialValue, BiFunction<U, T, U> aggregator) {
return values.thenApply(list -> {
U result = initialValue;
for (T value : list) {
result = aggregator.apply(result, value);
}
return result;
});
}
public static <U, T> CompletableFuture<List<U>> flatMap(List<T> inputs, Function<T, CompletableFuture<U>> mapper) {
List<CompletableFuture<U>> collect = inputs
.stream()
.map(mapper)
.collect(Collectors.toList());
return Async.each(collect);
}
private static <U, T> void reduceImpl(List<CompletableFuture<T>> values, int curIndex, U curValue, BiFunction<U, T, U> aggregator, CompletableFuture<U> result) {
if (curIndex == values.size()) {
result.complete(curValue);
return;
}
values.get(curIndex).
thenApply(oneValue -> aggregator.apply(curValue, oneValue))
.thenAccept(newValue -> reduceImpl(values, curIndex + 1, newValue, aggregator, result));
}
public static <U, T> CompletableFuture<List<U>> map(CompletableFuture<List<T>> values, Function<T, U> mapper) {
return values.thenApply(list -> list.stream().map(mapper).collect(Collectors.toList()));
}
public static <U, T> List<CompletableFuture<U>> map(List<CompletableFuture<T>> values, Function<T, U> mapper) {
return values
.stream()
.map(cf -> cf.thenApply(mapper::apply)).collect(Collectors.toList());
}
public static <U, T> List<CompletableFuture<U>> mapCompose(List<CompletableFuture<T>> values, Function<T, CompletableFuture<U>> mapper) {
return values
.stream()
.map(cf -> cf.thenCompose(mapper::apply)).collect(Collectors.toList());
}
}