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.Object;
15 import hunt.Functions;
16 import hunt.util.Common;
17 
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  */
126 interface CompletionStage(T) {
127 
128     /**
129      * Returns a new CompletionStage that, when this stage completes
130      * normally, is executed with this stage's result as the argument
131      * to the supplied function.
132      *
133      * <p>This method is analogous to
134      * {@link java.util.Optional#map Optional.map} and
135      * {@link java.util.stream.Stream#map Stream.map}.
136      *
137      * <p>See the {@link CompletionStage} documentation for rules
138      * covering exceptional completion.
139      *
140      * @param fn the function to use to compute the value of the
141      * returned CompletionStage
142      * @param (U) the function's return type
143      * @return the new CompletionStage
144      */
145     CompletionStage!(U) thenApply(U)(Function!(T, U) fn);
146 
147     /**
148      * Returns a new CompletionStage that, when this stage completes
149      * normally, is executed using this stage's default asynchronous
150      * execution facility, with this stage's result as the argument to
151      * the supplied function.
152      *
153      * See the {@link CompletionStage} documentation for rules
154      * covering exceptional completion.
155      *
156      * @param fn the function to use to compute the value of the
157      * returned CompletionStage
158      * @param (U) the function's return type
159      * @return the new CompletionStage
160      */
161     CompletionStage!(U) thenApplyAsync(U)(Function!(T, U) fn);
162 
163     /**
164      * Returns a new CompletionStage that, when this stage completes
165      * normally, is executed using the supplied Executor, with this
166      * stage's result as the argument to the supplied function.
167      *
168      * See the {@link CompletionStage} documentation for rules
169      * covering exceptional completion.
170      *
171      * @param fn the function to use to compute the value of the
172      * returned CompletionStage
173      * @param executor the executor to use for asynchronous execution
174      * @param (U) the function's return type
175      * @return the new CompletionStage
176      */
177 //     CompletionStage!(U) thenApplyAsync(U)(Function!(T, U) fn,
178 //          Executor executor);
179 
180 static if(is(T == void)) {
181 
182     CompletionStage!(void) thenAccept(Action action);
183     CompletionStage!(void) thenAcceptAsync(Action action);
184     CompletionStage!(void) thenAcceptAsync(Action action,
185                                                  Executor executor);
186 } else {
187     /**
188      * Returns a new CompletionStage that, when this stage completes
189      * normally, is executed with this stage's result as the argument
190      * to the supplied action.
191      *
192      * See the {@link CompletionStage} documentation for rules
193      * covering exceptional completion.
194      *
195      * @param action the action to perform before completing the
196      * returned CompletionStage
197      * @return the new CompletionStage
198      */
199     CompletionStage!(void) thenAccept(Consumer!(T) action);
200 
201     /**
202      * Returns a new CompletionStage that, when this stage completes
203      * normally, is executed using this stage's default asynchronous
204      * execution facility, with this stage's result as the argument to
205      * the supplied action.
206      *
207      * See the {@link CompletionStage} documentation for rules
208      * covering exceptional completion.
209      *
210      * @param action the action to perform before completing the
211      * returned CompletionStage
212      * @return the new CompletionStage
213      */
214     CompletionStage!(void) thenAcceptAsync(Consumer!(T) action);
215 
216     /**
217      * Returns a new CompletionStage that, when this stage completes
218      * normally, is executed using the supplied Executor, with this
219      * stage's result as the argument to the supplied action.
220      *
221      * See the {@link CompletionStage} documentation for rules
222      * covering exceptional completion.
223      *
224      * @param action the action to perform before completing the
225      * returned CompletionStage
226      * @param executor the executor to use for asynchronous execution
227      * @return the new CompletionStage
228      */
229     CompletionStage!(void) thenAcceptAsync(Consumer!(T) action,
230                                                  Executor executor);
231 
232      
233 }                                                 
234     /**
235      * Returns a new CompletionStage that, when this stage completes
236      * normally, executes the given action.
237      *
238      * See the {@link CompletionStage} documentation for rules
239      * covering exceptional completion.
240      *
241      * @param action the action to perform before completing the
242      * returned CompletionStage
243      * @return the new CompletionStage
244      */
245     CompletionStage!(void) thenRun(Runnable action);
246 
247     /**
248      * Returns a new CompletionStage that, when this stage completes
249      * normally, executes the given action using this stage's default
250      * asynchronous execution facility.
251      *
252      * See the {@link CompletionStage} documentation for rules
253      * covering exceptional completion.
254      *
255      * @param action the action to perform before completing the
256      * returned CompletionStage
257      * @return the new CompletionStage
258      */
259     CompletionStage!(void) thenRunAsync(Runnable action);
260 
261     /**
262      * Returns a new CompletionStage that, when this stage completes
263      * normally, executes the given action using the supplied Executor.
264      *
265      * See the {@link CompletionStage} documentation for rules
266      * covering exceptional completion.
267      *
268      * @param action the action to perform before completing the
269      * returned CompletionStage
270      * @param executor the executor to use for asynchronous execution
271      * @return the new CompletionStage
272      */
273     CompletionStage!(void) thenRunAsync(Runnable action,
274                                               Executor executor);
275 
276     /**
277      * Returns a new CompletionStage that, when this and the other
278      * given stage both complete normally, is executed with the two
279      * results as arguments to the supplied function.
280      *
281      * See the {@link CompletionStage} documentation for rules
282      * covering exceptional completion.
283      *
284      * @param other the other CompletionStage
285      * @param fn the function to use to compute the value of the
286      * returned CompletionStage
287      * @param (U) the type of the other CompletionStage's result
288      * @param (V) the function's return type
289      * @return the new CompletionStage
290      */
291 //     CompletionStage!(V) thenCombine(U,V)(CompletionStage!(U) other,
292 //          BiFunction!(T, U, V) fn);
293 
294     /**
295      * Returns a new CompletionStage that, when this and the other
296      * given stage both complete normally, is executed using this
297      * stage's default asynchronous execution facility, with the two
298      * results as arguments to the supplied function.
299      *
300      * See the {@link CompletionStage} documentation for rules
301      * covering exceptional completion.
302      *
303      * @param other the other CompletionStage
304      * @param fn the function to use to compute the value of the
305      * returned CompletionStage
306      * @param (U) the type of the other CompletionStage's result
307      * @param (V) the function's return type
308      * @return the new CompletionStage
309      */
310 //     CompletionStage!(V) thenCombineAsync(U,V)(CompletionStage!(U) other,
311 //          BiFunction!(T, U, V) fn);
312 
313     /**
314      * Returns a new CompletionStage that, when this and the other
315      * given stage both complete normally, is executed using the
316      * supplied executor, with the two results as arguments to the
317      * supplied function.
318      *
319      * See the {@link CompletionStage} documentation for rules
320      * covering exceptional completion.
321      *
322      * @param other the other CompletionStage
323      * @param fn the function to use to compute the value of the
324      * returned CompletionStage
325      * @param executor the executor to use for asynchronous execution
326      * @param (U) the type of the other CompletionStage's result
327      * @param (V) the function's return type
328      * @return the new CompletionStage
329      */
330 //     CompletionStage!(V) thenCombineAsync(U,V)(CompletionStage!(U) other,
331 //          BiFunction!(T, U, V) fn, Executor executor);
332 
333     /**
334      * Returns a new CompletionStage that, when this and the other
335      * given stage both complete normally, is executed with the two
336      * results as arguments to the supplied action.
337      *
338      * See the {@link CompletionStage} documentation for rules
339      * covering exceptional completion.
340      *
341      * @param other the other CompletionStage
342      * @param action the action to perform before completing the
343      * returned CompletionStage
344      * @param (U) the type of the other CompletionStage's result
345      * @return the new CompletionStage
346      */
347 //     CompletionStage!(void) thenAcceptBoth(U)(CompletionStage!(U) other,
348 //          BiConsumer!(T, U) action);
349 
350     /**
351      * Returns a new CompletionStage that, when this and the other
352      * given stage both complete normally, is executed using this
353      * stage's default asynchronous execution facility, with the two
354      * results as arguments to the supplied action.
355      *
356      * See the {@link CompletionStage} documentation for rules
357      * covering exceptional completion.
358      *
359      * @param other the other CompletionStage
360      * @param action the action to perform before completing the
361      * returned CompletionStage
362      * @param (U) the type of the other CompletionStage's result
363      * @return the new CompletionStage
364      */
365 //     CompletionStage!(void) thenAcceptBothAsync(U)(CompletionStage!(U) other,
366 //          BiConsumer!(T, U) action);
367 
368     /**
369      * Returns a new CompletionStage that, when this and the other
370      * given stage both complete normally, is executed using the
371      * supplied executor, with the two results as arguments to the
372      * supplied action.
373      *
374      * See the {@link CompletionStage} documentation for rules
375      * covering exceptional completion.
376      *
377      * @param other the other CompletionStage
378      * @param action the action to perform before completing the
379      * returned CompletionStage
380      * @param executor the executor to use for asynchronous execution
381      * @param (U) the type of the other CompletionStage's result
382      * @return the new CompletionStage
383      */
384 //     CompletionStage!(void) thenAcceptBothAsync(U)(CompletionStage!(U) other,
385 //          BiConsumer!(T, U) action, Executor executor);
386 
387     /**
388      * Returns a new CompletionStage that, when this and the other
389      * given stage both complete normally, executes the given action.
390      *
391      * See the {@link CompletionStage} documentation for rules
392      * covering exceptional completion.
393      *
394      * @param other the other CompletionStage
395      * @param action the action to perform before completing the
396      * returned CompletionStage
397      * @return the new CompletionStage
398      */
399 //     CompletionStage!(void) runAfterBoth(U)(CompletionStage!(U) other,
400 //                                               Runnable action);
401     /**
402      * Returns a new CompletionStage that, when this and the other
403      * given stage both complete normally, executes the given action
404      * using this stage's default asynchronous execution facility.
405      *
406      * See the {@link CompletionStage} documentation for rules
407      * covering exceptional completion.
408      *
409      * @param other the other CompletionStage
410      * @param action the action to perform before completing the
411      * returned CompletionStage
412      * @return the new CompletionStage
413      */
414 //     CompletionStage!(void) runAfterBothAsync(U)(CompletionStage!(U) other,
415 //                                                    Runnable action);
416 
417     /**
418      * Returns a new CompletionStage that, when this and the other
419      * given stage both complete normally, executes the given action
420      * using the supplied executor.
421      *
422      * See the {@link CompletionStage} documentation for rules
423      * covering exceptional completion.
424      *
425      * @param other the other CompletionStage
426      * @param action the action to perform before completing the
427      * returned CompletionStage
428      * @param executor the executor to use for asynchronous execution
429      * @return the new CompletionStage
430      */
431 //     CompletionStage!(void) runAfterBothAsync(U)(CompletionStage!(U) other,
432 //                                                    Runnable action,
433 //                                                    Executor executor);
434     /**
435      * Returns a new CompletionStage that, when either this or the
436      * other given stage complete normally, is executed with the
437      * corresponding result as argument to the supplied function.
438      *
439      * See the {@link CompletionStage} documentation for rules
440      * covering exceptional completion.
441      *
442      * @param other the other CompletionStage
443      * @param fn the function to use to compute the value of the
444      * returned CompletionStage
445      * @param (U) the function's return type
446      * @return the new CompletionStage
447      */
448 //     CompletionStage!(U) applyToEither(U)(CompletionStage!(T) other,
449 //          Function!(T, U) fn);
450 
451     /**
452      * Returns a new CompletionStage that, when either this or the
453      * other given stage complete normally, is executed using this
454      * stage's default asynchronous execution facility, with the
455      * corresponding result as argument to the supplied function.
456      *
457      * See the {@link CompletionStage} documentation for rules
458      * covering exceptional completion.
459      *
460      * @param other the other CompletionStage
461      * @param fn the function to use to compute the value of the
462      * returned CompletionStage
463      * @param (U) the function's return type
464      * @return the new CompletionStage
465      */
466 //     CompletionStage!(U) applyToEitherAsync(U)(CompletionStage!(T) other,
467 //          Function!(T, U) fn);
468 
469     /**
470      * Returns a new CompletionStage that, when either this or the
471      * other given stage complete normally, is executed using the
472      * supplied executor, with the corresponding result as argument to
473      * the supplied function.
474      *
475      * See the {@link CompletionStage} documentation for rules
476      * covering exceptional completion.
477      *
478      * @param other the other CompletionStage
479      * @param fn the function to use to compute the value of the
480      * returned CompletionStage
481      * @param executor the executor to use for asynchronous execution
482      * @param (U) the function's return type
483      * @return the new CompletionStage
484      */
485 //     CompletionStage!(U) applyToEitherAsync(U)(CompletionStage!(T) other,
486 //          Function!(T, U) fn, Executor executor);
487 
488 static if(is(T == void)) {
489     CompletionStage!(void) acceptEither(CompletionStage!(T) other,
490          Action action);
491     CompletionStage!(void) acceptEitherAsync(CompletionStage!(T) other,
492          Action action);
493     CompletionStage!(void) acceptEitherAsync(CompletionStage!(T) other,
494          Action action, Executor executor);
495 } else {
496     /**
497      * Returns a new CompletionStage that, when either this or the
498      * other given stage complete normally, is executed 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) acceptEither(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 this
515      * stage's default asynchronous execution facility, with the
516      * corresponding result as argument to 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      * @return the new CompletionStage
525      */
526     CompletionStage!(void) acceptEitherAsync(CompletionStage!(T) other,
527          Consumer!(T) action);
528 
529     /**
530      * Returns a new CompletionStage that, when either this or the
531      * other given stage complete normally, is executed using the
532      * supplied executor, with the corresponding result as argument to
533      * the supplied action.
534      *
535      * See the {@link CompletionStage} documentation for rules
536      * covering exceptional completion.
537      *
538      * @param other the other CompletionStage
539      * @param action the action to perform before completing the
540      * returned CompletionStage
541      * @param executor the executor to use for asynchronous execution
542      * @return the new CompletionStage
543      */
544     CompletionStage!(void) acceptEitherAsync(CompletionStage!(T) other,
545          Consumer!(T) action, Executor executor);
546      
547 }
548     /**
549      * Returns a new CompletionStage that, when either this or the
550      * other given stage complete normally, executes the given action.
551      *
552      * See the {@link CompletionStage} documentation for rules
553      * covering exceptional completion.
554      *
555      * @param other the other CompletionStage
556      * @param action the action to perform before completing the
557      * returned CompletionStage
558      * @return the new CompletionStage
559      */
560 //     CompletionStage!(void) runAfterEither(U)(CompletionStage!(U) other,
561 //                                                 Runnable action);
562 
563     /**
564      * Returns a new CompletionStage that, when either this or the
565      * other given stage complete normally, executes the given action
566      * using this stage's default asynchronous execution facility.
567      *
568      * See the {@link CompletionStage} documentation for rules
569      * covering exceptional completion.
570      *
571      * @param other the other CompletionStage
572      * @param action the action to perform before completing the
573      * returned CompletionStage
574      * @return the new CompletionStage
575      */
576 //     CompletionStage!(void) runAfterEitherAsync(U)(CompletionStage!(U) other,
577 //          Runnable action);
578 
579     /**
580      * Returns a new CompletionStage that, when either this or the
581      * other given stage complete normally, executes the given action
582      * using the supplied executor.
583      *
584      * See the {@link CompletionStage} documentation for rules
585      * covering exceptional completion.
586      *
587      * @param other the other CompletionStage
588      * @param action the action to perform before completing the
589      * returned CompletionStage
590      * @param executor the executor to use for asynchronous execution
591      * @return the new CompletionStage
592      */
593 //     CompletionStage!(void) runAfterEitherAsync(U)(CompletionStage!(U) other,
594 //          Runnable action, Executor executor);
595 
596     /**
597      * Returns a new CompletionStage that is completed with the same
598      * value as the CompletionStage returned by the given function.
599      *
600      * <p>When this stage completes normally, the given function is
601      * invoked with this stage's result as the argument, returning
602      * another CompletionStage.  When that stage completes normally,
603      * the CompletionStage returned by this method is completed with
604      * the same value.
605      *
606      * <p>To ensure progress, the supplied function must arrange
607      * eventual completion of its result.
608      *
609      * <p>This method is analogous to
610      * {@link java.util.Optional#flatMap Optional.flatMap} and
611      * {@link java.util.stream.Stream#flatMap Stream.flatMap}.
612      *
613      * <p>See the {@link CompletionStage} documentation for rules
614      * covering exceptional completion.
615      *
616      * @param fn the function to use to compute another CompletionStage
617      * @param (U) the type of the returned CompletionStage's result
618      * @return the new CompletionStage
619      */
620 //     CompletionStage!(U) thenCompose(U)(Function!(T, CompletionStage!(U)) fn);
621 
622     /**
623      * Returns a new CompletionStage that is completed with the same
624      * value as the CompletionStage returned by the given function,
625      * executed using this stage's default asynchronous execution
626      * facility.
627      *
628      * <p>When this stage completes normally, the given function is
629      * invoked with this stage's result as the argument, returning
630      * another CompletionStage.  When that stage completes normally,
631      * the CompletionStage returned by this method is completed with
632      * the same value.
633      *
634      * <p>To ensure progress, the supplied function must arrange
635      * eventual completion of its result.
636      *
637      * <p>See the {@link CompletionStage} documentation for rules
638      * covering exceptional completion.
639      *
640      * @param fn the function to use to compute another CompletionStage
641      * @param (U) the type of the returned CompletionStage's result
642      * @return the new CompletionStage
643      */
644 //     CompletionStage!(U) thenComposeAsync(U)(Function!(T, CompletionStage!(U)) fn);
645 
646     /**
647      * Returns a new CompletionStage that is completed with the same
648      * value as the CompletionStage returned by the given function,
649      * executed using the supplied Executor.
650      *
651      * <p>When this stage completes normally, the given function is
652      * invoked with this stage's result as the argument, returning
653      * another CompletionStage.  When that stage completes normally,
654      * the CompletionStage returned by this method is completed with
655      * the same value.
656      *
657      * <p>To ensure progress, the supplied function must arrange
658      * eventual completion of its result.
659      *
660      * <p>See the {@link CompletionStage} documentation for rules
661      * covering exceptional completion.
662      *
663      * @param fn the function to use to compute another CompletionStage
664      * @param executor the executor to use for asynchronous execution
665      * @param (U) the type of the returned CompletionStage's result
666      * @return the new CompletionStage
667      */
668 //     CompletionStage!(U) thenComposeAsync(U)
669 //         (Function!(T, CompletionStage!(U)) fn, Executor executor);
670 
671     /**
672      * Returns a new CompletionStage that, when this stage completes
673      * either normally or exceptionally, is executed with this stage's
674      * result and exception as arguments to the supplied function.
675      *
676      * <p>When this stage is complete, the given function is invoked
677      * with the result (or {@code null} if none) and the exception (or
678      * {@code null} if none) of this stage as arguments, and the
679      * function's result is used to complete the returned stage.
680      *
681      * @param fn the function to use to compute the value of the
682      * returned CompletionStage
683      * @param (U) the function's return type
684      * @return the new CompletionStage
685      */
686 //     CompletionStage!(U) handle(U)(BiFunction!(T, Throwable, U) fn);
687 
688     /**
689      * Returns a new CompletionStage that, when this stage completes
690      * either normally or exceptionally, is executed using this stage's
691      * default asynchronous execution facility, with this stage's
692      * result and exception as arguments to the supplied function.
693      *
694      * <p>When this stage is complete, the given function is invoked
695      * with the result (or {@code null} if none) and the exception (or
696      * {@code null} if none) of this stage as arguments, and the
697      * function's result is used to complete the returned stage.
698      *
699      * @param fn the function to use to compute the value of the
700      * returned CompletionStage
701      * @param (U) the function's return type
702      * @return the new CompletionStage
703      */
704 //     CompletionStage!(U) handleAsync(U)(BiFunction!(T, Throwable, U) fn);
705 
706     /**
707      * Returns a new CompletionStage that, when this stage completes
708      * either normally or exceptionally, is executed using the
709      * supplied executor, with this stage's result and exception as
710      * arguments to the supplied function.
711      *
712      * <p>When this stage is complete, the given function is invoked
713      * with the result (or {@code null} if none) and the exception (or
714      * {@code null} if none) of this stage as arguments, and the
715      * function's result is used to complete the returned stage.
716      *
717      * @param fn the function to use to compute the value of the
718      * returned CompletionStage
719      * @param executor the executor to use for asynchronous execution
720      * @param (U) the function's return type
721      * @return the new CompletionStage
722      */
723 //     CompletionStage!(U) handleAsync(U)(BiFunction!(T, Throwable, U) fn,
724 //          Executor executor);
725 
726 static if(is(T == void)) {
727     CompletionStage!(T) whenCompleteAsync(Action1!(Throwable) action);
728     CompletionStage!(T) whenComplete(Action1!(Throwable) action);
729     CompletionStage!(T) whenCompleteAsync(Action1!(Throwable) action, Executor executor);
730 } else {
731 
732     /**
733      * Returns a new CompletionStage with the same result or exception as
734      * this stage, that executes the given action when this stage completes.
735      *
736      * <p>When this stage is complete, the given action is invoked
737      * with the result (or {@code null} if none) and the exception (or
738      * {@code null} if none) of this stage as arguments.  The returned
739      * stage is completed when the action returns.
740      *
741      * <p>Unlike method {@link #handle handle},
742      * this method is not designed to translate completion outcomes,
743      * so the supplied action should not throw an exception. However,
744      * if it does, the following rules apply: if this stage completed
745      * normally but the supplied action throws an exception, then the
746      * returned stage completes exceptionally with the supplied
747      * action's exception. Or, if this stage completed exceptionally
748      * and the supplied action throws an exception, then the returned
749      * stage completes exceptionally with this stage's exception.
750      *
751      * @param action the action to perform
752      * @return the new CompletionStage
753      */
754     CompletionStage!(T) whenComplete(BiConsumer!(T, Throwable) action);
755 
756     /**
757      * Returns a new CompletionStage with the same result or exception as
758      * this stage, that executes the given action using this stage's
759      * default asynchronous execution facility when this stage completes.
760      *
761      * <p>When this stage is complete, the given action is invoked with the
762      * result (or {@code null} if none) and the exception (or {@code null}
763      * if none) of this stage as arguments.  The returned stage is completed
764      * when the action returns.
765      *
766      * <p>Unlike method {@link #handleAsync(BiFunction) handleAsync},
767      * this method is not designed to translate completion outcomes,
768      * so the supplied action should not throw an exception. However,
769      * if it does, the following rules apply: If this stage completed
770      * normally but the supplied action throws an exception, then the
771      * returned stage completes exceptionally with the supplied
772      * action's exception. Or, if this stage completed exceptionally
773      * and the supplied action throws an exception, then the returned
774      * stage completes exceptionally with this stage's exception.
775      *
776      * @param action the action to perform
777      * @return the new CompletionStage
778      */
779     CompletionStage!(T) whenCompleteAsync(BiConsumer!(T, Throwable) action);
780 
781     /**
782      * Returns a new CompletionStage with the same result or exception as
783      * this stage, that executes the given action using the supplied
784      * Executor when this stage completes.
785      *
786      * <p>When this stage is complete, the given action is invoked with the
787      * result (or {@code null} if none) and the exception (or {@code null}
788      * if none) of this stage as arguments.  The returned stage is completed
789      * when the action returns.
790      *
791      * <p>Unlike method {@link #handleAsync(BiFunction,Executor) handleAsync},
792      * this method is not designed to translate completion outcomes,
793      * so the supplied action should not throw an exception. However,
794      * if it does, the following rules apply: If this stage completed
795      * normally but the supplied action throws an exception, then the
796      * returned stage completes exceptionally with the supplied
797      * action's exception. Or, if this stage completed exceptionally
798      * and the supplied action throws an exception, then the returned
799      * stage completes exceptionally with this stage's exception.
800      *
801      * @param action the action to perform
802      * @param executor the executor to use for asynchronous execution
803      * @return the new CompletionStage
804      */
805     CompletionStage!(T) whenCompleteAsync(BiConsumer!(T, Throwable) action, Executor executor);
806      
807 }
808     /**
809      * Returns a new CompletionStage that, when this stage completes
810      * exceptionally, is executed with this stage's exception as the
811      * argument to the supplied function.  Otherwise, if this stage
812      * completes normally, then the returned stage also completes
813      * normally with the same value.
814      *
815      * @param fn the function to use to compute the value of the
816      * returned CompletionStage if this CompletionStage completed
817      * exceptionally
818      * @return the new CompletionStage
819      */
820     CompletionStage!(T) exceptionally(Function!(Throwable, T) fn);
821 
822     /**
823      * Returns a {@link CompletableFuture} maintaining the same
824      * completion properties as this stage. If this stage is already a
825      * CompletableFuture, this method may return this stage itself.
826      * Otherwise, invocation of this method may be equivalent in
827      * effect to {@code thenApply(x -> x)}, but returning an instance
828      * of type {@code CompletableFuture}.
829      *
830      * @return the CompletableFuture
831      */
832     CompletionStage!(T) toCompletableFuture();
833     
834 
835 }