3964 |
|
.collect(Collectors.toList()); |
3965 |
|
|
3966 |
|
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3967 |
< |
stages.add(new CompletableFuture<Integer>().minimalCompletionStage()); |
3967 |
> |
CompletionStage<Integer> min = |
3968 |
> |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3969 |
> |
stages.add(min); |
3970 |
> |
stages.add(min.thenApply(x -> x)); |
3971 |
|
stages.add(CompletableFuture.completedStage(1)); |
3972 |
|
stages.add(CompletableFuture.failedStage(new CFException())); |
3973 |
|
|
4003 |
|
throw new Error("Methods did not throw UOE: " + bugs); |
4004 |
|
} |
4005 |
|
|
4006 |
+ |
/** |
4007 |
+ |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4008 |
+ |
*/ |
4009 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4010 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4011 |
+ |
{ |
4012 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4013 |
+ |
CompletionStage minimal = f.minimalCompletionStage(); |
4014 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4015 |
+ |
g.complete(v1); |
4016 |
+ |
checkCompletedNormally(g, v1); |
4017 |
+ |
checkIncomplete(f); |
4018 |
+ |
checkIncomplete(minimal.toCompletableFuture()); |
4019 |
+ |
}} |
4020 |
+ |
|
4021 |
+ |
/** |
4022 |
+ |
* minimalStage.toCompletableFuture().join() awaits completion |
4023 |
+ |
*/ |
4024 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4025 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4026 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4027 |
+ |
{ |
4028 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4029 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4030 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4031 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4032 |
+ |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4033 |
+ |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4034 |
+ |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4035 |
+ |
}} |
4036 |
+ |
|
4037 |
+ |
/** Demo utility method for external reliable toCompletableFuture */ |
4038 |
+ |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4039 |
+ |
CompletableFuture<T> f = new CompletableFuture<>(); |
4040 |
+ |
stage.handle((T t, Throwable ex) -> { |
4041 |
+ |
if (ex != null) f.completeExceptionally(ex); |
4042 |
+ |
else f.complete(t); |
4043 |
+ |
return null; |
4044 |
+ |
}); |
4045 |
+ |
return f; |
4046 |
+ |
} |
4047 |
+ |
|
4048 |
+ |
/** Demo utility method to join a CompletionStage */ |
4049 |
+ |
static <T> T join(CompletionStage<T> stage) { |
4050 |
+ |
return toCompletableFuture(stage).join(); |
4051 |
+ |
} |
4052 |
+ |
|
4053 |
+ |
/** |
4054 |
+ |
* Joining a minimal stage "by hand" works |
4055 |
+ |
*/ |
4056 |
+ |
public void testMinimalCompletionStage_join_by_hand() { |
4057 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4058 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4059 |
+ |
{ |
4060 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4061 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4062 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4063 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4064 |
+ |
minimal.thenAccept((x) -> g.complete(x)); |
4065 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4066 |
+ |
g.join(); |
4067 |
+ |
checkCompletedNormally(g, v1); |
4068 |
+ |
checkCompletedNormally(f, v1); |
4069 |
+ |
assertEquals(v1, join(minimal)); |
4070 |
+ |
}} |
4071 |
+ |
|
4072 |
|
static class Monad { |
4073 |
|
static class ZeroException extends RuntimeException { |
4074 |
|
public ZeroException() { super("monadic zero"); } |