public final class WhenReady
extends java.lang.Object
Three ways to hook async processing on sync APIs are offered:
completeAsync
- returns a future that gets completed when a predicate reports that a sync API
is ready to be consumed without blocking. The caller can hook any continuation on the returned future.
This is the lowest-level pattern. It offers the highest degree of flexibility.
applyAsync
- executes a sync action asynchronously once a predicate reports that the action is
ready to be called. The returned future gets completed after the action has executed and finished.
This is implemented as a continuation of completeAsync(java.util.function.Predicate<S>, R, S)
that executes the given action.
startApplyLoopAsync
- starts executing a sync action asynchronously once a predicate
reports that the action is ready to be called, and continues until another predicate reports that the action should no
longer be called. The returned future gets completed after the loop has finished.
This is implemented as a loop of completeAsync(java.util.function.Predicate<S>, R, S)
and a continuation that executes the given action.
Modifier and Type | Method and Description |
---|---|
static <S,R> java.util.concurrent.CompletableFuture<R> |
applyAsync(java.util.function.Predicate<S> ready,
java.util.function.Function<S,R> action,
S state)
Returns a future that represents the continuation of
completeAsync(java.util.function.Predicate<S>, R, S) that executes
action. |
static <S,R> java.util.concurrent.CompletableFuture<R> |
applyAsync(java.util.function.Predicate<S> ready,
java.util.function.Function<S,R> action,
S state,
AsyncOptions asyncOptions)
Returns a future that represents the continuation of
completeAsync(java.util.function.Predicate<S>, R, S) that executes
action. |
static <S,R> java.util.concurrent.CompletableFuture<R> |
completeAsync(java.util.function.Predicate<S> ready,
R result,
S state)
Returns a future that gets completed with a value of result when the ready predicate returns true.
|
static <S,R> java.util.concurrent.CompletableFuture<R> |
completeAsync(java.util.function.Predicate<S> ready,
R result,
S state,
AsyncOptions asyncOptions)
Returns a future that gets completed with a value of result when the ready predicate returns true.
|
static <S,R> java.util.concurrent.CompletableFuture<R> |
startApplyLoopAsync(java.util.function.Predicate<S> ready,
java.util.function.Predicate<S> done,
java.util.function.Function<S,R> action,
S state)
Returns a future that represents a loop of continuations of
completeAsync(java.util.function.Predicate<S>, R, S) that execute
action. |
static <S,R> java.util.concurrent.CompletableFuture<R> |
startApplyLoopAsync(java.util.function.Predicate<S> ready,
java.util.function.Predicate<S> done,
java.util.function.Function<S,R> action,
S state,
AsyncOptions asyncOptions)
Returns a future that represents a loop of continuations of
completeAsync(java.util.function.Predicate<S>, R, S) that execute
action. |
public static <S,R> java.util.concurrent.CompletableFuture<R> completeAsync(java.util.function.Predicate<S> ready, R result, S state)
This overload uses default AsyncOptions
.
S
- Type of state passed in to ready.R
- Type of result set on the future upon completion.ready
- A predicate that should return true when a given sync API is ready to be consumed without blocking.result
- The value that should be set on the future upon completion.state
- State that is passed to the predicate in order for it to make the right decision. May be null.public static <S,R> java.util.concurrent.CompletableFuture<R> completeAsync(java.util.function.Predicate<S> ready, R result, S state, AsyncOptions asyncOptions)
S
- Type of state passed in to ready.R
- Type of result set on the future upon completion.ready
- A predicate that should return true when a given sync API is ready to be consumed without blocking.result
- The value that should be set on the future upon completion.state
- State that is passed to the predicate in order for it to make the right decision. May be null.asyncOptions
- AsyncOptions
that control this async call.public static <S,R> java.util.concurrent.CompletableFuture<R> applyAsync(java.util.function.Predicate<S> ready, java.util.function.Function<S,R> action, S state)
completeAsync(java.util.function.Predicate<S>, R, S)
that executes
action. The returned future gets completed with the result of action.
This overload uses default AsyncOptions
.
S
- Type of state passed in to ready as well as to action.R
- Type of the result returned from action set on the future upon completion.ready
- A predicate that should return true when a given sync API is ready to be consumed without blocking.action
- A function that gets called once ready returns true, and whose result is used to complete the returned future.state
- State that is passed to ready as well as to action. May be null.public static <S,R> java.util.concurrent.CompletableFuture<R> applyAsync(java.util.function.Predicate<S> ready, java.util.function.Function<S,R> action, S state, AsyncOptions asyncOptions)
completeAsync(java.util.function.Predicate<S>, R, S)
that executes
action. The returned future gets completed with the result of action.S
- Type of state passed in to ready as well as to action.R
- Type of the result returned from action set on the future upon completion.ready
- A predicate that should return true when a given sync API is ready to be consumed without blocking.action
- A function that gets called once ready returns true, and whose result is used to complete the returned future.state
- State that is passed to ready as well as to action. May be null.asyncOptions
- AsyncOptions
that control this async call.public static <S,R> java.util.concurrent.CompletableFuture<R> startApplyLoopAsync(java.util.function.Predicate<S> ready, java.util.function.Predicate<S> done, java.util.function.Function<S,R> action, S state)
completeAsync(java.util.function.Predicate<S>, R, S)
that execute
action. The loop ends when done returns true.
The returned future gets completed with the result from the last invocation of action.
This overload uses default AsyncOptions
.
S
- Type of state passed in to ready as well as to action.R
- Type of the result returned from action set on the future upon completion.ready
- A predicate that should return true when a given sync API is ready to be consumed without blocking.done
- A predicate that should return true when a given sync API should no longer be consumed.action
- A function that gets called once ready returns true, and whose result is used to complete the returned future.state
- State that is passed to ready as well as to action. May be null.public static <S,R> java.util.concurrent.CompletableFuture<R> startApplyLoopAsync(java.util.function.Predicate<S> ready, java.util.function.Predicate<S> done, java.util.function.Function<S,R> action, S state, AsyncOptions asyncOptions)
completeAsync(java.util.function.Predicate<S>, R, S)
that execute
action. The loop ends when done returns true.
The returned future gets completed with the result from the last invocation of action.S
- Type of state passed in to ready as well as to action.R
- Type of the result returned from action set on the future upon completion.ready
- A predicate that should return true when a given sync API is ready to be consumed without blocking.done
- A predicate that should return true when a given sync API should no longer be consumed.action
- A function that gets called once ready returns true, and whose result is used to complete the returned future.state
- State that is passed to ready as well as to action. May be null.asyncOptions
- AsyncOptions
that control this async call.