public class PromiseStage<T> extends PromiseBase<T>
CompletionStage
. As the callee controls how new dependant Promise objects are created,
said implementation will also control the threading, etc, of dependant stages.
The promise state and value is retrieved by way of callback, meaning that, provided the CompletionStage
implementation is correct, this promise will always be resolved before resolving promises registered with then, etc.
This class is provided more as a way to enable inter-operation with other libraries, then as a recommended way to use promises.
The thread-safe implementation of state is implemented by PromiseBase
.
Modifier and Type | Field and Description |
---|---|
protected java.util.concurrent.Executor |
executor |
protected java.util.concurrent.CompletionStage<T> |
stage |
lock
Constructor and Description |
---|
PromiseStage(java.util.concurrent.CompletionStage<T> stage) |
PromiseStage(java.util.concurrent.CompletionStage<T> stage,
java.util.concurrent.Executor executor) |
Modifier and Type | Method and Description |
---|---|
<U> Promise<U> |
always(java.util.function.BiFunction<? super T,java.lang.Throwable,? extends Promise<? extends U>> callback)
Specify a callback that will always run on resolution, or as soon as possible if
this is already in
a resolved state. |
Promise<T> |
except(java.util.function.BiConsumer<java.lang.Throwable,java.util.function.Consumer<? super T>> callback)
Specify a callback to be run if this resolves with a failed state
REJECTED , and return a new promise,
that will fulfill with the value accepted within the callback, into it's second argument, a Consumer . |
Promise<T> |
except(java.util.function.Function<java.lang.Throwable,? extends Promise<? extends T>> callback)
Specify a callback to be run if this resolves with a failed state
REJECTED , and return a new promise,
that will resolve with the same state and value as the callback's returned promise, after the callback
promise resolves. |
java.util.concurrent.Executor |
getExecutor() |
java.util.concurrent.CompletionStage<T> |
getStage() |
<U> Promise<U> |
then(java.util.function.BiConsumer<? super T,java.util.function.Consumer<? super U>> callback)
Specify a callback to be run on successful resolution
FULFILLED of this, and return a new promise,
that will fulfill with the value accepted within the callback, into it's second argument, a Consumer . |
<U> Promise<U> |
then(java.util.function.Function<? super T,? extends Promise<? extends U>> callback)
Specify a callback to be run on successful resolution
FULFILLED of this, and return a new promise,
that will resolve with the same state and value as the callback's returned promise, after the callback
promise resolves. |
static <T> Promise<T> |
wrap(java.util.concurrent.CompletionStage<T> stage,
java.util.concurrent.Executor executor,
Promise<? extends T> promise)
Use a completed
CompletionStage as a base, create a new PromiseStage , that will resolve
with the same state and value as the provided Promise . |
exceptSync, fulfill, getException, getState, getValue, reject, resolve, sync, thenSync
protected final java.util.concurrent.CompletionStage<T> stage
protected final java.util.concurrent.Executor executor
public PromiseStage(java.util.concurrent.CompletionStage<T> stage)
public PromiseStage(java.util.concurrent.CompletionStage<T> stage, java.util.concurrent.Executor executor)
public java.util.concurrent.CompletionStage<T> getStage()
public java.util.concurrent.Executor getExecutor()
public <U> Promise<U> then(java.util.function.Function<? super T,? extends Promise<? extends U>> callback)
Promise
FULFILLED
of this, and return a new promise,
that will resolve with the same state and value as the callback's returned promise, after the callback
promise resolves. The input parameter will be the FULFILLED
value of this
.
The callback will be run as soon as possible (but not inline) if this
is already FULFILLED
.
If the callback returns a null
value, then the returned promise will resolve as FULFILLED
with
value null
.
If an exception is thrown within the callback, then the returned promise will be REJECTED
, with that
exception.
If this
resolved with the REJECTED
state, then the returned promise will reflect the state and
value of this
, and the callback will not be run.
U
- The return type of the new promise, dictated by the callback promise's return type.callback
- The operation which will be performed if this
resolves successfully.public <U> Promise<U> then(java.util.function.BiConsumer<? super T,java.util.function.Consumer<? super U>> callback)
Promise
FULFILLED
of this, and return a new promise,
that will fulfill with the value accepted within the callback, into it's second argument, a Consumer
.
The callback will be run as soon as possible (but not inline) if this
is already FULFILLED
.
If no call is made to this second argument within the callback, then the resolved value of the returned
promise will be null
, and the state FULFILLED
.
If this
resolves as REJECTED
callback will not be called, and the returned promise will
reject with the same Throwable
.
If an exception is thrown within the callback, then the returned promise will be REJECTED
with that
exception, provided that it has not already resolved.
For example:
Promise<Integer> input = // some async operation which eventually fulfills with int(5)
Promise<Integer> output = input.then((inputValue, fulfill) -> fulfill.accept(inputValue + 10));
// will output 15
System.out.println(output.thenSync());
U
- The return type of the new promise.callback
- The operation which will be performed if the promise resolves successfully.public Promise<T> except(java.util.function.Function<java.lang.Throwable,? extends Promise<? extends T>> callback)
Promise
REJECTED
, and return a new promise,
that will resolve with the same state and value as the callback's returned promise, after the callback
promise resolves. The input parameter will be the REJECTED
value of this
(a Throwable
).
To preserve type safety, the returned type of promise within the callback is restricted to things which can be
cast to the type of T
, unlike Promise.then(Function)
, which can allow any type, due to the fact that the
no-callback case will only ever result in a Throwable
.
The callback will be run as soon as possible (but not inline) if this
is already REJECTED
.
If the callback returns a null
value, then the returned promise will resolve as FULFILLED
with
value null
.
If an exception is thrown within the callback, then the returned promise will be REJECTED
, with that
exception.
If this
resolved with the FULFILLED
state, then the returned promise will reflect the state and
value of this
, and the callback will not be run.
callback
- The operation which will be performed if the promise resolves exceptionally.public Promise<T> except(java.util.function.BiConsumer<java.lang.Throwable,java.util.function.Consumer<? super T>> callback)
Promise
REJECTED
, and return a new promise,
that will fulfill with the value accepted within the callback, into it's second argument, a Consumer
.
To preserve type safety, the type of fulfillment value within the callback is restricted, to things which can be
cast to the type of T
, unlike Promise.then(BiConsumer)
, which can allow any type, due to the fact that
the no-callback case will only ever result in a Throwable
.
The callback will be run as soon as possible (but not inline) if this
is already REJECTED
.
If no call is made to this second argument within the callback, then the resolved value of the returned
promise will be null
, and the state FULFILLED
.
If this
resolves as FULFILLED
callback will not be called, and the returned promise will
fulfill with the same value.
If an exception is thrown within the callback, then the returned promise will be REJECTED
with that
exception, provided that it has not already resolved.
For example:
Promise<Object> input = // some async operation which eventually rejects with an exception
Promise<Object> output = input.except((exception, fulfill) -> fulfill.accept(exception));
// will output the string representation of the FULFILLED exception originally REJECTED by input
System.out.println(output.thenSync());
callback
- The operation which will be performed if the promise resolves exceptionally.public <U> Promise<U> always(java.util.function.BiFunction<? super T,java.lang.Throwable,? extends Promise<? extends U>> callback)
Promise
this
is already in
a resolved state. Returns a new promise, that will resolve with the same state and value as the callback's return
value (a promise), after the returned promise resolves.
The right input parameter will be the REJECTED
exception (not null
), if this
rejected,
otherwise it can be assumed that the state was FULFILLED
, and the left input parameter will be set, if
the fulfillment value was non-null.
The callback will be run as soon as possible (but not inline) if this
is already resolved.
If the callback returns a null
value, then the returned promise will resolve as FULFILLED
with
value null
.
If an exception is thrown within the callback, then the returned promise will be REJECTED
, with that
exception.
U
- The return type of the new promise, dictated by the callback promise's return type.callback
- The operation to perform when the promise resolves.public static <T> Promise<T> wrap(java.util.concurrent.CompletionStage<T> stage, java.util.concurrent.Executor executor, Promise<? extends T> promise)
CompletionStage
as a base, create a new PromiseStage
, that will resolve
with the same state and value as the provided Promise
.
A null stage or promise will result in a NullPointerException
.
If the stage provided is not completed, and will never complete, then this thread will hang.
T
- The type of the returned promise.stage
- A SUCCESSFULLY COMPLETED completion stage.executor
- The executor to create the new PromiseStage
with. Can be null.promise
- The promise to wrap.PromiseStage
.