Class SearchUtils

java.lang.Object
com.renomad.minum.utils.SearchUtils

public final class SearchUtils extends Object
Utilities for searching collections of data
  • Method Details

    • findExactlyOne

      public static <T> T findExactlyOne(Stream<T> streamOfSomething, Predicate<? super T> searchPredicate)
      This helper method will give you the one item in this list, or null if there are none. If there's more than 1, it will throw an exception. This is for those times when we absolutely expect there to be just one of a thing in a database, like if we're searching for Persons by id.
      Parameters:
      searchPredicate - a Predicate run to search for an element in the stream.
      Throws:
      InvariantException - if there are two or more results found
    • findExactlyOne

      public static <T> T findExactlyOne(Stream<T> streamOfSomething, Predicate<? super T> searchPredicate, Callable<T> alternate)
      This is similar to findExactlyOne(Stream, Predicate) except that you can provide what gets returned if there are none found - so instead of returning null, it can return something else.
      The values will be pre-filtered to skip any null values.
      Parameters:
      searchPredicate - a Predicate run to search for an element in the stream.
      alternate - a Callable that will be run when no elements were found.
      Throws:
      InvariantException - if there are two or more results found