public class PromiseRunnableFactory extends PromiseApi
PromiseRunnable
instances.
Default promise runner (for the global instance) is ExecutorRunner
.
Constructor and Description |
---|
PromiseRunnableFactory(PromiseRunner runner) |
Modifier and Type | Method and Description |
---|---|
<T> Promise<T> |
create(java.util.function.BiConsumer<java.util.function.Consumer<? super T>,java.util.function.Consumer<java.lang.Throwable>> action)
Creates a promise, and executes it asynchronously.
|
<T> Promise<T> |
fulfill(T value)
Create a new promise that is
FULFILLED with the provided value. |
static PromiseRunnableFactory |
getInstance() |
<T> Promise<T> |
reject(java.lang.Throwable reason)
Create a new
REJECTED promise, with a provided reason for rejection. |
<T> Promise<T> |
wrap(Promise<? extends T> promise)
Return a promise created by this
PromiseFactory , from any given promise, that will resolve in the same
way. |
all, all, any, any, attempt, attempt, each, race, race, resolve, resolveAll, toCompletableFuture
public PromiseRunnableFactory(PromiseRunner runner)
public <T> Promise<T> create(java.util.function.BiConsumer<java.util.function.Consumer<? super T>,java.util.function.Consumer<java.lang.Throwable>> action)
PromiseFactory
The action parameter format is (fulfill, reject) -> // stuff
.
The first call to either fulfill or reject will be the resolved value. Both of these methods must throw a
MutatedStateException
for any subsequent calls. If no call is made within action, then the state of the
promise must be PENDING
immediately after action completes.
The implementation of BlockingPromise
takes advantage of this behaviour.
If fulfill is used to attempt to resolve a promise with itself, it must throw a
SelfResolutionException
.
Any Throwable throwable
that is thrown, within the action, will be the equivalent of calling
reject.accept(throwable)
.
Calling the reject parameter, within the action, with a null value, will cause a NullPointerException
to
be thrown internally, which will cause the returned promise to resolve as REJECTED
, with that exception.
A null action parameter will cause a NullPointerException
to be thrown.
T
- The type the promise will resolve with.action
- The task to perform asynchronously.public <T> Promise<T> reject(java.lang.Throwable reason)
PromiseFactory
REJECTED
promise, with a provided reason for rejection.
This MUST happen synchronously (the returned Promise MUST be REJECTED
, not PENDING
to reject
asynchronously).
A null
reason will result in a NullPointerException
being thrown.
reason
- The value this will reject with.public <T> Promise<T> fulfill(T value)
PromiseFactory
FULFILLED
with the provided value.
This MUST happen synchronously (the returned Promise MUST be FULFILLED
, not PENDING
to fulfill
asynchronously).
T
- The type of the value the promise resolved.value
- The value to fulfill.public <T> Promise<T> wrap(Promise<? extends T> promise)
PromiseFactory
PromiseFactory
, from any given promise, that will resolve in the same
way.
Performs a similar but tangential function to PromiseApi.resolve(Object, Class)
, some use cases will
require one or the other, and some will require both.
This method is provided as a convenience, for cases when you need to ensure certain behaviour, due to differences in promise implementations.
NOTE: The returned promise must be resolved not pending, if the promise to wrap was already resolved.
T
- The type of the promise.promise
- The promise you want to wrap.public static PromiseRunnableFactory getInstance()