ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CompletionStage.java
Revision: 1.30
Committed: Sun Jan 24 01:13:22 2016 UTC (8 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.29: +2 -2 lines
Log Message:
fix action/function javadoc mismatch

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