public abstract class PromiseApi extends java.lang.Object implements PromiseFactory
Note: This class is designed to work with any promise implementation, make a concrete class extending this.
Constructor and Description |
---|
PromiseApi() |
Modifier and Type | Method and Description |
---|---|
<T> Promise<java.util.List<T>> |
all(java.lang.Iterable<? extends Promise<? extends T>> promiseIterable)
Given an iterable of one or more promises, return a promise, that will resolve with a
List containing the
fulfillment values of each input promise, provided every promise resolves successfully. |
<T> Promise<java.util.List<T>> |
all(Promise<? extends T> promise,
Promise<? extends T>... promises) |
<T> Promise<T> |
any(java.lang.Iterable<? extends Promise<? extends T>> promiseIterable)
Given an iterable of one or more promises, return a promise, that will fulfill with the value of the first
of the input promises that fulfills.
|
<T> Promise<T> |
any(Promise<? extends T> promise,
Promise<? extends T>... promises) |
Promise<?> |
attempt(java.lang.Runnable action)
Perform an action, and return a promise that will resolve with rejected, if the action throws an exception.
|
<T> Promise<T> |
attempt(java.util.function.Supplier<? extends Promise<? extends T>> action)
Perform an action, and return a promise that will resolve with rejected, if the action throws an exception.
|
<T,U> Promise<java.util.List<U>> |
each(java.lang.Iterable<? extends T> inputIterable,
java.util.function.Function<? super T,? extends Promise<? extends U>> action)
Given an iterable, perform an asynchronous action on each element serially, similar to
Iterable.forEach(Consumer) . |
<T> Promise<T> |
race(java.lang.Iterable<? extends Promise<? extends T>> promiseIterable)
Given an iterable of one or more promises, return a promise, that will resolve with the same state and value as
the first one that resolves.
|
<T> Promise<T> |
race(Promise<? extends T> promise,
Promise<? extends T>... promises) |
<T> Promise<T> |
resolve(java.lang.Object value,
java.lang.Class<? extends T> type)
Create a new promise that will fulfill or reject, if given a promise and based on it's state, otherwise simply
fulfilling with the value given, provided it can be cast to the given type.
|
<T> java.util.List<Promise<T>> |
resolveAll(java.lang.Iterable<?> inputIterable,
java.lang.Class<? extends T> type)
Copy an iterable into a new
ArrayList , performing resolve(Object, Class) on each element. |
<T> java.util.concurrent.CompletableFuture<T> |
toCompletableFuture(Promise<? extends T> promise)
Convert a promise to a
CompletableFuture . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
create, fulfill, reject, wrap
@SafeVarargs public final <T> Promise<java.util.List<T>> all(Promise<? extends T> promise, Promise<? extends T>... promises) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
java.lang.NullPointerException
java.lang.IllegalArgumentException
all(Iterable)
@SafeVarargs public final <T> Promise<T> race(Promise<? extends T> promise, Promise<? extends T>... promises) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
java.lang.NullPointerException
java.lang.IllegalArgumentException
race(Iterable)
@SafeVarargs public final <T> Promise<T> any(Promise<? extends T> promise, Promise<? extends T>... promises) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
java.lang.NullPointerException
java.lang.IllegalArgumentException
any(Iterable)
public <T> Promise<java.util.List<T>> all(java.lang.Iterable<? extends Promise<? extends T>> promiseIterable) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
List
containing the
fulfillment values of each input promise, provided every promise resolves successfully. The returned promise
will reject with same reason as the first (if any) input promise that rejects. This will not wait on any
other promises, and is as such useful for situations requiring early-exit behaviour.
If any of the input promises are already rejected, then the first encountered rejected promise (using a iterator), will have it's rejection reason propagated to the returned promise.
Unlike the JS promise spec, an empty iterable WILL throw an IllegalArgumentException
.
Any null argument encountered will result in a NullPointerException
(MAY not be triggered by an input
containing a null value IF and only if there is a rejected promise encountered BEFORE the null value).
T
- The type of the promise to return.promiseIterable
- The promises to resolve. Must not be null, and must contain no null values.java.lang.NullPointerException
- If the promiseIterable parameter, or any of it's values, are null.*java.lang.IllegalArgumentException
- If the iterable was empty.public <T> Promise<T> race(java.lang.Iterable<? extends Promise<? extends T>> promiseIterable) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
If any of the provided promises are already resolved, then the first encountered promise (using an iterator) will be used to determine the state and value of the returned promise.
Unlike the JS promise spec, an empty iterable WILL throw an IllegalArgumentException
.
Any null argument encountered will result in a NullPointerException
(MAY not be triggered by an input
containing a null value IF and only if there is a resolve promise encountered BEFORE the null value).
T
- The type of the promise to return.promiseIterable
- The promises to race. Must not be null, and must contain no null values.java.lang.NullPointerException
- If the promiseIterable parameter, or any of it's values, are null.*java.lang.IllegalArgumentException
- If the iterable was empty.public <T> Promise<T> any(java.lang.Iterable<? extends Promise<? extends T>> promiseIterable) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
AggregateException
, which can be used to retrieve the reasons why it rejected.
If any of the provided promises are already fulfilled, then the first iterated promise will have it's fulfillment value propagated. If they are all rejected, then this will reject immediately.
Any null argument encountered will result in a NullPointerException
(MAY not be triggered by an input
containing a null value IF and only if there is a fulfilled promise encountered BEFORE the null value).
T
- The type of promise to return.promiseIterable
- The promises to watch. Must not be null, and must contain no null values.java.lang.NullPointerException
- If the promiseIterable parameter, or any of it's values, are null.*java.lang.IllegalArgumentException
- If the iterable was empty.public <T,U> Promise<java.util.List<U>> each(java.lang.Iterable<? extends T> inputIterable, java.util.function.Function<? super T,? extends Promise<? extends U>> action) throws java.lang.NullPointerException
Iterable.forEach(Consumer)
. These actions are chained together using the promise returned from each
call to action
, which takes the iterated value. The next element will not be iterated until / unless
the promise returned by the previous action call resolves successfully. A null return value from the action
is allowed, it will be treated as a null fulfillment.
If all actions complete successfully, the returned Promise
will fulfill with a List
, containing
an element for each element iterated, in order, from the inputIterable
. The elements will be set with
the fulfillment values of the promises returned by the corresponding action call.
If any action returns a rejected promise or throws any exceptions, then the returned promise will resolve as
REJECTED
, with the same exception.
It should be noted that the iterable is walked before the async operations are performed, in order to improve thread safety (the values are shallow copied locally).
Any null parameters will result in a NullPointerException
.
T
- The type of the input values.U
- The type of the output values.inputIterable
- The source of the values to iterate.action
- The action to perform on each value.List
of promisesjava.lang.NullPointerException
- If any parameters are null.public <T> Promise<T> attempt(java.util.function.Supplier<? extends Promise<? extends T>> action) throws java.lang.NullPointerException
If the action returns a non null promise, then the returned promise will reflect the same state and value.
T
- The type of the promise returned.action
- The action to attempt.java.lang.NullPointerException
- If the action is null.public Promise<?> attempt(java.lang.Runnable action) throws java.lang.NullPointerException
If you need a return type use attempt(Supplier)
.
action
- The action to perform.java.lang.NullPointerException
- If the action is null.If you need a typed promise.
public <T> java.util.concurrent.CompletableFuture<T> toCompletableFuture(Promise<? extends T> promise) throws java.lang.NullPointerException
CompletableFuture
. The reverse of this conversion can be performed by instancing
a new PromiseStage
.T
- The type of the returned CompletableFuture
.promise
- The promise to convert.CompletableFuture
that will resolve with the same state as the input promise.java.lang.NullPointerException
- If the input promise is null.public <T> Promise<T> resolve(java.lang.Object value, java.lang.Class<? extends T> type) throws CircularResolutionException, java.lang.IllegalArgumentException, java.lang.NullPointerException
This method supports the resolution of multiple promises, that are chained together to an unknown depth.
This method uses a provided type parameter to provide type safety. If at all possible it is, however, recommended to simply chain promises in a inherently type safe way.
If the provided value is not a promise, or it and any chained promise(s) are ALL resolved this MUST happen
synchronously, however if any are PENDING
then the returned promise will resolve AFTER value, and any
children.
NOTE: This method is RECURSIVE, like the A+ Promise spec, and MUST throw a CircularResolutionException
in
the event that the provided promise is already resolved, and is part of circularly linked resolved promises.
If Promise.class
or a subclass is used as the type, an IllegalArgumentException
will be thrown.
If type
is null, a NullPointerException
will be thrown.
value
- The value to resolve.type
- The type of the promise that will be resolved, from the input value.CircularResolutionException
- If the provided promise will form a loop.java.lang.IllegalArgumentException
- If the type was Promise.class
.java.lang.NullPointerException
- If the type was null
.public <T> java.util.List<Promise<T>> resolveAll(java.lang.Iterable<?> inputIterable, java.lang.Class<? extends T> type) throws java.lang.NullPointerException
ArrayList
, performing resolve(Object, Class)
on each element.
Any null parameters will result in a NullPointerException
.
T
- The target type of the items.inputIterable
- An iterable of items to resolve.ArrayList
containing the resolved promises (possibly pending).java.lang.NullPointerException
- If either input is null.