-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAsync.java
More file actions
430 lines (380 loc) · 15.7 KB
/
Async.java
File metadata and controls
430 lines (380 loc) · 15.7 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package graphql.execution;
import graphql.Assert;
import graphql.Internal;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
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;
import static graphql.Assert.assertTrue;
import static java.util.stream.Collectors.toList;
@Internal
@SuppressWarnings("FutureReturnValueIgnored")
public class Async {
/**
* A builder of materialized objects or {@link CompletableFuture}s than can present a promise to the list of them
* <p>
* This builder has a strict contract on size whereby if the expectedSize is five, then there MUST be five elements presented to it.
*
* @param <T> for two
*/
public interface CombinedBuilder<T> {
/**
* This adds a {@link CompletableFuture} into the collection of results
*
* @param completableFuture the CF to add
*/
void add(CompletableFuture<T> completableFuture);
/**
* This adds a new value which can be either a materialized value or a {@link CompletableFuture}
*
* @param object the object to add
*/
void addObject(Object object);
/**
* This will return a {@code CompletableFuture<List<T>>} even if the inputs are all materialized values
*
* @return a CompletableFuture to a List of values
*/
CompletableFuture<List<T>> await();
/**
* This will return a {@code CompletableFuture<List<T>>} if ANY of the input values are async
* otherwise it just return a materialised {@code List<T>}
*
* @return either a CompletableFuture or a materialized list
*/
/* CompletableFuture<List<T>> | List<T> */ Object awaitPolymorphic();
}
/**
* Combines zero or more CFs into one. It is a wrapper around <code>CompletableFuture.allOf</code>.
*
* @param expectedSize how many we expect
* @param <T> for two
*
* @return a combined builder of CFs
*/
public static <T> CombinedBuilder<T> ofExpectedSize(int expectedSize) {
if (expectedSize == 0) {
return new Empty<>();
} else if (expectedSize == 1) {
return new Single<>();
} else {
return new Many<>(expectedSize);
}
}
private static class Empty<T> implements CombinedBuilder<T> {
private int ix;
@Override
public void add(CompletableFuture<T> completableFuture) {
this.ix++;
}
@Override
public void addObject(Object object) {
this.ix++;
}
@Override
public CompletableFuture<List<T>> await() {
assertTrue(ix == 0, "expected size was 0 got %d", ix);
return typedEmpty();
}
@Override
public Object awaitPolymorphic() {
Assert.assertTrue(ix == 0, () -> "expected size was " + 0 + " got " + ix);
return Collections.emptyList();
}
// implementation details: infer the type of Completable<List<T>> from a singleton empty
private static final CompletableFuture<List<?>> EMPTY = CompletableFuture.completedFuture(Collections.emptyList());
@SuppressWarnings("unchecked")
private static <T> CompletableFuture<T> typedEmpty() {
return (CompletableFuture<T>) EMPTY;
}
}
private static class Single<T> implements CombinedBuilder<T> {
// avoiding array allocation as there is only 1 CF
private Object value;
private int ix;
@Override
public void add(CompletableFuture<T> completableFuture) {
this.value = completableFuture;
this.ix++;
}
@Override
public void addObject(Object object) {
this.value = object;
this.ix++;
}
@Override
public CompletableFuture<List<T>> await() {
commonSizeAssert();
if (value instanceof CompletableFuture) {
@SuppressWarnings("unchecked")
CompletableFuture<T> cf = (CompletableFuture<T>) value;
return cf.thenApply(Collections::singletonList);
}
//noinspection unchecked
return CompletableFuture.completedFuture(Collections.singletonList((T) value));
}
@Override
public Object awaitPolymorphic() {
commonSizeAssert();
if (value instanceof CompletableFuture) {
@SuppressWarnings("unchecked")
CompletableFuture<T> cf = (CompletableFuture<T>) value;
return cf.thenApply(Collections::singletonList);
}
//noinspection unchecked
return Collections.singletonList((T) value);
}
private void commonSizeAssert() {
Assert.assertTrue(ix == 1, () -> "expected size was " + 1 + " got " + ix);
}
}
private static class Many<T> implements CombinedBuilder<T> {
private final Object[] array;
private int ix;
private int cfCount;
private Many(int size) {
this.array = new Object[size];
this.ix = 0;
cfCount = 0;
}
@Override
public void add(CompletableFuture<T> completableFuture) {
array[ix++] = completableFuture;
cfCount++;
}
@Override
public void addObject(Object object) {
array[ix++] = object;
if (object instanceof CompletableFuture) {
cfCount++;
}
}
@SuppressWarnings("unchecked")
@Override
public CompletableFuture<List<T>> await() {
commonSizeAssert();
CompletableFuture<List<T>> overallResult = new CompletableFuture<>();
if (cfCount == 0) {
overallResult.complete(materialisedList(array));
} else {
CompletableFuture<T>[] cfsArr = copyOnlyCFsToArray();
CompletableFuture.allOf(cfsArr)
.whenComplete((ignored, exception) -> {
if (exception != null) {
overallResult.completeExceptionally(exception);
return;
}
List<T> results = new ArrayList<>(array.length);
if (cfsArr.length == array.length) {
// they are all CFs
for (CompletableFuture<T> cf : cfsArr) {
results.add(cf.join());
}
} else {
// it's a mixed bag of CFs and materialized objects
for (Object object : array) {
if (object instanceof CompletableFuture) {
CompletableFuture<T> cf = (CompletableFuture<T>) object;
// join is safe since they are all completed earlier via CompletableFuture.allOf()
results.add(cf.join());
} else {
results.add((T) object);
}
}
}
overallResult.complete(results);
});
}
return overallResult;
}
@SuppressWarnings("unchecked")
@NonNull
private CompletableFuture<T>[] copyOnlyCFsToArray() {
if (cfCount == array.length) {
// if it's all CFs - make a type safe copy via C code
return Arrays.copyOf(array, array.length, CompletableFuture[].class);
} else {
int i = 0;
CompletableFuture<T>[] dest = new CompletableFuture[cfCount];
for (Object o : array) {
if (o instanceof CompletableFuture) {
dest[i] = (CompletableFuture<T>) o;
i++;
}
}
return dest;
}
}
@Override
public Object awaitPolymorphic() {
if (cfCount == 0) {
commonSizeAssert();
return materialisedList(array);
} else {
return await();
}
}
@NonNull
@SuppressWarnings("unchecked")
private List<T> materialisedList(Object[] array) {
return (List<T>) Arrays.asList(array);
}
private void commonSizeAssert() {
Assert.assertTrue(ix == array.length, () -> "expected size was " + array.length + " got " + ix);
}
}
@SuppressWarnings("unchecked")
public static <T, U> CompletableFuture<List<U>> each(Collection<T> list, Function<T, Object> cfOrMaterialisedValueFactory) {
Object l = eachPolymorphic(list, cfOrMaterialisedValueFactory);
if (l instanceof CompletableFuture) {
return (CompletableFuture<List<U>>) l;
} else {
return CompletableFuture.completedFuture((List<U>) l);
}
}
/**
* This will run the value factory for each of the values in the provided list.
* <p>
* If any of the values provided is a {@link CompletableFuture} it will return a {@link CompletableFuture} result object
* that joins on all values otherwise if none of the values are a {@link CompletableFuture} then it will return a materialized list.
*
* @param list the list to work over
* @param cfOrMaterialisedValueFactory the value factory to call for each iterm in the list
* @param <T> for two
*
* @return a {@link CompletableFuture} to the list of resolved values or the list of values in a materialized fashion
*/
public static <T> /* CompletableFuture<List<U>> | List<U> */ Object eachPolymorphic(Collection<T> list, Function<T, Object> cfOrMaterialisedValueFactory) {
CombinedBuilder<Object> futures = ofExpectedSize(list.size());
for (T t : list) {
try {
Object value = cfOrMaterialisedValueFactory.apply(t);
futures.addObject(value);
} catch (Exception e) {
CompletableFuture<Object> 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 futures.awaitPolymorphic();
}
public static <T, U> CompletableFuture<List<U>> eachSequentially(Iterable<T> list, BiFunction<T, List<U>, Object> cfOrMaterialisedValueFactory) {
CompletableFuture<List<U>> result = new CompletableFuture<>();
eachSequentiallyPolymorphicImpl(list.iterator(), cfOrMaterialisedValueFactory, new ArrayList<>(), result);
return result;
}
@SuppressWarnings("unchecked")
private static <T, U> void eachSequentiallyPolymorphicImpl(Iterator<T> iterator, BiFunction<T, List<U>, Object> cfOrMaterialisedValueFactory, List<U> tmpResult, CompletableFuture<List<U>> overallResult) {
if (!iterator.hasNext()) {
overallResult.complete(tmpResult);
return;
}
Object value;
try {
value = cfOrMaterialisedValueFactory.apply(iterator.next(), tmpResult);
} catch (Exception e) {
overallResult.completeExceptionally(new CompletionException(e));
return;
}
if (value instanceof CompletableFuture) {
CompletableFuture<U> cf = (CompletableFuture<U>) value;
cf.whenComplete((cfResult, exception) -> {
if (exception != null) {
overallResult.completeExceptionally(exception);
return;
}
tmpResult.add(cfResult);
eachSequentiallyPolymorphicImpl(iterator, cfOrMaterialisedValueFactory, tmpResult, overallResult);
});
} else {
tmpResult.add((U) value);
eachSequentiallyPolymorphicImpl(iterator, cfOrMaterialisedValueFactory, tmpResult, overallResult);
}
}
/**
* Turns an object T into a CompletableFuture if it's not already
*
* @param t - the object to check
* @param <T> for two
*
* @return a CompletableFuture
*/
@SuppressWarnings("unchecked")
public static <T> CompletableFuture<T> toCompletableFuture(Object t) {
if (t instanceof CompletionStage) {
return ((CompletionStage<T>) t).toCompletableFuture();
} else {
return CompletableFuture.completedFuture((T) t);
}
}
/**
* Turns a CompletionStage into a CompletableFuture if it's not already, otherwise leaves it alone
* as a materialized object.
*
* @param object - the object to check
*
* @return a CompletableFuture from a CompletionStage or the materialized object itself
*/
public static Object toCompletableFutureOrMaterializedObject(Object object) {
if (object instanceof CompletionStage) {
return ((CompletionStage<?>) object).toCompletableFuture();
} else {
return object;
}
}
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;
}
/**
* If the passed in CompletableFuture is null, then it creates a CompletableFuture that resolves to null
*
* @param completableFuture the CF to use
* @param <T> for two
*
* @return the completableFuture if it's not null or one that always resoles to null
*/
public static <T> @NonNull CompletableFuture<T> orNullCompletedFuture(@Nullable CompletableFuture<T> completableFuture) {
return completableFuture != null ? completableFuture : CompletableFuture.completedFuture(null);
}
public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> cfs) {
return CompletableFuture.allOf(cfs.toArray(CompletableFuture[]::new))
.thenApply(v -> cfs.stream()
.map(CompletableFuture::join)
.collect(toList())
);
}
public static <K, V> CompletableFuture<Map<K, V>> allOf(Map<K, CompletableFuture<V>> cfs) {
return CompletableFuture.allOf(cfs.values().toArray(CompletableFuture[]::new))
.thenApply(v -> cfs.entrySet().stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
task -> task.getValue().join())
)
);
}
}