java.lang.Object
com.renomad.minum.utils.SearchUtils
Utilities for searching collections of data
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.static <T> T
findExactlyOne
(Stream<T> streamOfSomething, Predicate<? super T> searchPredicate, Callable<T> alternate) This is similar tofindExactlyOne(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.
-
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
- aPredicate
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 tofindExactlyOne(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
- aPredicate
run to search for an element in the stream.alternate
- aCallable
that will be run when no elements were found.- Throws:
InvariantException
- if there are two or more results found
-