Class Promises

java.lang.Object
org.forgerock.util.promise.Promises

public final class Promises extends Object
Utility methods for creating and composing Promises.
  • Method Details

    • newRuntimeExceptionPromise

      public static <V, E extends Exception> Promise<V,E> newRuntimeExceptionPromise(RuntimeException exception)
      Returns a Promise 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, or Void 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, or NeverThrowsException 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 a Promise 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, or Void 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, or NeverThrowsException 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 a Promise 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, or Void 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, or NeverThrowsException 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.
    • when

      public static <V, E extends Exception> Promise<List<V>,E> when(List<Promise<V,E>> promises)
      Returns a Promise 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, or Void 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, or NeverThrowsException 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 a Promise 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, or Void 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, or NeverThrowsException 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.