Package org.forgerock.util.promise
Class Promises
- java.lang.Object
-
- org.forgerock.util.promise.Promises
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Promises.Results
Ordered list of joined asynchronous results.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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.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.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.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.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.static <V,E extends Exception>
Promise<V,E>newExceptionPromise(E exception)
Returns aPromise
representing an asynchronous task which has already failed with the provided exception.static <V,E extends Exception>
Promise<V,E>newPromise(Supplier<V,E> supplier)
Execute a task and return the result as aPromise
.static <V,E extends Exception>
Promise<V,E>newResultPromise(V result)
Returns aPromise
representing an asynchronous task which has already succeeded with the provided result.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.static <E extends Exception>
Promise<Void,E>newVoidResultPromise()
Returns aPromise
representing an asynchronous task which has already succeeded without result.static <V,E extends Exception>
Promise<List<V>,E>when(List<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.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.
-
-
-
Method Detail
-
newPromise
public static <V,E extends Exception> Promise<V,E> newPromise(Supplier<V,E> supplier)
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
public static <V,E extends Exception> Promise<V,E> newExceptionPromise(E exception)
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
public static <V,E extends Exception> Promise<V,E> newResultPromise(V result)
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
public static <E extends Exception> Promise<Void,E> 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
public static <V,E extends Exception> Promise<List<V>,E> when(List<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 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
public static 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).- 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.
-
-