Class StreamHelper

java.lang.Object
one.x1f.sip.foundation.core.util.StreamHelper

public class StreamHelper extends Object
  • Constructor Details

    • StreamHelper

      public StreamHelper()
  • Method Details

    • typeFilter

      public static <S, T extends S> Function<S,Stream<T>> typeFilter(Class<T> type)
      Filters a stream of objects by type.

      Usage example: List<Object>.stream().flatMap(typeFilter(String.class)).toList()

      Type Parameters:
      S - The type of the stream
      T - Filtering type
      Parameters:
      type - The filtering type
      Returns:
      Filtering function
    • findAtMostOne

      public static <T> Optional<T> findAtMostOne(Stream<T> stream, Predicate<T> predicate, Supplier<RuntimeException> multipleException)
      Returns at most one element from the given stream matching the given predicate, but throws the exception retrieved from multipleException if more match.
      Type Parameters:
      T - Element type
      Parameters:
      stream - Stream to retrieve unique element from
      predicate - Predicate for filtering
      multipleException - Error thrown if more than one element matches the predicate
      Returns:
      Optional element found