Package org.forgerock.util.promise
Class Promises
java.lang.Object
org.forgerock.util.promise.Promises
Utility methods for creating and composing 
Promises.- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic final classPromises.IterateOverBuilder<V,E extends Exception> static classOrdered list of joined asynchronous results.
- 
Method SummaryModifier and TypeMethodDescriptionstatic Promise<Void,NeverThrowsException> allDone(Collection<Promise<?, ?>> promises) Returns aPromisewhich will be completed successfully once all the provided promises have completed (successfully or not, including runtime exceptions).static <V> Promise<V,NeverThrowsException> allDone(Collection<Promise<?, ?>> promises, V context) Returns aPromisewhich will be completed successfully once all the provided promises have completed (successfully or not, including runtime exceptions), returning back thecontextparameter.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 aPromisethat will be completed once the supplier has been executed.static <V,E extends Exception> 
 Promises.IterateOverBuilder<V,E> iterateOver(Iterable<AsyncSupplier<V, E>> promiseIterable) Configure anPromises.IterateOverBuilderto iterate over anIterableof promisesuppliersin search of the first to match a givenpredicate.static <V,E extends Exception> 
 Promises.IterateOverBuilder<V,E> iterateOver(Stream<AsyncSupplier<V, E>> promiseStream) Configure anPromises.IterateOverBuilderto iterate over aStreamof promisesuppliersin search of the first to match a givenpredicate.static Promise<Promises.Results,Exception> Returns aPromisewhich will be completed once all the provided promises have succeeded, or as soon as one of them fails.static Promise<Promises.Results,Exception> Returns aPromisewhich will be completed once all the provided promises have succeeded, or as soon as one of them fails.newExceptionPromise(E exception) Returns aPromiserepresenting 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 aPromiserepresenting an asynchronous task which has already succeeded with the provided result.newRuntimeExceptionPromise(RuntimeException exception) Returns aPromiserepresenting an asynchronous task which has already failed with the provided runtime exception.Returns aPromiserepresenting an asynchronous task which has already succeeded without result.Returns aPromisewhich will be completed once all of the provided promises have succeeded, or as soon as one of them fails.Returns aPromisewhich will be completed once all of the provided promises have succeeded, or as soon as one of them fails.
- 
Method Details- 
newPromiseExecute a task and return the result as aPromise.If the task executed successfully, then the Promiseis completed with the returned result of the task's execution.If the task failed during its execution then the Promiseis completed with the caughtException(orRuntimeException).- Type Parameters:
- V- The type of the task's result, or- Voidif 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, or- NeverThrowsExceptionif the task cannot fail.
- Parameters:
- supplier- The supplier that computes the result or throw an exception in case of failure.
- Returns:
- A Promiserepresenting a completed asynchronous task.
 
- 
executeAsyncpublic 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 aPromisethat will be completed once the supplier has been executed.- Type Parameters:
- T- The value type
- E- The exception type
- Parameters:
- executor- The executor to use to execute the supplier
- supplier- The block to execute asynchronously through the executor
- Returns:
- A Promise that will be completed once the supplier has been executed.
 
- 
newRuntimeExceptionPromisepublic static <V,E extends Exception> Promise<V,E> newRuntimeExceptionPromise(RuntimeException exception) Returns aPromiserepresenting 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, or- Voidif 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, or- NeverThrowsExceptionif the task cannot fail.
- Parameters:
- exception- The exception indicating why the asynchronous task has failed.
- Returns:
- A Promiserepresenting an asynchronous task which has already failed with the provided exception.
 
- 
newExceptionPromiseReturns aPromiserepresenting 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, or- Voidif 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, or- NeverThrowsExceptionif the task cannot fail.
- Parameters:
- exception- The exception indicating why the asynchronous task has failed.
- Returns:
- A Promiserepresenting an asynchronous task which has already failed with the provided exception.
 
- 
newResultPromiseReturns aPromiserepresenting 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, or- Voidif 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, or- NeverThrowsExceptionif the task cannot fail.
- Parameters:
- result- The result of the asynchronous task.
- Returns:
- A Promiserepresenting an asynchronous task which has already succeeded with the provided result.
 
- 
newVoidResultPromiseReturns aPromiserepresenting 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, or- NeverThrowsExceptionif the task cannot fail.
- Returns:
- A Promiserepresenting an asynchronous task which has already succeeded with the provided result.
 
- 
whenReturns aPromisewhich 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, or- Voidif 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, or- NeverThrowsExceptionif the tasks cannot fail.
- Parameters:
- promises- The list of tasks to be combined.
- Returns:
- A Promisewhich 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 aPromisewhich 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, or- Voidif 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, or- NeverThrowsExceptionif the tasks cannot fail.
- Parameters:
- promises- The list of tasks to be combined.
- Returns:
- A Promisewhich will be completed once all of the provided promises have succeeded, or as soon as one of them has thrown an exception.
 
- 
anyResultFrompublic 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.
 
- 
iterateOverpublic static <V,E extends Exception> Promises.IterateOverBuilder<V,E> iterateOver(Stream<AsyncSupplier<V, E>> promiseStream) Configure anPromises.IterateOverBuilderto iterate over aStreamof promisesuppliersin search of the first to match a givenpredicate.- Type Parameters:
- V- The type of promise's- Optionalvalue
- E- The type of promise exception
- Parameters:
- promiseStream-- Streamof promise- suppliers
- Returns:
- An Promises.IterateOverBuilderto be configured for use
- See Also:
 
- 
iterateOverpublic static <V,E extends Exception> Promises.IterateOverBuilder<V,E> iterateOver(Iterable<AsyncSupplier<V, E>> promiseIterable) Configure anPromises.IterateOverBuilderto iterate over anIterableof promisesuppliersin search of the first to match a givenpredicate.- Type Parameters:
- V- The type of promise's- Optionalvalue
- E- The type of promise exception
- Parameters:
- promiseIterable-- Iterableof promise- suppliers
- Returns:
- An Promises.IterateOverBuilderto be configured for use
- See Also:
 
- 
allDoneReturns aPromisewhich will be completed successfully once all the provided promises have completed (successfully or not, including runtime exceptions).- Parameters:
- promises- The collection of tasks to be combined.
- Returns:
- A Promisewhich will be completed successfully once all the provided promises have completed (successfully or not).
 
- 
allDonepublic static <V> Promise<V,NeverThrowsException> allDone(Collection<Promise<?, ?>> promises, V context) Returns aPromisewhich will be completed successfully once all the provided promises have completed (successfully or not, including runtime exceptions), returning back thecontextparameter.- Type Parameters:
- V- The type of the promise' result (can be- Void).
- Parameters:
- promises- The collection of tasks to be combined.
- context- Instance passed back to the resulting promise on completion (can be- null)
- Returns:
- A Promisewhich 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 aPromisewhich will be completed once all the provided promises have succeeded, or as soon as one of them fails.On successful completion, an immutable Promises.Resultsinstance 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 Promisewhich will be completed once all of the provided promises have succeeded, or as soon as one of them has thrown an exception.
 
- 
joinpublic static Promise<Promises.Results,Exception> join(List<Promise<?, ? extends Exception>> promises) Returns aPromisewhich will be completed once all the provided promises have succeeded, or as soon as one of them fails.On successful completion, an immutable Promises.Resultsinstance 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 Promisewhich will be completed once all the provided promises have succeeded, or as soon as one of them has thrown an exception.
 
 
-