ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CompletionStage.java
Revision: 1.16
Committed: Sat Dec 20 18:21:48 2014 UTC (9 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.15: +8 -6 lines
Log Message:
Improve whenComplete javadoc wording

File Contents

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