/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * This file is available under and governed by the GNU General Public * License version 2 only, as published by the Free Software Foundation. * However, the following notice accompanied the original version of this * file: * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package java.util.concurrent; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; /** * A stage of a possibly asynchronous computation, that performs an * action or computes a value when another CompletionStage completes. * A stage completes upon termination of its computation, but this may * in turn trigger other dependent stages. The functionality defined * in this interface takes only a few basic forms, which expand out to * a larger set of methods to capture a range of usage styles: * * * *

All methods adhere to the above triggering, execution, and * exceptional completion specifications (which are not repeated in * individual method specifications). Additionally, while arguments * used to pass a completion result (that is, for parameters of type * {@code T}) for methods accepting them may be null, passing a null * value for any other parameter will result in a {@link * NullPointerException} being thrown. * *

Method form {@link #handle handle} is the most general way of * creating a continuation stage, unconditionally performing a * computation that is given both the result and exception (if any) of * the triggering CompletionStage, and computing an arbitrary result. * Method {@link #whenComplete whenComplete} is similar, but preserves * the result of the triggering stage instead of computing a new one. * Because a stage's normal result may be {@code null}, both methods * should have a computation structured thus: * *

{@code (result, exception) -> {
 *   if (exception == null) {
 *     // triggering stage completed normally
 *   } else {
 *     // triggering stage completed exceptionally
 *   }
 * }}
* *

This interface does not define methods for initially creating, * forcibly completing normally or exceptionally, probing completion * status or results, or awaiting completion of a stage. * Implementations of CompletionStage may provide means of achieving * such effects, as appropriate. Method {@link #toCompletableFuture} * enables interoperability among different implementations of this * interface by providing a common conversion type. * * @author Doug Lea * @since 1.8 */ public interface CompletionStage { /** * Returns a new CompletionStage that, when this stage completes * normally, is executed with this stage's result as the argument * to the supplied function. * *

This method is analogous to * {@link java.util.Optional#map Optional.map} and * {@link java.util.stream.Stream#map Stream.map}. * *

See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param fn the function to use to compute the value of the * returned CompletionStage * @param the function's return type * @return the new CompletionStage */ public CompletionStage thenApply(Function fn); /** * Returns a new CompletionStage that, when this stage completes * normally, is executed using this stage's default asynchronous * execution facility, with this stage's result as the argument to * the supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param fn the function to use to compute the value of the * returned CompletionStage * @param the function's return type * @return the new CompletionStage */ public CompletionStage thenApplyAsync (Function fn); /** * Returns a new CompletionStage that, when this stage completes * normally, is executed using the supplied Executor, with this * stage's result as the argument to the supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param fn the function to use to compute the value of the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @param the function's return type * @return the new CompletionStage */ public CompletionStage thenApplyAsync (Function fn, Executor executor); /** * Returns a new CompletionStage that, when this stage completes * normally, is executed with this stage's result as the argument * to the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage thenAccept(Consumer action); /** * Returns a new CompletionStage that, when this stage completes * normally, is executed using this stage's default asynchronous * execution facility, with this stage's result as the argument to * the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage thenAcceptAsync(Consumer action); /** * Returns a new CompletionStage that, when this stage completes * normally, is executed using the supplied Executor, with this * stage's result as the argument to the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param action the action to perform before completing the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @return the new CompletionStage */ public CompletionStage thenAcceptAsync(Consumer action, Executor executor); /** * Returns a new CompletionStage that, when this stage completes * normally, executes the given action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage thenRun(Runnable action); /** * Returns a new CompletionStage that, when this stage completes * normally, executes the given action using this stage's default * asynchronous execution facility. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage thenRunAsync(Runnable action); /** * Returns a new CompletionStage that, when this stage completes * normally, executes the given action using the supplied Executor. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param action the action to perform before completing the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @return the new CompletionStage */ public CompletionStage thenRunAsync(Runnable action, Executor executor); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, is executed with the two * results as arguments to the supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param fn the function to use to compute the value of the * returned CompletionStage * @param the type of the other CompletionStage's result * @param the function's return type * @return the new CompletionStage */ public CompletionStage thenCombine (CompletionStage other, BiFunction fn); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, is executed using this * stage's default asynchronous execution facility, with the two * results as arguments to the supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param fn the function to use to compute the value of the * returned CompletionStage * @param the type of the other CompletionStage's result * @param the function's return type * @return the new CompletionStage */ public CompletionStage thenCombineAsync (CompletionStage other, BiFunction fn); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, is executed using the * supplied executor, with the two results as arguments to the * supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param fn the function to use to compute the value of the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @param the type of the other CompletionStage's result * @param the function's return type * @return the new CompletionStage */ public CompletionStage thenCombineAsync (CompletionStage other, BiFunction fn, Executor executor); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, is executed with the two * results as arguments to the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @param the type of the other CompletionStage's result * @return the new CompletionStage */ public CompletionStage thenAcceptBoth (CompletionStage other, BiConsumer action); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, is executed using this * stage's default asynchronous execution facility, with the two * results as arguments to the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @param the type of the other CompletionStage's result * @return the new CompletionStage */ public CompletionStage thenAcceptBothAsync (CompletionStage other, BiConsumer action); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, is executed using the * supplied executor, with the two results as arguments to the * supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @param the type of the other CompletionStage's result * @return the new CompletionStage */ public CompletionStage thenAcceptBothAsync (CompletionStage other, BiConsumer action, Executor executor); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, executes the given action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage runAfterBoth(CompletionStage other, Runnable action); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, executes the given action * using this stage's default asynchronous execution facility. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage runAfterBothAsync(CompletionStage other, Runnable action); /** * Returns a new CompletionStage that, when this and the other * given stage both complete normally, executes the given action * using the supplied executor. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @return the new CompletionStage */ public CompletionStage runAfterBothAsync(CompletionStage other, Runnable action, Executor executor); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, is executed with the * corresponding result as argument to the supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param fn the function to use to compute the value of the * returned CompletionStage * @param the function's return type * @return the new CompletionStage */ public CompletionStage applyToEither (CompletionStage other, Function fn); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, is executed using this * stage's default asynchronous execution facility, with the * corresponding result as argument to the supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param fn the function to use to compute the value of the * returned CompletionStage * @param the function's return type * @return the new CompletionStage */ public CompletionStage applyToEitherAsync (CompletionStage other, Function fn); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, is executed using the * supplied executor, with the corresponding result as argument to * the supplied function. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param fn the function to use to compute the value of the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @param the function's return type * @return the new CompletionStage */ public CompletionStage applyToEitherAsync (CompletionStage other, Function fn, Executor executor); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, is executed with the * corresponding result as argument to the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage acceptEither (CompletionStage other, Consumer action); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, is executed using this * stage's default asynchronous execution facility, with the * corresponding result as argument to the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage acceptEitherAsync (CompletionStage other, Consumer action); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, is executed using the * supplied executor, with the corresponding result as argument to * the supplied action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @return the new CompletionStage */ public CompletionStage acceptEitherAsync (CompletionStage other, Consumer action, Executor executor); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, executes the given action. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage runAfterEither(CompletionStage other, Runnable action); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, executes the given action * using this stage's default asynchronous execution facility. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @return the new CompletionStage */ public CompletionStage runAfterEitherAsync (CompletionStage other, Runnable action); /** * Returns a new CompletionStage that, when either this or the * other given stage complete normally, executes the given action * using the supplied executor. * * See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param other the other CompletionStage * @param action the action to perform before completing the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @return the new CompletionStage */ public CompletionStage runAfterEitherAsync (CompletionStage other, Runnable action, Executor executor); /** * Returns a new CompletionStage that is completed with the same * value as the CompletionStage returned by the given function. * *

When this stage completes normally, the given function is * invoked with this stage's result as the argument, returning * another CompletionStage. When that stage completes normally, * the CompletionStage returned by this method is completed with * the same value. * *

To ensure progress, the supplied function must arrange * eventual completion of its result. * *

This method is analogous to * {@link java.util.Optional#flatMap Optional.flatMap} and * {@link java.util.stream.Stream#flatMap Stream.flatMap}. * *

See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param fn the function to use to compute another CompletionStage * @param the type of the returned CompletionStage's result * @return the new CompletionStage */ public CompletionStage thenCompose (Function> fn); /** * Returns a new CompletionStage that is completed with the same * value as the CompletionStage returned by the given function, * executed using this stage's default asynchronous execution * facility. * *

When this stage completes normally, the given function is * invoked with this stage's result as the argument, returning * another CompletionStage. When that stage completes normally, * the CompletionStage returned by this method is completed with * the same value. * *

To ensure progress, the supplied function must arrange * eventual completion of its result. * *

See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param fn the function to use to compute another CompletionStage * @param the type of the returned CompletionStage's result * @return the new CompletionStage */ public CompletionStage thenComposeAsync (Function> fn); /** * Returns a new CompletionStage that is completed with the same * value as the CompletionStage returned by the given function, * executed using the supplied Executor. * *

When this stage completes normally, the given function is * invoked with this stage's result as the argument, returning * another CompletionStage. When that stage completes normally, * the CompletionStage returned by this method is completed with * the same value. * *

To ensure progress, the supplied function must arrange * eventual completion of its result. * *

See the {@link CompletionStage} documentation for rules * covering exceptional completion. * * @param fn the function to use to compute another CompletionStage * @param executor the executor to use for asynchronous execution * @param the type of the returned CompletionStage's result * @return the new CompletionStage */ public CompletionStage thenComposeAsync (Function> fn, Executor executor); /** * Returns a new CompletionStage that, when this stage completes * either normally or exceptionally, is executed with this stage's * result and exception as arguments to the supplied function. * *

When this stage is complete, the given function is invoked * with the result (or {@code null} if none) and the exception (or * {@code null} if none) of this stage as arguments, and the * function's result is used to complete the returned stage. * * @param fn the function to use to compute the value of the * returned CompletionStage * @param the function's return type * @return the new CompletionStage */ public CompletionStage handle (BiFunction fn); /** * Returns a new CompletionStage that, when this stage completes * either normally or exceptionally, is executed using this stage's * default asynchronous execution facility, with this stage's * result and exception as arguments to the supplied function. * *

When this stage is complete, the given function is invoked * with the result (or {@code null} if none) and the exception (or * {@code null} if none) of this stage as arguments, and the * function's result is used to complete the returned stage. * * @param fn the function to use to compute the value of the * returned CompletionStage * @param the function's return type * @return the new CompletionStage */ public CompletionStage handleAsync (BiFunction fn); /** * Returns a new CompletionStage that, when this stage completes * either normally or exceptionally, is executed using the * supplied executor, with this stage's result and exception as * arguments to the supplied function. * *

When this stage is complete, the given function is invoked * with the result (or {@code null} if none) and the exception (or * {@code null} if none) of this stage as arguments, and the * function's result is used to complete the returned stage. * * @param fn the function to use to compute the value of the * returned CompletionStage * @param executor the executor to use for asynchronous execution * @param the function's return type * @return the new CompletionStage */ public CompletionStage handleAsync (BiFunction fn, Executor executor); /** * Returns a new CompletionStage with the same result or exception as * this stage, that executes the given action when this stage completes. * *

When this stage is complete, the given action is invoked * with the result (or {@code null} if none) and the exception (or * {@code null} if none) of this stage as arguments. The returned * stage is completed when the action returns. * *

Unlike method {@link #handle handle}, * this method is not designed to translate completion outcomes, * so the supplied action should not throw an exception. However, * if it does, the following rules apply: if this stage completed * normally but the supplied action throws an exception, then the * returned stage completes exceptionally with the supplied * action's exception. Or, if this stage completed exceptionally * and the supplied action throws an exception, then the returned * stage completes exceptionally with this stage's exception. * * @param action the action to perform * @return the new CompletionStage */ public CompletionStage whenComplete (BiConsumer action); /** * Returns a new CompletionStage with the same result or exception as * this stage, that executes the given action using this stage's * default asynchronous execution facility when this stage completes. * *

When this stage is complete, the given action is invoked with the * result (or {@code null} if none) and the exception (or {@code null} * if none) of this stage as arguments. The returned stage is completed * when the action returns. * *

Unlike method {@link #handleAsync(BiFunction) handleAsync}, * this method is not designed to translate completion outcomes, * so the supplied action should not throw an exception. However, * if it does, the following rules apply: If this stage completed * normally but the supplied action throws an exception, then the * returned stage completes exceptionally with the supplied * action's exception. Or, if this stage completed exceptionally * and the supplied action throws an exception, then the returned * stage completes exceptionally with this stage's exception. * * @param action the action to perform * @return the new CompletionStage */ public CompletionStage whenCompleteAsync (BiConsumer action); /** * Returns a new CompletionStage with the same result or exception as * this stage, that executes the given action using the supplied * Executor when this stage completes. * *

When this stage is complete, the given action is invoked with the * result (or {@code null} if none) and the exception (or {@code null} * if none) of this stage as arguments. The returned stage is completed * when the action returns. * *

Unlike method {@link #handleAsync(BiFunction,Executor) handleAsync}, * this method is not designed to translate completion outcomes, * so the supplied action should not throw an exception. However, * if it does, the following rules apply: If this stage completed * normally but the supplied action throws an exception, then the * returned stage completes exceptionally with the supplied * action's exception. Or, if this stage completed exceptionally * and the supplied action throws an exception, then the returned * stage completes exceptionally with this stage's exception. * * @param action the action to perform * @param executor the executor to use for asynchronous execution * @return the new CompletionStage */ public CompletionStage whenCompleteAsync (BiConsumer action, Executor executor); /** * Returns a new CompletionStage that, when this stage completes * exceptionally, is executed with this stage's exception as the * argument to the supplied function. Otherwise, if this stage * completes normally, then the returned stage also completes * normally with the same value. * * @param fn the function to use to compute the value of the * returned CompletionStage if this CompletionStage completed * exceptionally * @return the new CompletionStage */ public CompletionStage exceptionally (Function fn); /** * Returns a new CompletionStage that, when this stage completes * exceptionally, is executed with this stage's exception as the * argument to the supplied function, using this stage's default * asynchronous execution facility. Otherwise, if this stage * completes normally, then the returned stage also completes * normally with the same value. * * @implSpec The default implementation invokes {@link #handle}, * relaying to {@link #handleAsync} on exception, then {@link * #thenCompose} for result. * * @param fn the function to use to compute the value of the * returned CompletionStage if this CompletionStage completed * exceptionally * @return the new CompletionStage * @since 12 */ public default CompletionStage exceptionallyAsync (Function fn) { return handle((r, ex) -> (ex == null) ? this : this.handleAsync((r1, ex1) -> fn.apply(ex1))) .thenCompose(Function.identity()); } /** * Returns a new CompletionStage that, when this stage completes * exceptionally, is executed with this stage's exception as the * argument to the supplied function, using the supplied Executor. * Otherwise, if this stage completes normally, then the returned * stage also completes normally with the same value. * * @implSpec The default implementation invokes {@link #handle}, * relaying to {@link #handleAsync} on exception, then {@link * #thenCompose} for result. * * @param fn the function to use to compute the value of the * returned CompletionStage if this CompletionStage completed * exceptionally * @param executor the executor to use for asynchronous execution * @return the new CompletionStage * @since 12 */ public default CompletionStage exceptionallyAsync (Function fn, Executor executor) { return handle((r, ex) -> (ex == null) ? this : this.handleAsync((r1, ex1) -> fn.apply(ex1), executor)) .thenCompose(Function.identity()); } /** * Returns a new CompletionStage that, when this stage completes * exceptionally, is composed using the results of the supplied * function applied to this stage's exception. * * @implSpec The default implementation invokes {@link #handle}, * invoking the given function on exception, then {@link * #thenCompose} for result. * * @param fn the function to use to compute the returned * CompletionStage if this CompletionStage completed exceptionally * @return the new CompletionStage * @since 12 */ public default CompletionStage exceptionallyCompose (Function> fn) { return handle((r, ex) -> (ex == null) ? this : fn.apply(ex)) .thenCompose(Function.identity()); } /** * Returns a new CompletionStage that, when this stage completes * exceptionally, is composed using the results of the supplied * function applied to this stage's exception, using this stage's * default asynchronous execution facility. * * @implSpec The default implementation invokes {@link #handle}, * relaying to {@link #handleAsync} on exception, then {@link * #thenCompose} for result. * * @param fn the function to use to compute the returned * CompletionStage if this CompletionStage completed exceptionally * @return the new CompletionStage * @since 12 */ public default CompletionStage exceptionallyComposeAsync (Function> fn) { return handle((r, ex) -> (ex == null) ? this : this.handleAsync((r1, ex1) -> fn.apply(ex1)) .thenCompose(Function.identity())) .thenCompose(Function.identity()); } /** * Returns a new CompletionStage that, when this stage completes * exceptionally, is composed using the results of the supplied * function applied to this stage's exception, using the * supplied Executor. * * @implSpec The default implementation invokes {@link #handle}, * relaying to {@link #handleAsync} on exception, then {@link * #thenCompose} for result. * * @param fn the function to use to compute the returned * CompletionStage if this CompletionStage completed exceptionally * @param executor the executor to use for asynchronous execution * @return the new CompletionStage * @since 12 */ public default CompletionStage exceptionallyComposeAsync (Function> fn, Executor executor) { return handle((r, ex) -> (ex == null) ? this : this.handleAsync((r1, ex1) -> fn.apply(ex1), executor) .thenCompose(Function.identity())) .thenCompose(Function.identity()); } /** * Returns a {@link CompletableFuture} maintaining the same * completion properties as this stage. If this stage is already a * CompletableFuture, this method may return this stage itself. * Otherwise, invocation of this method may be equivalent in * effect to {@code thenApply(x -> x)}, but returning an instance * of type {@code CompletableFuture}. * * @return the CompletableFuture */ public CompletableFuture toCompletableFuture(); }