public interface CompletionStep<T>
stage.thenApply(x -> square(x))
.thenAccept(x -> System.out.print(x))
.thenRun(() -> System.out.println());
An additional form (compose) allows the construction of computation pipelines from functions returning completion stages.
Any argument to a stage's computation is the outcome of a triggering stage's computation.
catch keyword.
In all other cases, if a stage's computation terminates abruptly
with an (unchecked) exception or error, then all dependent stages
requiring its completion complete exceptionally as well, with a
{ CompletionException} holding the exception as its cause. If
a stage is dependent on both of two stages, and both
complete exceptionally, then the CompletionException may correspond
to either one of these exceptions. If a stage is dependent on
either of two others, and only one of them completes
exceptionally, no guarantees are made about whether the dependent
stage completes normally or exceptionally. In the case of method
whenComplete, when the supplied action itself encounters an
exception, then the stage completes exceptionally with this
exception unless the source stage also completed exceptionally, in
which case the exceptional completion from the source stage is
given preference and propagated to the dependent stage.
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
T) for methods accepting them may be null, passing a null
value for any other parameter will result in a {
NullPointerException} being thrown.
Method form { #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 { #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 null, both methods
should have a computation structured thus:
(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 { #toCompletableFuture} enables interoperability among different implementations of this interface by providing a common conversion type.
| 限定符和类型 | 方法和说明 |
|---|---|
CompletionStep<Void> |
acceptEither(CompletionStep<? extends T> other,
Consumer<? super T> action)
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.
|
CompletionStep<Void> |
acceptEitherAsync(CompletionStep<? extends T> other,
Consumer<? super T> 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.
|
CompletionStep<Void> |
acceptEitherAsync(CompletionStep<? extends T> other,
Consumer<? super T> action,
Executor executor)
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.
|
<U> CompletionStep<U> |
applyToEither(CompletionStep<? extends T> other,
Function<? super T,U> fn)
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.
|
<U> CompletionStep<U> |
applyToEitherAsync(CompletionStep<? extends T> other,
Function<? super T,U> 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.
|
<U> CompletionStep<U> |
applyToEitherAsync(CompletionStep<? extends T> other,
Function<? super T,U> fn,
Executor executor)
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.
|
CompletionStep<T> |
exceptionally(Function<Throwable,? extends T> 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.
|
<U> CompletionStep<U> |
handle(Function2<? super T,Throwable,? extends U> fn)
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.
|
<U> CompletionStep<U> |
handleAsync(Function2<? super T,Throwable,? extends U> 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.
|
<U> CompletionStep<U> |
handleAsync(Function2<? super T,Throwable,? extends U> fn,
Executor executor)
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.
|
CompletionStep<Void> |
runAfterBoth(CompletionStep<?> other,
Runnable action)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, executes the given action.
|
CompletionStep<Void> |
runAfterBothAsync(CompletionStep<?> 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.
|
CompletionStep<Void> |
runAfterBothAsync(CompletionStep<?> other,
Runnable action,
Executor executor)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, executes the given action
using the supplied executor.
|
CompletionStep<Void> |
runAfterEither(CompletionStep<?> other,
Runnable action)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, executes the given action.
|
CompletionStep<Void> |
runAfterEitherAsync(CompletionStep<?> 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.
|
CompletionStep<Void> |
runAfterEitherAsync(CompletionStep<?> other,
Runnable action,
Executor executor)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, executes the given action
using the supplied executor.
|
CompletionStep<Void> |
thenAccept(Consumer<? super T> action)
Returns a new CompletionStage that, when this stage completes
normally, is executed with this stage's result as the argument
to the supplied action.
|
CompletionStep<Void> |
thenAcceptAsync(Consumer<? super T> 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.
|
CompletionStep<Void> |
thenAcceptAsync(Consumer<? super T> action,
Executor executor)
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.
|
<U> CompletionStep<Void> |
thenAcceptBoth(CompletionStep<? extends U> other,
Consumer2<? super T,? super U> action)
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.
|
<U> CompletionStep<Void> |
thenAcceptBothAsync(CompletionStep<? extends U> other,
Consumer2<? super T,? super U> 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.
|
<U> CompletionStep<Void> |
thenAcceptBothAsync(CompletionStep<? extends U> other,
Consumer2<? super T,? super U> action,
Executor executor)
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.
|
<U> CompletionStep<U> |
thenApply(Function<? super T,? extends U> fn)
Returns a new CompletionStage that, when this stage completes
normally, is executed with this stage's result as the argument
to the supplied function.
|
<U> CompletionStep<U> |
thenApplyAsync(Function<? super T,? extends U> 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.
|
<U> CompletionStep<U> |
thenApplyAsync(Function<? super T,? extends U> fn,
Executor executor)
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.
|
<U,V> CompletionStep<V> |
thenCombine(CompletionStep<? extends U> other,
Function2<? super T,? super U,? extends V> fn)
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.
|
<U,V> CompletionStep<V> |
thenCombineAsync(CompletionStep<? extends U> other,
Function2<? super T,? super U,? extends V> 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.
|
<U,V> CompletionStep<V> |
thenCombineAsync(CompletionStep<? extends U> other,
Function2<? super T,? super U,? extends V> fn,
Executor executor)
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.
|
<U> CompletionStep<U> |
thenCompose(Function<? super T,? extends CompletionStep<U>> fn)
Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function.
|
<U> CompletionStep<U> |
thenComposeAsync(Function<? super T,? extends CompletionStep<U>> 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.
|
<U> CompletionStep<U> |
thenComposeAsync(Function<? super T,? extends CompletionStep<U>> fn,
Executor executor)
Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function,
executed using the supplied Executor.
|
CompletionStep<Void> |
thenRun(Runnable action)
Returns a new CompletionStage that, when this stage completes
normally, executes the given action.
|
CompletionStep<Void> |
thenRunAsync(Runnable action)
Returns a new CompletionStage that, when this stage completes
normally, executes the given action using this stage's default
asynchronous execution facility.
|
CompletionStep<Void> |
thenRunAsync(Runnable action,
Executor executor)
Returns a new CompletionStage that, when this stage completes
normally, executes the given action using the supplied Executor.
|
CompletableFuture<T> |
toCompletableFuture()
Returns a { CompletableFuture} maintaining the same
completion properties as this stage.
|
CompletionStep<T> |
whenComplete(Consumer2<? super T,? super Throwable> action)
Returns a new CompletionStage with the same result or exception as
this stage, that executes the given action when this stage completes.
|
CompletionStep<T> |
whenCompleteAsync(Consumer2<? super T,? super Throwable> 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.
|
CompletionStep<T> |
whenCompleteAsync(Consumer2<? super T,? super Throwable> action,
Executor executor)
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.
|
<U> CompletionStep<U> thenApply(Function<? super T,? extends U> fn)
This method is analogous to { java.util.Optional#map Optional.map} and { java.util.stream.Stream#map Stream.map}.
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the function's return typefn - the function to use to compute the value of the
returned CompletionStage<U> CompletionStep<U> thenApplyAsync(Function<? super T,? extends U> fn)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the function's return typefn - the function to use to compute the value of the
returned CompletionStage<U> CompletionStep<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the function's return typefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous executionCompletionStep<Void> thenAccept(Consumer<? super T> action)
See the { CompletionStep} documentation for rules covering exceptional completion.
action - the action to perform before completing the
returned CompletionStageCompletionStep<Void> thenAcceptAsync(Consumer<? super T> action)
See the { CompletionStep} documentation for rules covering exceptional completion.
action - the action to perform before completing the
returned CompletionStageCompletionStep<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
action - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionCompletionStep<Void> thenRun(Runnable action)
See the { CompletionStep} documentation for rules covering exceptional completion.
action - the action to perform before completing the
returned CompletionStageCompletionStep<Void> thenRunAsync(Runnable action)
See the { CompletionStep} documentation for rules covering exceptional completion.
action - the action to perform before completing the
returned CompletionStageCompletionStep<Void> thenRunAsync(Runnable action, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
action - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous execution<U,V> CompletionStep<V> thenCombine(CompletionStep<? extends U> other, Function2<? super T,? super U,? extends V> fn)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the other CompletionStage's resultV - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStage<U,V> CompletionStep<V> thenCombineAsync(CompletionStep<? extends U> other, Function2<? super T,? super U,? extends V> fn)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the other CompletionStage's resultV - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStage<U,V> CompletionStep<V> thenCombineAsync(CompletionStep<? extends U> other, Function2<? super T,? super U,? extends V> fn, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the other CompletionStage's resultV - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous execution<U> CompletionStep<Void> thenAcceptBoth(CompletionStep<? extends U> other, Consumer2<? super T,? super U> action)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the other CompletionStage's resultother - the other CompletionStageaction - the action to perform before completing the
returned CompletionStage<U> CompletionStep<Void> thenAcceptBothAsync(CompletionStep<? extends U> other, Consumer2<? super T,? super U> action)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the other CompletionStage's resultother - the other CompletionStageaction - the action to perform before completing the
returned CompletionStage<U> CompletionStep<Void> thenAcceptBothAsync(CompletionStep<? extends U> other, Consumer2<? super T,? super U> action, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the other CompletionStage's resultother - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionCompletionStep<Void> runAfterBoth(CompletionStep<?> other, Runnable action)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageCompletionStep<Void> runAfterBothAsync(CompletionStep<?> other, Runnable action)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageCompletionStep<Void> runAfterBothAsync(CompletionStep<?> other, Runnable action, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous execution<U> CompletionStep<U> applyToEither(CompletionStep<? extends T> other, Function<? super T,U> fn)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStage<U> CompletionStep<U> applyToEitherAsync(CompletionStep<? extends T> other, Function<? super T,U> fn)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStage<U> CompletionStep<U> applyToEitherAsync(CompletionStep<? extends T> other, Function<? super T,U> fn, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous executionCompletionStep<Void> acceptEither(CompletionStep<? extends T> other, Consumer<? super T> action)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageCompletionStep<Void> acceptEitherAsync(CompletionStep<? extends T> other, Consumer<? super T> action)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageCompletionStep<Void> acceptEitherAsync(CompletionStep<? extends T> other, Consumer<? super T> action, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionCompletionStep<Void> runAfterEither(CompletionStep<?> other, Runnable action)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageCompletionStep<Void> runAfterEitherAsync(CompletionStep<?> other, Runnable action)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageCompletionStep<Void> runAfterEitherAsync(CompletionStep<?> other, Runnable action, Executor executor)
See the { CompletionStep} documentation for rules covering exceptional completion.
other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous execution<U> CompletionStep<U> thenCompose(Function<? super T,? extends CompletionStep<U>> fn)
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 { java.util.Optional#flatMap Optional.flatMap} and { java.util.stream.Stream#flatMap Stream.flatMap}.
See the { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the returned CompletionStage's resultfn - the function to use to compute another CompletionStage<U> CompletionStep<U> thenComposeAsync(Function<? super T,? extends CompletionStep<U>> fn)
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 { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the returned CompletionStage's resultfn - the function to use to compute another CompletionStage<U> CompletionStep<U> thenComposeAsync(Function<? super T,? extends CompletionStep<U>> fn, Executor 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 { CompletionStep} documentation for rules covering exceptional completion.
U - the type of the returned CompletionStage's resultfn - the function to use to compute another CompletionStageexecutor - the executor to use for asynchronous execution<U> CompletionStep<U> handle(Function2<? super T,Throwable,? extends U> fn)
When this stage is complete, the given function is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments, and the
function's result is used to complete the returned stage.
U - the function's return typefn - the function to use to compute the value of the
returned CompletionStage<U> CompletionStep<U> handleAsync(Function2<? super T,Throwable,? extends U> fn)
When this stage is complete, the given function is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments, and the
function's result is used to complete the returned stage.
U - the function's return typefn - the function to use to compute the value of the
returned CompletionStage<U> CompletionStep<U> handleAsync(Function2<? super T,Throwable,? extends U> fn, Executor executor)
When this stage is complete, the given function is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments, and the
function's result is used to complete the returned stage.
U - the function's return typefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous executionCompletionStep<T> whenComplete(Consumer2<? super T,? super Throwable> action)
When this stage is complete, the given action is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments. The returned
stage is completed when the action returns.
Unlike method { #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.
action - the action to performCompletionStep<T> whenCompleteAsync(Consumer2<? super T,? super Throwable> action)
When this stage is complete, the given action is invoked with the
result (or null if none) and the exception (or null
if none) of this stage as arguments. The returned stage is completed
when the action returns.
Unlike method { #handleAsync(Function2) 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.
action - the action to performCompletionStep<T> whenCompleteAsync(Consumer2<? super T,? super Throwable> action, Executor executor)
When this stage is complete, the given action is invoked with the
result (or null if none) and the exception (or null
if none) of this stage as arguments. The returned stage is completed
when the action returns.
Unlike method { #handleAsync(Function2,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.
action - the action to performexecutor - the executor to use for asynchronous executionCompletionStep<T> exceptionally(Function<Throwable,? extends T> fn)
fn - the function to use to compute the value of the
returned CompletionStage if this CompletionStage completed
exceptionallyCompletableFuture<T> toCompletableFuture()
thenApply(x -> x), but returning an instance
of type CompletableFuture.Copyright © 2020. All rights reserved.