< prev index next >

src/java.base/share/classes/java/util/concurrent/CompletionStage.java

Print this page
8210971: Add exception handling methods to CompletionStage and CompletableFuture
Reviewed-by: martin, chegar


 834     public CompletionStage<T> whenCompleteAsync
 835         (BiConsumer<? super T, ? super Throwable> action,
 836          Executor executor);
 837 
 838     /**
 839      * Returns a new CompletionStage that, when this stage completes
 840      * exceptionally, is executed with this stage's exception as the
 841      * argument to the supplied function.  Otherwise, if this stage
 842      * completes normally, then the returned stage also completes
 843      * normally with the same value.
 844      *
 845      * @param fn the function to use to compute the value of the
 846      * returned CompletionStage if this CompletionStage completed
 847      * exceptionally
 848      * @return the new CompletionStage
 849      */
 850     public CompletionStage<T> exceptionally
 851         (Function<Throwable, ? extends T> fn);
 852 
 853     /**




























































































































 854      * Returns a {@link CompletableFuture} maintaining the same
 855      * completion properties as this stage. If this stage is already a
 856      * CompletableFuture, this method may return this stage itself.
 857      * Otherwise, invocation of this method may be equivalent in
 858      * effect to {@code thenApply(x -> x)}, but returning an instance
 859      * of type {@code CompletableFuture}.
 860      *
 861      * @return the CompletableFuture
 862      */
 863     public CompletableFuture<T> toCompletableFuture();
 864 
 865 }


 834     public CompletionStage<T> whenCompleteAsync
 835         (BiConsumer<? super T, ? super Throwable> action,
 836          Executor executor);
 837 
 838     /**
 839      * Returns a new CompletionStage that, when this stage completes
 840      * exceptionally, is executed with this stage's exception as the
 841      * argument to the supplied function.  Otherwise, if this stage
 842      * completes normally, then the returned stage also completes
 843      * normally with the same value.
 844      *
 845      * @param fn the function to use to compute the value of the
 846      * returned CompletionStage if this CompletionStage completed
 847      * exceptionally
 848      * @return the new CompletionStage
 849      */
 850     public CompletionStage<T> exceptionally
 851         (Function<Throwable, ? extends T> fn);
 852 
 853     /**
 854      * Returns a new CompletionStage that, when this stage completes
 855      * exceptionally, is executed with this stage's exception as the
 856      * argument to the supplied function, using this stage's default
 857      * asynchronous execution facility.  Otherwise, if this stage
 858      * completes normally, then the returned stage also completes
 859      * normally with the same value.
 860      *
 861      * @implSpec The default implementation invokes {@link #handle},
 862      * relaying to {@link #handleAsync} on exception, then {@link
 863      * #thenCompose} for result.
 864      *
 865      * @param fn the function to use to compute the value of the
 866      * returned CompletionStage if this CompletionStage completed
 867      * exceptionally
 868      * @return the new CompletionStage
 869      * @since 12
 870      */
 871     public default CompletionStage<T> exceptionallyAsync
 872         (Function<Throwable, ? extends T> fn) {
 873         return handle((r, ex) -> (ex == null)
 874                       ? this
 875                       : this.<T>handleAsync((r1, ex1) -> fn.apply(ex1)))
 876             .thenCompose(Function.identity());
 877     }
 878 
 879     /**
 880      * Returns a new CompletionStage that, when this stage completes
 881      * exceptionally, is executed with this stage's exception as the
 882      * argument to the supplied function, using the supplied Executor.
 883      * Otherwise, if this stage completes normally, then the returned
 884      * stage also completes normally with the same value.
 885      *
 886      * @implSpec The default implementation invokes {@link #handle},
 887      * relaying to {@link #handleAsync} on exception, then {@link
 888      * #thenCompose} for result.
 889      *
 890      * @param fn the function to use to compute the value of the
 891      * returned CompletionStage if this CompletionStage completed
 892      * exceptionally
 893      * @param executor the executor to use for asynchronous execution
 894      * @return the new CompletionStage
 895      * @since 12
 896      */
 897     public default CompletionStage<T> exceptionallyAsync
 898         (Function<Throwable, ? extends T> fn, Executor executor) {
 899         return handle((r, ex) -> (ex == null)
 900                       ? this
 901                       : this.<T>handleAsync((r1, ex1) -> fn.apply(ex1), executor))
 902             .thenCompose(Function.identity());
 903     }
 904 
 905     /**
 906      * Returns a new CompletionStage that, when this stage completes
 907      * exceptionally, is composed using the results of the supplied
 908      * function applied to this stage's exception.
 909      *
 910      * @implSpec The default implementation invokes {@link #handle},
 911      * invoking the given function on exception, then {@link
 912      * #thenCompose} for result.
 913      *
 914      * @param fn the function to use to compute the returned
 915      * CompletionStage if this CompletionStage completed exceptionally
 916      * @return the new CompletionStage
 917      * @since 12
 918      */
 919     public default CompletionStage<T> exceptionallyCompose
 920         (Function<Throwable, ? extends CompletionStage<T>> fn) {
 921         return handle((r, ex) -> (ex == null)
 922                       ? this
 923                       : fn.apply(ex))
 924             .thenCompose(Function.identity());
 925     }
 926 
 927     /**
 928      * Returns a new CompletionStage that, when this stage completes
 929      * exceptionally, is composed using the results of the supplied
 930      * function applied to this stage's exception, using this stage's
 931      * default asynchronous execution facility.
 932      *
 933      * @implSpec The default implementation invokes {@link #handle},
 934      * relaying to {@link #handleAsync} on exception, then {@link
 935      * #thenCompose} for result.
 936      *
 937      * @param fn the function to use to compute the returned
 938      * CompletionStage if this CompletionStage completed exceptionally
 939      * @return the new CompletionStage
 940      * @since 12
 941      */
 942     public default CompletionStage<T> exceptionallyComposeAsync
 943         (Function<Throwable, ? extends CompletionStage<T>> fn) {
 944         return handle((r, ex) -> (ex == null)
 945                       ? this
 946                       : this.handleAsync((r1, ex1) -> fn.apply(ex1))
 947                         .thenCompose(Function.identity()))
 948             .thenCompose(Function.identity());
 949     }
 950 
 951     /**
 952      * Returns a new CompletionStage that, when this stage completes
 953      * exceptionally, is composed using the results of the supplied
 954      * function applied to this stage's exception, using the
 955      * supplied Executor.
 956      *
 957      * @implSpec The default implementation invokes {@link #handle},
 958      * relaying to {@link #handleAsync} on exception, then {@link
 959      * #thenCompose} for result.
 960      *
 961      * @param fn the function to use to compute the returned
 962      * CompletionStage if this CompletionStage completed exceptionally
 963      * @param executor the executor to use for asynchronous execution
 964      * @return the new CompletionStage
 965      * @since 12
 966      */
 967     public default CompletionStage<T> exceptionallyComposeAsync
 968         (Function<Throwable, ? extends CompletionStage<T>> fn,
 969          Executor executor) {
 970         return handle((r, ex) -> (ex == null)
 971                       ? this
 972                       : this.handleAsync((r1, ex1) -> fn.apply(ex1), executor)
 973                         .thenCompose(Function.identity()))
 974             .thenCompose(Function.identity());
 975     }
 976 
 977     /**
 978      * Returns a {@link CompletableFuture} maintaining the same
 979      * completion properties as this stage. If this stage is already a
 980      * CompletableFuture, this method may return this stage itself.
 981      * Otherwise, invocation of this method may be equivalent in
 982      * effect to {@code thenApply(x -> x)}, but returning an instance
 983      * of type {@code CompletableFuture}.
 984      *
 985      * @return the CompletableFuture
 986      */
 987     public CompletableFuture<T> toCompletableFuture();
 988 
 989 }
< prev index next >