22
33import org .junit .Test ;
44
5- import java .util .concurrent .CompletableFuture ;
6- import java .util .concurrent .ForkJoinPool ;
7- import java .util .concurrent .ThreadLocalRandom ;
8- import java .util .concurrent .TimeUnit ;
5+ import java .util .Optional ;
6+ import java .util .concurrent .*;
7+
8+ import static org .junit .Assert .assertEquals ;
9+ import static org .junit .Assert .assertTrue ;
910
1011public class Methods {
1112
@@ -22,6 +23,14 @@ private Integer slowTask() {
2223
2324 @ Test
2425 public void create () {
26+ final CompletableFuture <Integer > future = new CompletableFuture <>();
27+ final CompletableFuture <Integer > completedFuture = CompletableFuture .completedFuture (1 );
28+
29+ final CompletableFuture <Integer > supplied = CompletableFuture .supplyAsync (this ::slowTask );
30+ final CompletableFuture <Integer > supplied2 = CompletableFuture .supplyAsync (this ::slowTask , customExecutor );
31+
32+ final CompletableFuture <Void > runAsync = CompletableFuture .runAsync (this ::slowTask );
33+ final CompletableFuture <Void > runAsync2 = CompletableFuture .runAsync (this ::slowTask , customExecutor );
2534 // new
2635 // supplyAsync
2736 // runAsync
@@ -31,21 +40,64 @@ public void create() {
3140 @ Test
3241 public void write () {
3342 // complete (completed)
43+ final CompletableFuture <Integer > future = new CompletableFuture <>();
44+ future .complete (1 );
45+
46+ final CompletableFuture <Integer > future2 = new CompletableFuture <>();
47+ //new Thread(() -> future2.complete(slowTask())).start();
48+ ForkJoinPool .commonPool ().submit (() -> {
49+ try {
50+ final Integer result = slowTask ();
51+ future2 .complete (result );
52+ } catch (Exception e ) {
53+ future2 .completeExceptionally (e );
54+ }
55+ });
56+
57+ final CompletableFuture <Void > voidFuture = new CompletableFuture <>();
58+ ForkJoinPool .commonPool ().submit (() -> {
59+ try {
60+ slowTask ();
61+ voidFuture .complete (null );
62+ } catch (Exception e ) {
63+ voidFuture .completeExceptionally (e );
64+ }
65+ });
66+
3467 // completeExceptionally
3568 }
3669
3770 @ Test
38- public void rewrite () {
71+ public void rewrite () throws ExecutionException , InterruptedException {
3972 // obtrudeValue
4073 // obtrudeException
74+ final CompletableFuture <Integer > future = CompletableFuture .completedFuture (1 );
75+ assertEquals (Integer .valueOf (1 ), future .get ());
76+
77+ future .obtrudeValue (2 );
78+ assertEquals (Integer .valueOf (2 ), future .get ());
4179 }
4280
4381 @ Test
4482 public void read () {
83+ final CompletableFuture <Integer > future = CompletableFuture .completedFuture (1 );
84+ final CompletableFuture <Integer > future2 = new CompletableFuture <>();
85+ future2 .completeExceptionally (new UnsupportedOperationException ());
4586 // isDone
87+ assertTrue (future .isDone ());
88+ assertTrue (future2 .isDone ());
4689 // get
90+ try {
91+ assertEquals (Integer .valueOf (1 ), future .get ());
92+ } catch (InterruptedException e ) {
93+ e .printStackTrace ();
94+ } catch (ExecutionException e ) {
95+ e .printStackTrace ();
96+ }
4797 // join
98+ assertEquals (Integer .valueOf (1 ), future .join ());
4899 // getNow
100+ future .getNow (0 );
49101 // getNumberOfDependents
50102 }
51103
@@ -56,9 +108,19 @@ public void foreachMapFlatMap() {
56108 final CompletableFuture <String > future2 = null ;
57109
58110 // forEach
111+ final CompletableFuture <Void > thenAccept = future .thenAccept (System .out ::println );
112+ final CompletableFuture <Void > thenAcceptAsync = future .thenAcceptAsync (System .out ::println );
113+ final CompletableFuture <Void > thenAcceptAsync2 = future .thenAcceptAsync (System .out ::println , customExecutor );
114+ final CompletableFuture <Void > thenRun = future .thenRun (() -> System .out .println ("Done" ));
59115 // map
116+ final CompletableFuture <String > thenApply = future .thenApply (Object ::toString );
60117 // flatMap
118+ final CompletableFuture <Integer > thenCompose = future .thenCompose (i -> CompletableFuture .completedFuture (i + 1 ));
119+ final CompletableFuture <Integer > thenComposeAsync = future .thenComposeAsync (i -> CompletableFuture .completedFuture (i + 1 ));
61120 // *3
121+ // *Async
122+
123+ final CompletableFuture <Integer > sum = future .thenCompose (i -> future1 .thenApply (i1 -> i + i1 ));
62124 }
63125
64126 @ Test
@@ -68,12 +130,31 @@ public void allAnyOf() {
68130 final CompletableFuture <String > future2 = null ;
69131
70132 // All of
133+ final CompletableFuture <Void > allOf = CompletableFuture .allOf (future , future1 , future2 );
134+ allOf .thenRun (() -> {
135+ final Integer i = future .join ();
136+ final String s = future2 .join ();
137+ });
138+
139+
71140 // Any of
141+ final CompletableFuture <Object > anyOf = CompletableFuture .anyOf (future , future1 );
142+ anyOf .thenRun (() -> {
143+ if (future .isDone ()) {
144+ future .join ();
145+ } else {
146+ future1 .join ();
147+ }
148+ });
149+
72150
73151 // any of
74- // acceptEither
75152 // applyToEither
153+ final CompletableFuture <Integer > applyToEither = future .applyToEither (future1 , i -> i + 1 );
154+ // acceptEither
155+ final CompletableFuture <Void > acceptEither = future .acceptEither (future1 , System .out ::println );
76156 // runAfterEither
157+ final CompletableFuture <Void > runAfterEither = future .runAfterEither (future1 , () -> System .out .println ("Done!" ));
77158
78159 // all of
79160 // thenCombine
@@ -82,4 +163,27 @@ public void allAnyOf() {
82163
83164 }
84165
166+ @ Test
167+ public void recover () {
168+ final CompletableFuture <String > future = new CompletableFuture <>();
169+ // exceptionally
170+ final CompletableFuture <Optional <String >> exceptionally =
171+ future .thenApply (Optional ::of ).exceptionally (e -> {
172+ e .printStackTrace ();
173+ return Optional .empty ();
174+ });
175+
176+ // handle
177+ final CompletableFuture <Optional <?>> handle =
178+ future .handle ((s , e ) -> {
179+ if (e != null ) {
180+ e .printStackTrace ();
181+ return Optional .empty ();
182+ } else {
183+ return Optional .of (s );
184+ }
185+ });
186+
187+ }
188+
85189}
0 commit comments