Class Promises.IterateOverBuilder<V,E extends Exception>

java.lang.Object
org.forgerock.util.promise.Promises.IterateOverBuilder<V,E>
Type Parameters:
V - The expected result value
E - Exception that may occur - consider NeverThrowsException
Enclosing class:
Promises

public static final class Promises.IterateOverBuilder<V,E extends Exception> extends Object
IterateOverBuilder provides a mechanism for iterating over a stream or iterable of promise suppliers in search of the first to match the supplied predicate. If no predicate is supplied then find matches on the first non-null result by default.

The supplied promises may not yet be resolved. If a promise yields a result that does not satisfy the predicate then the next is tried in sequence. Once a satisfying result is found, remaining promises in the stream are ignored (will not be resolved by this process, as they will not be requested from their supplier).

The resulting Promise refers to an Optional containing the result of the first of the supplied promises to complete with a result satisfying the predicate. The promise's Optional will be empty if none completed with a satisfactory result.

By default, the promise will be completed with the first failure to occur (in the sequence). The iteration can be configured to continue on failure, in which case subsequent promises will be tried to obtain a value. Should a value still not be found then the resulting promise will complete with the original failure (first failure).

See Also:
  • Method Details

    • findFirst

      public Promises.IterateOverBuilder<V,E> findFirst(Predicate<V> predicate)
      Configure the Predicate to match against when iterating. A match terminates the iteration.

      If no predicate is supplied then find matches on the first non-null result by default.

      Parameters:
      predicate - The predicate
      Returns:
      This instance
    • continueOnFailure

      public Promises.IterateOverBuilder<V,E> continueOnFailure()
      Configure whether the iteration should fail immediately on any failure, or should proceed to try the next element in sequence. This provides the opportunity to source a value later in the sequence. If no value is sourced following a failure then the promise will complete with the original failure.
      Returns:
      This instance
    • get

      public Promise<Optional<V>,E> get()
      Get the result of the iteration, returning a Promise with an optional result matching the configured predicate. If no promise yielded a match then the result is an empty Optional.
      Returns:
      A promise of an optional result that matches the predicate, or empty
      See Also:
    • orElse

      public Promise<V,E> orElse(V defaultValue)
      Get the result of the iteration, returning a Promise with the actual result matching the configured predicate. If no promise yielded a match then the defaultValue is used instead.
      Parameters:
      defaultValue - The value to use should no promise yield a result
      Returns:
      A promise of a result that matches the predicate, or the defaultValue
      See Also:
    • orElseGet

      public Promise<V,E> orElseGet(Supplier<V> defaultValue)
      Get the result of the iteration, returning a Promise with the actual result matching the configured predicate. If no promise yielded a match then the supplied defaultValue is used instead.
      Parameters:
      defaultValue - Supplier of the value to use should no promise yield a result
      Returns:
      A promise of a result that matches the predicate, or the supplied defaultValue
      See Also:
    • orElseGetAsync

      public Promise<V,E> orElseGetAsync(Supplier<Promise<V,E>> defaultValue)
      Get the result of the iteration, returning a Promise with the actual result matching the configured predicate. If no promise yielded a match then the supplied defaultValue promise is used instead.
      Parameters:
      defaultValue - Supplier of a promise of the value to use should no promise yield a result
      Returns:
      A promise of a result that matches the predicate, or the supplied defaultValue promise
      See Also: