public class StageSupport extends Object
CompletionStages| Modifier and Type | Method | Description |
|---|---|---|
static <T> CompletionStage<T> |
completedStage(T t) |
Creates a
CompletionStage that is already completed with the given value. |
static <T> CompletionStage<T> |
exceptionalStage(Throwable ex) |
Creates a
CompletionStage that is already completed exceptionally. |
static <T,U> CompletionStage<U> |
thenComposeOrRecover(CompletionStage<T> stage,
BiFunction<? super T,Throwable,? extends CompletionStage<U>> fn) |
Uses the possibly exceptional result of
stage to produce a new stage. |
static <T,R extends AutoCloseable> |
tryComposeWith(CompletionStage<R> resource,
Function<? super R,? extends CompletionStage<T>> fn) |
Performs an asynchronous function with an asynchronously acquired
AutoCloseable,
ensuring that the resource is closed after the stage returned by
the function completes. |
static <T,R extends AsyncCloseable> |
tryComposeWith(R resource,
Function<? super R,? extends CompletionStage<T>> fn) |
Performs an asynchronous function with an
AsyncCloseable resource, ensuring that the
resource is AsyncCloseable.close() closed} after the stage returned by the function
completes. |
static <T,R extends AutoCloseable> |
tryWith(CompletionStage<R> resource,
Function<? super R,? extends T> fn) |
Performs a function with an asynchronously acquired
auto closeable,
ensuring that the resource is closed after the function runs. |
static <T,R extends AsyncCloseable> |
tryWith(R resource,
Function<? super R,? extends T> fn) |
Performs a function with an
AsyncCloseable resource, ensuring that the resource is
AsyncCloseable.close() closed} after the function completes. |
static <T> CompletionStage<Void> |
voided(CompletionStage<T> stage) |
Creates a
CompletionStage that completes when stage completes but ignores the
result. |
static CompletionStage<Void> |
voidStage() |
Gets an already completed
CompletionStage of Void. |
public static CompletionStage<Void> voidStage()
CompletionStage of Void. This common static instance can be
used as an alternative to StageSupport.<Void>completedStage(null)
This has a few advantages:
CompletionStage<Void> or an CompletionStage<T>. Using this method clearly indicates
that we are returning a void future, not a T future with a null result.
CompletionStage of Voidpublic static <T> CompletionStage<Void> voided(CompletionStage<T> stage)
CompletionStage that completes when stage completes but ignores the
result.stage - a non-void CompletionStageCompletionStage of type Void which completes when stage completespublic static <T> CompletionStage<T> completedStage(T t)
CompletionStage that is already completed with the given value.
Non-Async methods on the returned stage will run their dependent actions immediately on the calling thread.
Async methods which do not supply an executor (and don't involve another stage) will use the
same default executor as used by CompletableFuture. If another stage is involved (e.g.
thenAcceptBothAsync) then the other stage's default execution facility is used.
A completed exceptional stage can be similarly created with the method
exceptionalStage(Throwable)
t - the value to be held by the returned stageCompletionStage that has already been completed with texceptionalStage(Throwable)public static <T> CompletionStage<T> exceptionalStage(Throwable ex)
CompletionStage that is already completed exceptionally. This is the
exceptional analog of completedStage(Object).
Non-Async methods on the returned stage will run their dependent actions immediately on the calling thread.
Async methods which do not supply an executor (and don't involve another stage) will use the
same default executor as used by CompletableFuture. If another stage is involved (e.g.
thenAcceptBothAsync) then the other stage's default execution facility is used.
ex - the exception that completes the returned stageCompletionStage that has already been completed exceptionally with excompletedStage(Object)public static <T,R extends AutoCloseable> CompletionStage<T> tryWith(CompletionStage<R> resource, Function<? super R,? extends T> fn)
auto closeable,
ensuring that the resource is closed after the function runs.
Similar to a try-with-resources block, the resource will be closed even if fn throws an
exception.
The returned stage will complete exceptionally in the following scenarios
resource completes exceptionally
fn throws an exception
AutoCloseable.close() throws an exception
fn as a suppressed exception. If
AutoCloseable.close() throws a non-runtime exception, it will be wrapped in a
CompletionException.T - the result type of fnR - the AutoCloseable resource typeresource - a CompletionStage that completes with an AutoCloseablefn - an function to perform that uses result of resource to produce a valueCompletionStage that completes with the result of fn or completes
exceptionallypublic static <T,R extends AutoCloseable> CompletionStage<T> tryComposeWith(CompletionStage<R> resource, Function<? super R,? extends CompletionStage<T>> fn)
AutoCloseable,
ensuring that the resource is closed after the stage returned by
the function completes. Similar to a try-with-resources block, the resource will be closed even
if fn throws an exception or if the returned stage completes exceptionally.
The returned stage will complete exceptionally in the following scenarios
resource completes exceptionally
fn throws an exception
fn returns an stage that completes exceptionally
AutoCloseable.close() throws an exception
fn as a suppressed
exception. If AutoCloseable.close() throws a non-runtime exception, it will be wrapped
in a CompletionException.T - the type of the CompletionStage produced by fnR - the AutoCloseable resource typeresource - a CompletionStage that completes with an AutoCloseable which
will be {AutoCloseable.close() closed} when the stage returned by fn
completes.fn - an function to perform that uses result of resource to produce a new
CompletionStageCompletionStage that completes with the result of fn or completes
exceptionallypublic static <T,R extends AsyncCloseable> CompletionStage<T> tryWith(R resource, Function<? super R,? extends T> fn)
AsyncCloseable resource, ensuring that the resource is
AsyncCloseable.close() closed} after the function completes. The returned stage will
complete when the close stage has completed. Similar to a try-with-resources block, the
resource will be closed even if fn throws an exception.
The returned stage will complete exceptionally in the following scenarios
fn throws an exception
AutoCloseable.close() throws an exception
AutoCloseable.close() returns a stage that completes exceptionally
AsyncCloseable.close() produces an
exception the exception produced by close will be added to the exception from fn as a
suppressed exception.T - the type produced by fnR - the AsyncCloseable resource typeresource - an AsyncCloseable which will be {AsyncCloseable.close() closed}
after fn is runfn - a function to perform that uses resource to produce a resultCompletionStage that completes with the result of fn after close has
completed or completes exceptionallypublic static <T,R extends AsyncCloseable> CompletionStage<T> tryComposeWith(R resource, Function<? super R,? extends CompletionStage<T>> fn)
AsyncCloseable resource, ensuring that the
resource is AsyncCloseable.close() closed} after the stage returned by the function
completes. The returned stage will complete when the close stage has completed. Similar to a
try-with-resources block, the resource will be closed even if fn throws an exception or
if its returned stage completes exceptionally.
The returned stage will complete exceptionally in the following scenarios
fn throws an exception
fn returns a stage that completes exceptionally
AutoCloseable.close() throws an exception
AutoCloseable.close() returns a stage that completes exceptionally
AsyncCloseable.close() produces an exception (cases 3 or 4) the exception produced by
close will be added to the exception from fn as a
suppressed exception.T - the type of the CompletionStage produced by fnR - the AsyncCloseable resource typeresource - an AsyncCloseable which will be {AsyncCloseable.close() closed}
when the stage returned by fn completes.fn - a function to perform that uses resource to produce a new
CompletionStageCompletionStage that completes with the result of fn after close has
completed or completes exceptionallypublic static <T,U> CompletionStage<U> thenComposeOrRecover(CompletionStage<T> stage, BiFunction<? super T,Throwable,? extends CompletionStage<U>> fn)
stage to produce a new stage.
When stage completes, fn will be applied with the result of the stage (or null
if it completed exceptionally) and the exception from the stage (or null if it completed
normally) to produce a new stage. The returned stage will be completed with the outcome of the
stage produced by fn.
CompletionStage<Integer> backupValue(){...};
int compute(int i) {
if (i % 2 == 0)
return i / 2;
else
throw new OddException();
}
// get a stage that has the computed value if compute completes normally,
// or asynchronously computes the backup value otherwise.
CompletionStage<Optional<Integer>> optionalStage = thenComposeOrRecover(
() -> CompletableFuture.supplyAsync(compute(myInt)),
(result, throwable) -> throwable == null ? backupValue() : CompletableFuture.completedOf(result));
T - the type stageU - the type of the returned CompletionStagestage - a CompletionStage that may complete exceptionallyfn - a function that will run with the outcome of stage to produce a new
CompletionStageCompletionStage which will complete with the result of fnCompletionStage.handle(BiFunction)Copyright © 2018. All rights reserved.