public class PromiseRunnable<T> extends PromiseBase<T>
The focus was maximum flexibility, with the ability to extend or implement promises for various use cases.
The most complex part of this implementation is the action property which is explained below:
- action is used to simplify and provide safety for all paths
- action will only be executed AT MOST once, either manually, or part of internal logic
- neither action nor runner is required IF you are not calling run()
, and will manage the state externally
- executing actions are handled by the PromiseRunner, implement that however you wish
lock
Constructor and Description |
---|
PromiseRunnable() |
PromiseRunnable(PromiseRunner runner) |
PromiseRunnable(PromiseRunner runner,
java.util.function.Consumer<PromiseRunnable<T>> action) |
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. |
PromiseRunnable<T> |
fulfill(T value) |
java.util.function.Consumer<PromiseRunnable<T>> |
getAction() |
PromiseRunner |
getRunner() |
boolean |
isRun() |
PromiseRunnable<T> |
reject(java.lang.Throwable exception) |
PromiseRunnable<T> |
resolve(Promise<? extends T> promise)
Resolve this promise with the same value/exception and state as another promise.
|
PromiseRunnable<T> |
run() |
PromiseRunnable<T> |
setAction(java.util.function.Consumer<PromiseRunnable<T>> action) |
PromiseRunnable<T> |
setRun() |
PromiseRunnable<T> |
setRunner(PromiseRunner runner) |
<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. |
exceptSync, getException, getState, getValue, sync, thenSync
public PromiseRunnable()
public PromiseRunnable(PromiseRunner runner)
public PromiseRunnable(PromiseRunner runner, java.util.function.Consumer<PromiseRunnable<T>> action)
public java.util.function.Consumer<PromiseRunnable<T>> getAction()
public PromiseRunnable<T> setAction(java.util.function.Consumer<PromiseRunnable<T>> action) throws java.lang.IllegalStateException
java.lang.IllegalStateException
public PromiseRunner getRunner()
public PromiseRunnable<T> setRunner(PromiseRunner runner) throws java.lang.IllegalStateException
java.lang.IllegalStateException
public boolean isRun()
public PromiseRunnable<T> setRun()
public PromiseRunnable<T> run()
public PromiseRunnable<T> reject(java.lang.Throwable exception) throws MutatedStateException, java.lang.NullPointerException
reject
in class PromiseBase<T>
MutatedStateException
java.lang.NullPointerException
public PromiseRunnable<T> fulfill(T value) throws SelfResolutionException, MutatedStateException
fulfill
in class PromiseBase<T>
SelfResolutionException
MutatedStateException
public PromiseRunnable<T> resolve(Promise<? extends T> promise) throws SelfResolutionException, MutatedStateException
PromiseBase
Note that this may happen asynchronously, subsequent calls to resolve may fail silently. Take this into consideration especially if exposing this method externally.
Circular references are not checked, but we do take care not to directly resolve this.
resolve
in class PromiseBase<T>
promise
- The promise to resolve.SelfResolutionException
- If promise is this, or the promise is resolved to this.MutatedStateException
- If this was already resolved, at the time of this call. Not triggered immediately if promise is PENDING.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 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 <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 <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.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.