Interface Optionals


public interface Optionals
Utility methods to work with Optionals.
Author:
Oliver Gierke, Christoph Strobl
  • Method Details

    • isAnyPresent

      static boolean isAnyPresent(Optional<?>... optionals)
      Returns whether any of the given Optionals is present.
      Parameters:
      optionals - must not be null.
      Returns:
    • toStream

      @SafeVarargs static <T> Stream<T> toStream(Optional<? extends T>... optionals)
      Turns the given Optional into a one-element Stream or an empty one if not present.
      Parameters:
      optionals - must not be null.
      Returns:
    • firstNonEmpty

      static <S, T> Optional<T> firstNonEmpty(Iterable<S> source, Function<S,Optional<T>> function)
      Applies the given function to the elements of the source and returns the first non-empty result.
      Parameters:
      source - must not be null.
      function - must not be null.
      Returns:
    • firstNonEmpty

      static <S, T> T firstNonEmpty(Iterable<S> source, Function<S,T> function, T defaultValue)
      Applies the given function to the elements of the source and returns the first non-empty result.
      Parameters:
      source - must not be null.
      function - must not be null.
      Returns:
    • firstNonEmpty

      @SafeVarargs static <T> Optional<T> firstNonEmpty(Supplier<Optional<T>>... suppliers)
      Invokes the given Suppliers for Optional results one by one and returns the first non-empty one.
      Parameters:
      suppliers - must not be null.
      Returns:
    • firstNonEmpty

      static <T> Optional<T> firstNonEmpty(Iterable<Supplier<Optional<T>>> suppliers)
      Invokes the given Suppliers for Optional results one by one and returns the first non-empty one.
      Parameters:
      suppliers - must not be null.
      Returns:
    • next

      static <T> Optional<T> next(Iterator<T> iterator)
      Returns the next element of the given Iterator or Optional.empty() in case there is no next element.
      Parameters:
      iterator - must not be null.
      Returns:
    • withBoth

      static <T, S> Optional<Pair<T,S>> withBoth(Optional<T> left, Optional<S> right)
      Returns a Pair if both Optional instances have values or Optional.empty() if one or both are missing.
      Parameters:
      left -
      right -
      Returns:
    • ifAllPresent

      static <T, S> void ifAllPresent(Optional<T> left, Optional<S> right, BiConsumer<T,S> consumer)
      Invokes the given BiConsumer if all given Optional are present.
      Parameters:
      left - must not be null.
      right - must not be null.
      consumer - must not be null.
    • mapIfAllPresent

      static <T, S, R> Optional<R> mapIfAllPresent(Optional<T> left, Optional<S> right, BiFunction<T,S,R> function)
      Maps the values contained in the given Optional if both of them are present.
      Parameters:
      left - must not be null.
      right - must not be null.
      function - must not be null.
      Returns:
    • ifPresentOrElse

      static <T> void ifPresentOrElse(Optional<T> optional, Consumer<? super T> consumer, Runnable runnable)
      Invokes the given Consumer if the Optional is present or the Runnable if not.
      Parameters:
      optional - must not be null.
      consumer - must not be null.
      runnable - must not be null.