Package org.forgerock.util.promise
Class Promises
java.lang.Object
org.forgerock.util.promise.Promises
Utility methods for creating and composing
Promise
s.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Ordered list of joined asynchronous results. -
Method Summary
Modifier and TypeMethodDescriptionstatic Promise<Void,
NeverThrowsException> allDone
(Collection<Promise<?, ?>> promises) Returns aPromise
which will be completed successfully once all of the provided promises have completed (successfully or not, including runtime exceptions).static <V> Promise<V,
NeverThrowsException> allDone
(Collection<Promise<?, ?>> promises, V context) Returns aPromise
which will be completed successfully once all of the provided promises have completed (successfully or not, including runtime exceptions), returning back thecontext
parameter.anyResultFrom
(Stream<Promise<V, E>> promises) Takes a stream of promises that may not yet be resolved and returns a resolved value from them, or if none resolve to a result, the failure from the last.executeAsync
(Executor executor, Supplier<T, E> supplier) Execute the provided supplier through the executor and returns immediately aPromise
that will be completed once the supplier has been executed.static Promise<Promises.Results,
Exception> Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.static Promise<Promises.Results,
Exception> Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.newExceptionPromise
(E exception) Returns aPromise
representing an asynchronous task which has already failed with the provided exception.newPromise
(Supplier<V, E> supplier) Execute a task and return the result as aPromise
.newResultPromise
(V result) Returns aPromise
representing an asynchronous task which has already succeeded with the provided result.newRuntimeExceptionPromise
(RuntimeException exception) Returns aPromise
representing an asynchronous task which has already failed with the provided runtime exception.Returns aPromise
representing an asynchronous task which has already succeeded without result.Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.
-
Method Details
-
newPromise
Execute a task and return the result as aPromise
.If the task executed successfully, then the
Promise
is completed with the returned result of the task's execution.If the task failed during its execution then the
Promise
is completed with the caughtException
(orRuntimeException
).- Type Parameters:
V
- The type of the task's result, orVoid
if the task does not return anything (i.e. it only has side-effects).E
- The type of the exception thrown by the task if it fails, orNeverThrowsException
if the task cannot fail.- Parameters:
supplier
- The supplier that computes the result or throw an exception in case of failure.- Returns:
- A
Promise
representing a completed asynchronous task.
-
executeAsync
public static <T,E extends Exception> Promise<T,E> executeAsync(Executor executor, Supplier<T, E> supplier) Execute the provided supplier through the executor and returns immediately aPromise
that will be completed once the supplier has been executed.- Type Parameters:
T
- The value typeE
- The exception type- Parameters:
executor
- The executor to use to execute the suppliersupplier
- The block to execute asynchronously through the executor- Returns:
- A Promise that will be completed once the supplier has been executed.
-
newRuntimeExceptionPromise
public static <V,E extends Exception> Promise<V,E> newRuntimeExceptionPromise(RuntimeException exception) Returns aPromise
representing an asynchronous task which has already failed with the provided runtime exception. Attempts to get the result will immediately fail, and any listeners registered against the returned promise will be immediately invoked in the same thread as the caller.- Type Parameters:
V
- The type of the task's result, orVoid
if the task does not return anything (i.e. it only has side-effects).E
- The type of the exception thrown by the task if it fails, orNeverThrowsException
if the task cannot fail.- Parameters:
exception
- The exception indicating why the asynchronous task has failed.- Returns:
- A
Promise
representing an asynchronous task which has already failed with the provided exception.
-
newExceptionPromise
Returns aPromise
representing an asynchronous task which has already failed with the provided exception. Attempts to get the result will immediately fail, and any listeners registered against the returned promise will be immediately invoked in the same thread as the caller.- Type Parameters:
V
- The type of the task's result, orVoid
if the task does not return anything (i.e. it only has side-effects).E
- The type of the exception thrown by the task if it fails, orNeverThrowsException
if the task cannot fail.- Parameters:
exception
- The exception indicating why the asynchronous task has failed.- Returns:
- A
Promise
representing an asynchronous task which has already failed with the provided exception.
-
newResultPromise
Returns aPromise
representing an asynchronous task which has already succeeded with the provided result. Attempts to get the result will immediately return the result, and any listeners registered against the returned promise will be immediately invoked in the same thread as the caller.- Type Parameters:
V
- The type of the task's result, orVoid
if the task does not return anything (i.e. it only has side-effects).E
- The type of the exception thrown by the task if it fails, orNeverThrowsException
if the task cannot fail.- Parameters:
result
- The result of the asynchronous task.- Returns:
- A
Promise
representing an asynchronous task which has already succeeded with the provided result.
-
newVoidResultPromise
Returns aPromise
representing an asynchronous task which has already succeeded without result. Any listeners registered against the returned promise will be immediately invoked in the same thread as the caller.- Type Parameters:
E
- The type of the exception thrown by the task if it fails, orNeverThrowsException
if the task cannot fail.- Returns:
- A
Promise
representing an asynchronous task which has already succeeded with the provided result.
-
when
Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.- Type Parameters:
V
- The type of the tasks' result, orVoid
if the tasks do not return anything (i.e. they only has side-effects).E
- The type of the exception thrown by the tasks if they fail, orNeverThrowsException
if the tasks cannot fail.- Parameters:
promises
- The list of tasks to be combined.- Returns:
- A
Promise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.
-
when
@SafeVarargs public static <V,E extends Exception> Promise<List<V>,E> when(Promise<V, E>... promises) Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.- Type Parameters:
V
- The type of the tasks' result, orVoid
if the tasks do not return anything (i.e. they only has side-effects).E
- The type of the exception thrown by the tasks if they fail, orNeverThrowsException
if the tasks cannot fail.- Parameters:
promises
- The list of tasks to be combined.- Returns:
- A
Promise
which will be completed once all of the provided promises have succeeded, or as soon as one of them has thrown an exception.
-
anyResultFrom
public static <V,E extends Exception> Optional<Promise<V,E>> anyResultFrom(Stream<Promise<V, E>> promises) Takes a stream of promises that may not yet be resolved and returns a resolved value from them, or if none resolve to a result, the failure from the last.- Type Parameters:
V
- The type of promise value.E
- The type of promise exception.- Parameters:
promises
- The promises.- Returns:
- A promise that will either have a completed value from one of the provided promsies, or the failure from the last promise to complete, or an empty optional if there were no promises.
-
allDone
Returns aPromise
which will be completed successfully once all of the provided promises have completed (successfully or not, including runtime exceptions).- Parameters:
promises
- The collection of tasks to be combined.- Returns:
- A
Promise
which will be completed successfully once all of the provided promises have completed (successfully or not).
-
allDone
public static <V> Promise<V,NeverThrowsException> allDone(Collection<Promise<?, ?>> promises, V context) Returns aPromise
which will be completed successfully once all of the provided promises have completed (successfully or not, including runtime exceptions), returning back thecontext
parameter.- Type Parameters:
V
- The type of the promise' result (can beVoid
).- Parameters:
promises
- The collection of tasks to be combined.context
- Instance passed back to the resulting promise on completion (can benull
)- Returns:
- A
Promise
which will be completed successfully once all of the provided promises have completed (successfully or not).
-
join
@SafeVarargs public static Promise<Promises.Results,Exception> join(Promise<?, ? extends Exception>... promises) Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.On successful completion, an immutable
Promises.Results
instance containing all respective results of the joined promises (in the same order) is provided.- Parameters:
promises
- The list of tasks to be combined.- Returns:
- A
Promise
which will be completed once all of the provided promises have succeeded, or as soon as one of them has thrown an exception.
-
join
public static Promise<Promises.Results,Exception> join(List<Promise<?, ? extends Exception>> promises) Returns aPromise
which will be completed once all of the provided promises have succeeded, or as soon as one of them fails.On successful completion, an immutable
Promises.Results
instance containing all respective results of the joined promises (in the same order) is provided.- Parameters:
promises
- The list of tasks to be combined.- Returns:
- A
Promise
which will be completed once all of the provided promises have succeeded, or as soon as one of them has thrown an exception.
-