1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.concurrency.CompletionStage;
13 
14 // import hunt.util.functional.BiConsumer;
15 // import hunt.util.functional.BiFunction;
16 // import hunt.util.functional.Consumer;
17 // import hunt.util.functional;
18 
19 /**
20  * A stage of a possibly asynchronous computation, that performs an
21  * action or computes a value when another CompletionStage completes.
22  * A stage completes upon termination of its computation, but this may
23  * in turn trigger other dependent stages.  The functionality defined
24  * in this interface takes only a few basic forms, which expand out to
25  * a larger set of methods to capture a range of usage styles:
26  *
27  * <ul>
28  *
29  * <li>The computation performed by a stage may be expressed as a
30  * Function, Consumer, or Runnable (using methods with names including
31  * <em>apply</em>, <em>accept</em>, or <em>run</em>, respectively)
32  * depending on whether it requires arguments and/or produces results.
33  * For example:
34  * <pre> {@code
35  * stage.thenApply(x -> square(x))
36  *      .thenAccept(x -> System.out.print(x))
37  *      .thenRun(() -> System.out.println());}</pre>
38  *
39  * An additional form (<em>compose</em>) allows the construction of
40  * computation pipelines from functions returning completion stages.
41  *
42  * <p>Any argument to a stage's computation is the outcome of a
43  * triggering stage's computation.
44  *
45  * <li>One stage's execution may be triggered by completion of a
46  * single stage, or both of two stages, or either of two stages.
47  * Dependencies on a single stage are arranged using methods with
48  * prefix <em>then</em>. Those triggered by completion of
49  * <em>both</em> of two stages may <em>combine</em> their results or
50  * effects, using correspondingly named methods. Those triggered by
51  * <em>either</em> of two stages make no guarantees about which of the
52  * results or effects are used for the dependent stage's computation.
53  *
54  * <li>Dependencies among stages control the triggering of
55  * computations, but do not otherwise guarantee any particular
56  * ordering. Additionally, execution of a new stage's computations may
57  * be arranged in any of three ways: default execution, default
58  * asynchronous execution (using methods with suffix <em>async</em>
59  * that employ the stage's default asynchronous execution facility),
60  * or custom (via a supplied {@link Executor}).  The execution
61  * properties of default and async modes are specified by
62  * CompletionStage implementations, not this interface. Methods with
63  * explicit Executor arguments may have arbitrary execution
64  * properties, and might not even support concurrent execution, but
65  * are arranged for processing in a way that accommodates asynchrony.
66  *
67  * <li>Two method forms ({@link #handle handle} and {@link
68  * #whenComplete whenComplete}) support unconditional computation
69  * whether the triggering stage completed normally or exceptionally.
70  * Method {@link #exceptionally exceptionally} supports computation
71  * only when the triggering stage completes exceptionally, computing a
72  * replacement result, similarly to the java {@code catch} keyword.
73  * In all other cases, if a stage's computation terminates abruptly
74  * with an (unchecked) exception or error, then all dependent stages
75  * requiring its completion complete exceptionally as well, with a
76  * {@link CompletionException} holding the exception as its cause.  If
77  * a stage is dependent on <em>both</em> of two stages, and both
78  * complete exceptionally, then the CompletionException may correspond
79  * to either one of these exceptions.  If a stage is dependent on
80  * <em>either</em> of two others, and only one of them completes
81  * exceptionally, no guarantees are made about whether the dependent
82  * stage completes normally or exceptionally. In the case of method
83  * {@code whenComplete}, when the supplied action itself encounters an
84  * exception, then the stage completes exceptionally with this
85  * exception unless the source stage also completed exceptionally, in
86  * which case the exceptional completion from the source stage is
87  * given preference and propagated to the dependent stage.
88  *
89  * </ul>
90  *
91  * <p>All methods adhere to the above triggering, execution, and
92  * exceptional completion specifications (which are not repeated in
93  * individual method specifications). Additionally, while arguments
94  * used to pass a completion result (that is, for parameters of type
95  * {@code T}) for methods accepting them may be null, passing a null
96  * value for any other parameter will result in a {@link
97  * NullPointerException} being thrown.
98  *
99  * <p>Method form {@link #handle handle} is the most general way of
100  * creating a continuation stage, unconditionally performing a
101  * computation that is given both the result and exception (if any) of
102  * the triggering CompletionStage, and computing an arbitrary result.
103  * Method {@link #whenComplete whenComplete} is similar, but preserves
104  * the result of the triggering stage instead of computing a new one.
105  * Because a stage's normal result may be {@code null}, both methods
106  * should have a computation structured thus:
107  *
108  * <pre>{@code (result, exception) -> {
109  *   if (exception is null) {
110  *     // triggering stage completed normally
111  *   } else {
112  *     // triggering stage completed exceptionally
113  *   }
114  * }}</pre>
115  *
116  * <p>This interface does not define methods for initially creating,
117  * forcibly completing normally or exceptionally, probing completion
118  * status or results, or awaiting completion of a stage.
119  * Implementations of CompletionStage may provide means of achieving
120  * such effects, as appropriate.  Method {@link #toCompletableFuture}
121  * enables interoperability among different implementations of this
122  * interface by providing a common conversion type.
123  *
124  * @author Doug Lea
125  * @since 1.8
126  */
127 interface CompletionStage(T) {
128 
129     /**
130      * Returns a new CompletionStage that, when this stage completes
131      * normally, is executed with this stage's result as the argument
132      * to the supplied function.
133      *
134      * <p>This method is analogous to
135      * {@link java.util.Optional#map Optional.map} and
136      * {@link java.util.stream.Stream#map Stream.map}.
137      *
138      * <p>See the {@link CompletionStage} documentation for rules
139      * covering exceptional completion.
140      *
141      * @param fn the function to use to compute the value of the
142      * returned CompletionStage
143      * @param (U) the function's return type
144      * @return the new CompletionStage
145      */
146     CompletionStage!(U) thenApply(U)(Function!(T, U) fn);
147 
148     /**
149      * Returns a new CompletionStage that, when this stage completes
150      * normally, is executed using this stage's default asynchronous
151      * execution facility, with this stage's result as the argument to
152      * the supplied function.
153      *
154      * See the {@link CompletionStage} documentation for rules
155      * covering exceptional completion.
156      *
157      * @param fn the function to use to compute the value of the
158      * returned CompletionStage
159      * @param (U) the function's return type
160      * @return the new CompletionStage
161      */
162     CompletionStage!(U) thenApplyAsync(U)(Function!(T, U) fn);
163 
164     /**
165      * Returns a new CompletionStage that, when this stage completes
166      * normally, is executed using the supplied Executor, with this
167      * stage's result as the argument to the supplied function.
168      *
169      * See the {@link CompletionStage} documentation for rules
170      * covering exceptional completion.
171      *
172      * @param fn the function to use to compute the value of the
173      * returned CompletionStage
174      * @param executor the executor to use for asynchronous execution
175      * @param (U) the function's return type
176      * @return the new CompletionStage
177      */
178     CompletionStage!(U) thenApplyAsync(U)(Function!(T, U) fn,
179          Executor executor);
180 
181     /**
182      * Returns a new CompletionStage that, when this stage completes
183      * normally, is executed with this stage's result as the argument
184      * to the supplied action.
185      *
186      * See the {@link CompletionStage} documentation for rules
187      * covering exceptional completion.
188      *
189      * @param action the action to perform before completing the
190      * returned CompletionStage
191      * @return the new CompletionStage
192      */
193     CompletionStage!(void) thenAccept(Consumer!(T) action);
194 
195     /**
196      * Returns a new CompletionStage that, when this stage completes
197      * normally, is executed using this stage's default asynchronous
198      * execution facility, with this stage's result as the argument to
199      * the supplied action.
200      *
201      * See the {@link CompletionStage} documentation for rules
202      * covering exceptional completion.
203      *
204      * @param action the action to perform before completing the
205      * returned CompletionStage
206      * @return the new CompletionStage
207      */
208     CompletionStage!(void) thenAcceptAsync(Consumer!(T) action);
209 
210     /**
211      * Returns a new CompletionStage that, when this stage completes
212      * normally, is executed using the supplied Executor, with this
213      * stage's result as the argument to the supplied action.
214      *
215      * See the {@link CompletionStage} documentation for rules
216      * covering exceptional completion.
217      *
218      * @param action the action to perform before completing the
219      * returned CompletionStage
220      * @param executor the executor to use for asynchronous execution
221      * @return the new CompletionStage
222      */
223     CompletionStage!(void) thenAcceptAsync(Consumer!(T) action,
224                                                  Executor executor);
225     /**
226      * Returns a new CompletionStage that, when this stage completes
227      * normally, executes the given action.
228      *
229      * See the {@link CompletionStage} documentation for rules
230      * covering exceptional completion.
231      *
232      * @param action the action to perform before completing the
233      * returned CompletionStage
234      * @return the new CompletionStage
235      */
236     CompletionStage!(void) thenRun(Runnable action);
237 
238     /**
239      * Returns a new CompletionStage that, when this stage completes
240      * normally, executes the given action using this stage's default
241      * asynchronous execution facility.
242      *
243      * See the {@link CompletionStage} documentation for rules
244      * covering exceptional completion.
245      *
246      * @param action the action to perform before completing the
247      * returned CompletionStage
248      * @return the new CompletionStage
249      */
250     CompletionStage!(void) thenRunAsync(Runnable action);
251 
252     /**
253      * Returns a new CompletionStage that, when this stage completes
254      * normally, executes the given action using the supplied Executor.
255      *
256      * See the {@link CompletionStage} documentation for rules
257      * covering exceptional completion.
258      *
259      * @param action the action to perform before completing the
260      * returned CompletionStage
261      * @param executor the executor to use for asynchronous execution
262      * @return the new CompletionStage
263      */
264     CompletionStage!(void) thenRunAsync(Runnable action,
265                                               Executor executor);
266 
267     /**
268      * Returns a new CompletionStage that, when this and the other
269      * given stage both complete normally, is executed with the two
270      * results as arguments to the supplied function.
271      *
272      * See the {@link CompletionStage} documentation for rules
273      * covering exceptional completion.
274      *
275      * @param other the other CompletionStage
276      * @param fn the function to use to compute the value of the
277      * returned CompletionStage
278      * @param (U) the type of the other CompletionStage's result
279      * @param (V) the function's return type
280      * @return the new CompletionStage
281      */
282     CompletionStage!(V) thenCombine(U,V)(CompletionStage!(U) other,
283          BiFunction!(T, U, V) fn);
284 
285     /**
286      * Returns a new CompletionStage that, when this and the other
287      * given stage both complete normally, is executed using this
288      * stage's default asynchronous execution facility, with the two
289      * results as arguments to the supplied function.
290      *
291      * See the {@link CompletionStage} documentation for rules
292      * covering exceptional completion.
293      *
294      * @param other the other CompletionStage
295      * @param fn the function to use to compute the value of the
296      * returned CompletionStage
297      * @param (U) the type of the other CompletionStage's result
298      * @param (V) the function's return type
299      * @return the new CompletionStage
300      */
301     CompletionStage!(V) thenCombineAsync(U,V)(CompletionStage!(U) other,
302          BiFunction!(T, U, V) fn);
303 
304     /**
305      * Returns a new CompletionStage that, when this and the other
306      * given stage both complete normally, is executed using the
307      * supplied executor, with the two results as arguments to the
308      * supplied function.
309      *
310      * See the {@link CompletionStage} documentation for rules
311      * covering exceptional completion.
312      *
313      * @param other the other CompletionStage
314      * @param fn the function to use to compute the value of the
315      * returned CompletionStage
316      * @param executor the executor to use for asynchronous execution
317      * @param (U) the type of the other CompletionStage's result
318      * @param (V) the function's return type
319      * @return the new CompletionStage
320      */
321     CompletionStage!(V) thenCombineAsync(U,V)(CompletionStage!(U) other,
322          BiFunction!(T, U, V) fn, Executor executor);
323 
324     /**
325      * Returns a new CompletionStage that, when this and the other
326      * given stage both complete normally, is executed with the two
327      * results as arguments to the supplied action.
328      *
329      * See the {@link CompletionStage} documentation for rules
330      * covering exceptional completion.
331      *
332      * @param other the other CompletionStage
333      * @param action the action to perform before completing the
334      * returned CompletionStage
335      * @param (U) the type of the other CompletionStage's result
336      * @return the new CompletionStage
337      */
338     CompletionStage!(void) thenAcceptBoth(U)(CompletionStage!(U) other,
339          BiConsumer!(T, U) action);
340 
341     /**
342      * Returns a new CompletionStage that, when this and the other
343      * given stage both complete normally, is executed using this
344      * stage's default asynchronous execution facility, with the two
345      * results as arguments to the supplied action.
346      *
347      * See the {@link CompletionStage} documentation for rules
348      * covering exceptional completion.
349      *
350      * @param other the other CompletionStage
351      * @param action the action to perform before completing the
352      * returned CompletionStage
353      * @param (U) the type of the other CompletionStage's result
354      * @return the new CompletionStage
355      */
356     CompletionStage!(void) thenAcceptBothAsync(U)(CompletionStage!(U) other,
357          BiConsumer!(T, U) action);
358 
359     /**
360      * Returns a new CompletionStage that, when this and the other
361      * given stage both complete normally, is executed using the
362      * supplied executor, with the two results as arguments to the
363      * supplied action.
364      *
365      * See the {@link CompletionStage} documentation for rules
366      * covering exceptional completion.
367      *
368      * @param other the other CompletionStage
369      * @param action the action to perform before completing the
370      * returned CompletionStage
371      * @param executor the executor to use for asynchronous execution
372      * @param (U) the type of the other CompletionStage's result
373      * @return the new CompletionStage
374      */
375     CompletionStage!(void) thenAcceptBothAsync(U)(CompletionStage!(U) other,
376          BiConsumer!(T, U) action, Executor executor);
377 
378     /**
379      * Returns a new CompletionStage that, when this and the other
380      * given stage both complete normally, executes the given action.
381      *
382      * See the {@link CompletionStage} documentation for rules
383      * covering exceptional completion.
384      *
385      * @param other the other CompletionStage
386      * @param action the action to perform before completing the
387      * returned CompletionStage
388      * @return the new CompletionStage
389      */
390     CompletionStage!(void) runAfterBoth(U)(CompletionStage!(U) other,
391                                               Runnable action);
392     /**
393      * Returns a new CompletionStage that, when this and the other
394      * given stage both complete normally, executes the given action
395      * using this stage's default asynchronous execution facility.
396      *
397      * See the {@link CompletionStage} documentation for rules
398      * covering exceptional completion.
399      *
400      * @param other the other CompletionStage
401      * @param action the action to perform before completing the
402      * returned CompletionStage
403      * @return the new CompletionStage
404      */
405     CompletionStage!(void) runAfterBothAsync(U)(CompletionStage!(U) other,
406                                                    Runnable action);
407 
408     /**
409      * Returns a new CompletionStage that, when this and the other
410      * given stage both complete normally, executes the given action
411      * using the supplied executor.
412      *
413      * See the {@link CompletionStage} documentation for rules
414      * covering exceptional completion.
415      *
416      * @param other the other CompletionStage
417      * @param action the action to perform before completing the
418      * returned CompletionStage
419      * @param executor the executor to use for asynchronous execution
420      * @return the new CompletionStage
421      */
422     CompletionStage!(void) runAfterBothAsync(U)(CompletionStage!(U) other,
423                                                    Runnable action,
424                                                    Executor executor);
425     /**
426      * Returns a new CompletionStage that, when either this or the
427      * other given stage complete normally, is executed with the
428      * corresponding result as argument to the supplied function.
429      *
430      * See the {@link CompletionStage} documentation for rules
431      * covering exceptional completion.
432      *
433      * @param other the other CompletionStage
434      * @param fn the function to use to compute the value of the
435      * returned CompletionStage
436      * @param (U) the function's return type
437      * @return the new CompletionStage
438      */
439     CompletionStage!(U) applyToEither(U)(CompletionStage!(T) other,
440          Function!(T, U) fn);
441 
442     /**
443      * Returns a new CompletionStage that, when either this or the
444      * other given stage complete normally, is executed using this
445      * stage's default asynchronous execution facility, with the
446      * corresponding result as argument to the supplied function.
447      *
448      * See the {@link CompletionStage} documentation for rules
449      * covering exceptional completion.
450      *
451      * @param other the other CompletionStage
452      * @param fn the function to use to compute the value of the
453      * returned CompletionStage
454      * @param (U) the function's return type
455      * @return the new CompletionStage
456      */
457     CompletionStage!(U) applyToEitherAsync(U)(CompletionStage!(T) other,
458          Function!(T, U) fn);
459 
460     /**
461      * Returns a new CompletionStage that, when either this or the
462      * other given stage complete normally, is executed using the
463      * supplied executor, with the corresponding result as argument to
464      * the supplied function.
465      *
466      * See the {@link CompletionStage} documentation for rules
467      * covering exceptional completion.
468      *
469      * @param other the other CompletionStage
470      * @param fn the function to use to compute the value of the
471      * returned CompletionStage
472      * @param executor the executor to use for asynchronous execution
473      * @param (U) the function's return type
474      * @return the new CompletionStage
475      */
476     CompletionStage!(U) applyToEitherAsync(U)(CompletionStage!(T) other,
477          Function!(T, U) fn, Executor executor);
478 
479     /**
480      * Returns a new CompletionStage that, when either this or the
481      * other given stage complete normally, is executed with the
482      * corresponding result as argument to the supplied action.
483      *
484      * See the {@link CompletionStage} documentation for rules
485      * covering exceptional completion.
486      *
487      * @param other the other CompletionStage
488      * @param action the action to perform before completing the
489      * returned CompletionStage
490      * @return the new CompletionStage
491      */
492     CompletionStage!(void) acceptEither(CompletionStage!(T) other,
493          Consumer!(T) action);
494 
495     /**
496      * Returns a new CompletionStage that, when either this or the
497      * other given stage complete normally, is executed using this
498      * stage's default asynchronous execution facility, with the
499      * corresponding result as argument to the supplied action.
500      *
501      * See the {@link CompletionStage} documentation for rules
502      * covering exceptional completion.
503      *
504      * @param other the other CompletionStage
505      * @param action the action to perform before completing the
506      * returned CompletionStage
507      * @return the new CompletionStage
508      */
509     CompletionStage!(void) acceptEitherAsync(CompletionStage!(T) other,
510          Consumer!(T) action);
511 
512     /**
513      * Returns a new CompletionStage that, when either this or the
514      * other given stage complete normally, is executed using the
515      * supplied executor, with the corresponding result as argument to
516      * the supplied action.
517      *
518      * See the {@link CompletionStage} documentation for rules
519      * covering exceptional completion.
520      *
521      * @param other the other CompletionStage
522      * @param action the action to perform before completing the
523      * returned CompletionStage
524      * @param executor the executor to use for asynchronous execution
525      * @return the new CompletionStage
526      */
527     CompletionStage!(void) acceptEitherAsync(CompletionStage!(T) other,
528          Consumer!(T) action, Executor executor);
529 
530     /**
531      * Returns a new CompletionStage that, when either this or the
532      * other given stage complete normally, executes the given action.
533      *
534      * See the {@link CompletionStage} documentation for rules
535      * covering exceptional completion.
536      *
537      * @param other the other CompletionStage
538      * @param action the action to perform before completing the
539      * returned CompletionStage
540      * @return the new CompletionStage
541      */
542     CompletionStage!(void) runAfterEither(U)(CompletionStage!(U) other,
543                                                 Runnable action);
544 
545     /**
546      * Returns a new CompletionStage that, when either this or the
547      * other given stage complete normally, executes the given action
548      * using this stage's default asynchronous execution facility.
549      *
550      * See the {@link CompletionStage} documentation for rules
551      * covering exceptional completion.
552      *
553      * @param other the other CompletionStage
554      * @param action the action to perform before completing the
555      * returned CompletionStage
556      * @return the new CompletionStage
557      */
558     CompletionStage!(void) runAfterEitherAsync(U)(CompletionStage!(U) other,
559          Runnable action);
560 
561     /**
562      * Returns a new CompletionStage that, when either this or the
563      * other given stage complete normally, executes the given action
564      * using the supplied executor.
565      *
566      * See the {@link CompletionStage} documentation for rules
567      * covering exceptional completion.
568      *
569      * @param other the other CompletionStage
570      * @param action the action to perform before completing the
571      * returned CompletionStage
572      * @param executor the executor to use for asynchronous execution
573      * @return the new CompletionStage
574      */
575     CompletionStage!(void) runAfterEitherAsync(U)(CompletionStage!(U) other,
576          Runnable action, Executor executor);
577 
578     /**
579      * Returns a new CompletionStage that is completed with the same
580      * value as the CompletionStage returned by the given function.
581      *
582      * <p>When this stage completes normally, the given function is
583      * invoked with this stage's result as the argument, returning
584      * another CompletionStage.  When that stage completes normally,
585      * the CompletionStage returned by this method is completed with
586      * the same value.
587      *
588      * <p>To ensure progress, the supplied function must arrange
589      * eventual completion of its result.
590      *
591      * <p>This method is analogous to
592      * {@link java.util.Optional#flatMap Optional.flatMap} and
593      * {@link java.util.stream.Stream#flatMap Stream.flatMap}.
594      *
595      * <p>See the {@link CompletionStage} documentation for rules
596      * covering exceptional completion.
597      *
598      * @param fn the function to use to compute another CompletionStage
599      * @param (U) the type of the returned CompletionStage's result
600      * @return the new CompletionStage
601      */
602     CompletionStage!(U) thenCompose(U)(Function!(T, CompletionStage!(U)) fn);
603 
604     /**
605      * Returns a new CompletionStage that is completed with the same
606      * value as the CompletionStage returned by the given function,
607      * executed using this stage's default asynchronous execution
608      * facility.
609      *
610      * <p>When this stage completes normally, the given function is
611      * invoked with this stage's result as the argument, returning
612      * another CompletionStage.  When that stage completes normally,
613      * the CompletionStage returned by this method is completed with
614      * the same value.
615      *
616      * <p>To ensure progress, the supplied function must arrange
617      * eventual completion of its result.
618      *
619      * <p>See the {@link CompletionStage} documentation for rules
620      * covering exceptional completion.
621      *
622      * @param fn the function to use to compute another CompletionStage
623      * @param (U) the type of the returned CompletionStage's result
624      * @return the new CompletionStage
625      */
626     CompletionStage!(U) thenComposeAsync(U)
627         (Function!(T, CompletionStage!(U)) fn);
628 
629     /**
630      * Returns a new CompletionStage that is completed with the same
631      * value as the CompletionStage returned by the given function,
632      * executed using the supplied Executor.
633      *
634      * <p>When this stage completes normally, the given function is
635      * invoked with this stage's result as the argument, returning
636      * another CompletionStage.  When that stage completes normally,
637      * the CompletionStage returned by this method is completed with
638      * the same value.
639      *
640      * <p>To ensure progress, the supplied function must arrange
641      * eventual completion of its result.
642      *
643      * <p>See the {@link CompletionStage} documentation for rules
644      * covering exceptional completion.
645      *
646      * @param fn the function to use to compute another CompletionStage
647      * @param executor the executor to use for asynchronous execution
648      * @param (U) the type of the returned CompletionStage's result
649      * @return the new CompletionStage
650      */
651     CompletionStage!(U) thenComposeAsync(U)
652         (Function!(T, CompletionStage!(U)) fn, Executor executor);
653 
654     /**
655      * Returns a new CompletionStage that, when this stage completes
656      * either normally or exceptionally, is executed with this stage's
657      * result and exception as arguments to the supplied function.
658      *
659      * <p>When this stage is complete, the given function is invoked
660      * with the result (or {@code null} if none) and the exception (or
661      * {@code null} if none) of this stage as arguments, and the
662      * function's result is used to complete the returned stage.
663      *
664      * @param fn the function to use to compute the value of the
665      * returned CompletionStage
666      * @param (U) the function's return type
667      * @return the new CompletionStage
668      */
669     CompletionStage!(U) handle(U)(BiFunction!(T, Throwable, U) fn);
670 
671     /**
672      * Returns a new CompletionStage that, when this stage completes
673      * either normally or exceptionally, is executed using this stage's
674      * default asynchronous execution facility, with this stage's
675      * result and exception as arguments to the supplied function.
676      *
677      * <p>When this stage is complete, the given function is invoked
678      * with the result (or {@code null} if none) and the exception (or
679      * {@code null} if none) of this stage as arguments, and the
680      * function's result is used to complete the returned stage.
681      *
682      * @param fn the function to use to compute the value of the
683      * returned CompletionStage
684      * @param (U) the function's return type
685      * @return the new CompletionStage
686      */
687     CompletionStage!(U) handleAsync(U)(BiFunction!(T, Throwable, U) fn);
688 
689     /**
690      * Returns a new CompletionStage that, when this stage completes
691      * either normally or exceptionally, is executed using the
692      * supplied executor, with this stage's result and exception as
693      * arguments to the supplied function.
694      *
695      * <p>When this stage is complete, the given function is invoked
696      * with the result (or {@code null} if none) and the exception (or
697      * {@code null} if none) of this stage as arguments, and the
698      * function's result is used to complete the returned stage.
699      *
700      * @param fn the function to use to compute the value of the
701      * returned CompletionStage
702      * @param executor the executor to use for asynchronous execution
703      * @param (U) the function's return type
704      * @return the new CompletionStage
705      */
706     CompletionStage!(U) handleAsync(U)(BiFunction!(T, Throwable, U) fn,
707          Executor executor);
708 
709     /**
710      * Returns a new CompletionStage with the same result or exception as
711      * this stage, that executes the given action when this stage completes.
712      *
713      * <p>When this stage is complete, the given action is invoked
714      * with the result (or {@code null} if none) and the exception (or
715      * {@code null} if none) of this stage as arguments.  The returned
716      * stage is completed when the action returns.
717      *
718      * <p>Unlike method {@link #handle handle},
719      * this method is not designed to translate completion outcomes,
720      * so the supplied action should not throw an exception. However,
721      * if it does, the following rules apply: if this stage completed
722      * normally but the supplied action throws an exception, then the
723      * returned stage completes exceptionally with the supplied
724      * action's exception. Or, if this stage completed exceptionally
725      * and the supplied action throws an exception, then the returned
726      * stage completes exceptionally with this stage's exception.
727      *
728      * @param action the action to perform
729      * @return the new CompletionStage
730      */
731     CompletionStage!(T) whenComplete
732         (BiConsumer!(T, Throwable) action);
733 
734     /**
735      * Returns a new CompletionStage with the same result or exception as
736      * this stage, that executes the given action using this stage's
737      * default asynchronous execution facility when this stage completes.
738      *
739      * <p>When this stage is complete, the given action is invoked with the
740      * result (or {@code null} if none) and the exception (or {@code null}
741      * if none) of this stage as arguments.  The returned stage is completed
742      * when the action returns.
743      *
744      * <p>Unlike method {@link #handleAsync(BiFunction) handleAsync},
745      * this method is not designed to translate completion outcomes,
746      * so the supplied action should not throw an exception. However,
747      * if it does, the following rules apply: If this stage completed
748      * normally but the supplied action throws an exception, then the
749      * returned stage completes exceptionally with the supplied
750      * action's exception. Or, if this stage completed exceptionally
751      * and the supplied action throws an exception, then the returned
752      * stage completes exceptionally with this stage's exception.
753      *
754      * @param action the action to perform
755      * @return the new CompletionStage
756      */
757     CompletionStage!(T) whenCompleteAsync
758         (BiConsumer!(T, Throwable) action);
759 
760     /**
761      * Returns a new CompletionStage with the same result or exception as
762      * this stage, that executes the given action using the supplied
763      * Executor when this stage completes.
764      *
765      * <p>When this stage is complete, the given action is invoked with the
766      * result (or {@code null} if none) and the exception (or {@code null}
767      * if none) of this stage as arguments.  The returned stage is completed
768      * when the action returns.
769      *
770      * <p>Unlike method {@link #handleAsync(BiFunction,Executor) handleAsync},
771      * this method is not designed to translate completion outcomes,
772      * so the supplied action should not throw an exception. However,
773      * if it does, the following rules apply: If this stage completed
774      * normally but the supplied action throws an exception, then the
775      * returned stage completes exceptionally with the supplied
776      * action's exception. Or, if this stage completed exceptionally
777      * and the supplied action throws an exception, then the returned
778      * stage completes exceptionally with this stage's exception.
779      *
780      * @param action the action to perform
781      * @param executor the executor to use for asynchronous execution
782      * @return the new CompletionStage
783      */
784     CompletionStage!(T) whenCompleteAsync
785         (BiConsumer!(T, Throwable) action, Executor executor);
786 
787     /**
788      * Returns a new CompletionStage that, when this stage completes
789      * exceptionally, is executed with this stage's exception as the
790      * argument to the supplied function.  Otherwise, if this stage
791      * completes normally, then the returned stage also completes
792      * normally with the same value.
793      *
794      * @param fn the function to use to compute the value of the
795      * returned CompletionStage if this CompletionStage completed
796      * exceptionally
797      * @return the new CompletionStage
798      */
799     CompletionStage!(T) exceptionally(Function!(Throwable, T) fn);
800 
801     /**
802      * Returns a {@link CompletableFuture} maintaining the same
803      * completion properties as this stage. If this stage is already a
804      * CompletableFuture, this method may return this stage itself.
805      * Otherwise, invocation of this method may be equivalent in
806      * effect to {@code thenApply(x -> x)}, but returning an instance
807      * of type {@code CompletableFuture}.
808      *
809      * @return the CompletableFuture
810      */
811     CompletableFuture!(T) toCompletableFuture();
812 
813 }