334 |
|
return (x == null) ? null : x + 1; |
335 |
|
} |
336 |
|
|
337 |
< |
static final Supplier<Integer> supplyOne = |
338 |
< |
() -> Integer.valueOf(1); |
339 |
< |
static final Function<Integer, Integer> inc = |
340 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
341 |
< |
static final BiFunction<Integer, Integer, Integer> subtract = |
342 |
< |
(Integer x, Integer y) -> subtract(x, y); |
337 |
> |
static final class IntegerSupplier implements Supplier<Integer> { |
338 |
> |
final ExecutionMode m; |
339 |
> |
int invocationCount = 0; |
340 |
> |
final Integer value; |
341 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
342 |
> |
this.m = m; |
343 |
> |
this.value = value; |
344 |
> |
} |
345 |
> |
public Integer get() { |
346 |
> |
m.checkExecutionMode(); |
347 |
> |
invocationCount++; |
348 |
> |
return value; |
349 |
> |
} |
350 |
> |
} |
351 |
> |
|
352 |
|
static final class IncAction implements Consumer<Integer> { |
353 |
|
int invocationCount = 0; |
354 |
|
Integer value; |
515 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
516 |
|
throw new UnsupportedOperationException(); |
517 |
|
} |
518 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
519 |
+ |
throw new UnsupportedOperationException(); |
520 |
+ |
} |
521 |
|
public <T> CompletableFuture<Void> thenRun |
522 |
|
(CompletableFuture<T> f, Runnable a) { |
523 |
|
return f.thenRun(a); |
589 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
590 |
|
return CompletableFuture.runAsync(a); |
591 |
|
} |
592 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
593 |
+ |
return CompletableFuture.supplyAsync(a); |
594 |
+ |
} |
595 |
|
public <T> CompletableFuture<Void> thenRun |
596 |
|
(CompletableFuture<T> f, Runnable a) { |
597 |
|
return f.thenRunAsync(a); |
662 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
663 |
|
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
664 |
|
} |
665 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
666 |
+ |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
667 |
+ |
} |
668 |
|
public <T> CompletableFuture<Void> thenRun |
669 |
|
(CompletableFuture<T> f, Runnable a) { |
670 |
|
return f.thenRunAsync(a, new ThreadExecutor()); |
730 |
|
|
731 |
|
public abstract void checkExecutionMode(); |
732 |
|
public abstract CompletableFuture<Void> runAsync(Runnable a); |
733 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
734 |
|
public abstract <T> CompletableFuture<Void> thenRun |
735 |
|
(CompletableFuture<T> f, Runnable a); |
736 |
|
public abstract <T> CompletableFuture<Void> thenAccept |
1017 |
|
/** |
1018 |
|
* supplyAsync completes with result of supplier |
1019 |
|
*/ |
1020 |
< |
public void testSupplyAsync() { |
1021 |
< |
CompletableFuture<Integer> f; |
1022 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1023 |
< |
assertEquals(f.join(), one); |
1024 |
< |
checkCompletedNormally(f, one); |
1025 |
< |
} |
1026 |
< |
|
1027 |
< |
/** |
1028 |
< |
* supplyAsync with executor completes with result of supplier |
1029 |
< |
*/ |
1030 |
< |
public void testSupplyAsync2() { |
1031 |
< |
CompletableFuture<Integer> f; |
1032 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1033 |
< |
assertEquals(f.join(), one); |
1015 |
< |
checkCompletedNormally(f, one); |
1016 |
< |
} |
1020 |
> |
public void testSupplyAsync_normalCompletion() { |
1021 |
> |
ExecutionMode[] executionModes = { |
1022 |
> |
ExecutionMode.ASYNC, |
1023 |
> |
ExecutionMode.EXECUTOR, |
1024 |
> |
}; |
1025 |
> |
for (ExecutionMode m : executionModes) |
1026 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1027 |
> |
{ |
1028 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1029 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1030 |
> |
assertSame(v1, f.join()); |
1031 |
> |
checkCompletedNormally(f, v1); |
1032 |
> |
assertEquals(1, r.invocationCount); |
1033 |
> |
}} |
1034 |
|
|
1035 |
|
/** |
1036 |
|
* Failing supplyAsync completes exceptionally |
1037 |
|
*/ |
1038 |
< |
public void testSupplyAsync3() { |
1039 |
< |
FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC); |
1040 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1038 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1039 |
> |
ExecutionMode[] executionModes = { |
1040 |
> |
ExecutionMode.ASYNC, |
1041 |
> |
ExecutionMode.EXECUTOR, |
1042 |
> |
}; |
1043 |
> |
for (ExecutionMode m : executionModes) |
1044 |
> |
{ |
1045 |
> |
FailingSupplier r = new FailingSupplier(m); |
1046 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1047 |
|
checkCompletedWithWrappedCFException(f); |
1048 |
|
assertEquals(1, r.invocationCount); |
1049 |
< |
} |
1049 |
> |
}} |
1050 |
|
|
1051 |
|
// seq completion methods |
1052 |
|
|
2686 |
|
Runnable[] throwingActions = { |
2687 |
|
() -> CompletableFuture.supplyAsync(null), |
2688 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
2689 |
< |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
2689 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
2690 |
|
|
2691 |
|
() -> CompletableFuture.runAsync(null), |
2692 |
|
() -> CompletableFuture.runAsync(null, exec), |