ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CompletionStage.java
Revision: 1.15
Committed: Tue Dec 2 12:16:23 2014 UTC (9 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.14: +2 -2 lines
Log Message:
Wording fix for thenCompose spec

File Contents

# Content
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 * depending on whether it requires arguments and/or produces results.
27 * 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 * themselves, rather than their results. </li>
31 *
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 * or custom (via a supplied {@link Executor}). The execution
49 * 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 * {@code whenComplete}, when the supplied action itself encounters an
73 * 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 * <p>This interface does not define methods for initially creating,
87 * 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 * @param <U> the function's return type
110 * @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 * @param <U> the function's return type
126 * @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 * @param <U> the function's return type
143 * @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 Executor executor);
193 /**
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 * @param <U> the type of the other CompletionStage's result
247 * @param <V> the function's return type
248 * @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 * @param <U> the type of the other CompletionStage's result
267 * @param <V> the function's return type
268 * @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 * @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 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 * @param <U> the type of the other CompletionStage's result
308 * @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 * @param <U> the type of the other CompletionStage's result
324 * @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 * @param <U> the type of the other CompletionStage's result
341 * @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 * the supplied executor.
382 *
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 * @param <U> the function's return type
407 * @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 * @param <U> the function's return type
426 * @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 * @param <U> the function's return type
446 * @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
523 /**
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 * using the supplied executor.
544 *
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 * normally, is executed with this stage's result as the argument
562 * 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 * @param <U> the type of the returned CompletionStage's result
569 * @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 * execution facility, with this stage's result as the argument to the
578 * 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 * @param <U> the type of the returned CompletionStage's result
585 * @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 * @param <U> the type of the returned CompletionStage's result
601 * @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 * 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 * <p>When this stage is complete, the given action is invoked with the
627 * result (or {@code null} if none) and the exception (or {@code null}
628 * if none) of this stage as arguments. The returned stage is completed
629 * when the action returns. If the supplied action itself encounters an
630 * exception, then the returned stage exceptionally completes with this
631 * exception unless this stage also completed exceptionally.
632 *
633 * @param action the action to perform
634 * @return the new CompletionStage
635 */
636 public CompletionStage<T> whenComplete
637 (BiConsumer<? super T, ? super Throwable> action);
638
639 /**
640 * Returns a new CompletionStage with the same result or exception as
641 * this stage, that executes the given action using this stage's
642 * default asynchronous execution facility when this stage completes.
643 *
644 * <p>When this stage is complete, the given action is invoked with the
645 * result (or {@code null} if none) and the exception (or {@code null}
646 * if none) of this stage as arguments. The returned stage is completed
647 * when the action returns. If the supplied action itself encounters an
648 * exception, then the returned stage exceptionally completes with this
649 * exception unless this stage also completed exceptionally.
650 *
651 * @param action the action to perform
652 * @return the new CompletionStage
653 */
654 public CompletionStage<T> whenCompleteAsync
655 (BiConsumer<? super T, ? super Throwable> action);
656
657 /**
658 * Returns a new CompletionStage with the same result or exception as
659 * this stage, that executes the given action using the supplied
660 * Executor when this stage completes.
661 *
662 * <p>When this stage is complete, the given action is invoked with the
663 * result (or {@code null} if none) and the exception (or {@code null}
664 * if none) of this stage as arguments. The returned stage is completed
665 * when the action returns. If the supplied action itself encounters an
666 * exception, then the returned stage exceptionally completes with this
667 * exception unless this stage also completed exceptionally.
668 *
669 * @param action the action to perform
670 * @param executor the executor to use for asynchronous execution
671 * @return the new CompletionStage
672 */
673 public CompletionStage<T> whenCompleteAsync
674 (BiConsumer<? super T, ? super Throwable> action,
675 Executor executor);
676
677 /**
678 * Returns a new CompletionStage that, when this stage completes
679 * either normally or exceptionally, is executed with this stage's
680 * result and exception as arguments to the supplied function.
681 *
682 * <p>When this stage is complete, the given function is invoked
683 * with the result (or {@code null} if none) and the exception (or
684 * {@code null} if none) of this stage as arguments, and the
685 * function's result is used to complete the returned stage.
686 *
687 * @param fn the function to use to compute the value of the
688 * returned CompletionStage
689 * @param <U> the function's return type
690 * @return the new CompletionStage
691 */
692 public <U> CompletionStage<U> handle
693 (BiFunction<? super T, Throwable, ? extends U> fn);
694
695 /**
696 * Returns a new CompletionStage that, when this stage completes
697 * either normally or exceptionally, is executed using this stage's
698 * default asynchronous execution facility, with this stage's
699 * result and exception as arguments to the supplied function.
700 *
701 * <p>When this stage is complete, the given function is invoked
702 * with the result (or {@code null} if none) and the exception (or
703 * {@code null} if none) of this stage as arguments, and the
704 * function's result is used to complete the returned stage.
705 *
706 * @param fn the function to use to compute the value of the
707 * returned CompletionStage
708 * @param <U> the function's return type
709 * @return the new CompletionStage
710 */
711 public <U> CompletionStage<U> handleAsync
712 (BiFunction<? super T, Throwable, ? extends U> fn);
713
714 /**
715 * Returns a new CompletionStage that, when this stage completes
716 * either normally or exceptionally, is executed using the
717 * supplied executor, with this stage's result and exception as
718 * arguments to the supplied function.
719 *
720 * <p>When this stage is complete, the given function is invoked
721 * with the result (or {@code null} if none) and the exception (or
722 * {@code null} if none) of this stage as arguments, and the
723 * function's result is used to complete the returned stage.
724 *
725 * @param fn the function to use to compute the value of the
726 * returned CompletionStage
727 * @param executor the executor to use for asynchronous execution
728 * @param <U> the function's return type
729 * @return the new CompletionStage
730 */
731 public <U> CompletionStage<U> handleAsync
732 (BiFunction<? super T, Throwable, ? extends U> fn,
733 Executor executor);
734
735 /**
736 * Returns a {@link CompletableFuture} maintaining the same
737 * completion properties as this stage. If this stage is already a
738 * CompletableFuture, this method may return this stage itself.
739 * Otherwise, invocation of this method may be equivalent in
740 * effect to {@code thenApply(x -> x)}, but returning an instance
741 * of type {@code CompletableFuture}. A CompletionStage
742 * implementation that does not choose to interoperate with others
743 * may throw {@code UnsupportedOperationException}.
744 *
745 * @return the CompletableFuture
746 * @throws UnsupportedOperationException if this implementation
747 * does not interoperate with CompletableFuture
748 */
749 public CompletableFuture<T> toCompletableFuture();
750
751 }