| 1 | package com.renomad.minum.utils; | |
| 2 | ||
| 3 | import java.util.List; | |
| 4 | import java.util.Objects; | |
| 5 | import java.util.concurrent.Callable; | |
| 6 | import java.util.function.Predicate; | |
| 7 | import java.util.stream.Stream; | |
| 8 | ||
| 9 | /** | |
| 10 | * Utilities for searching collections of data | |
| 11 | */ | |
| 12 | public final class SearchUtils { | |
| 13 | ||
| 14 | private SearchUtils() { | |
| 15 | // not meant to be instantiated | |
| 16 | } | |
| 17 | ||
| 18 | /** | |
| 19 | * This helper method will give you the one item in this list, or | |
| 20 | * null if there are none. If there's more than 1, it will throw | |
| 21 | * an exception. This is for those times when we absolutely expect | |
| 22 | * there to be just one of a thing in a database, like if we're searching | |
| 23 | * for Persons by id. | |
| 24 | * @param searchPredicate a {@link Predicate} run to search for an element in the stream. | |
| 25 | * @throws InvariantException if there are two or more results found | |
| 26 | */ | |
| 27 | public static <T> T findExactlyOne(Stream<T> streamOfSomething, Predicate<? super T> searchPredicate) { | |
| 28 |
1
1. findExactlyOne : replaced return value with null for com/renomad/minum/utils/SearchUtils::findExactlyOne → KILLED |
return findExactlyOne(streamOfSomething, searchPredicate, () -> null); |
| 29 | } | |
| 30 | ||
| 31 | /** | |
| 32 | * This is similar to {@link #findExactlyOne(Stream, Predicate)} except that you | |
| 33 | * can provide what gets returned if there are none found - so instead of | |
| 34 | * returning null, it can return something else. | |
| 35 | * <br> | |
| 36 | * The values will be pre-filtered to skip any null values. | |
| 37 | * <br> | |
| 38 | * @param alternate a {@link Callable} that will be run when no elements were found. | |
| 39 | * @param searchPredicate a {@link Predicate} run to search for an element in the stream. | |
| 40 | * @throws InvariantException if there are two or more results found | |
| 41 | */ | |
| 42 | public static <T> T findExactlyOne(Stream<T> streamOfSomething, Predicate<? super T> searchPredicate, Callable<T> alternate) { | |
| 43 | List<T> listOfThings = streamOfSomething.filter(Objects::nonNull).filter(searchPredicate).toList(); | |
| 44 |
2
1. findExactlyOne : negated conditional → KILLED 2. findExactlyOne : negated conditional → KILLED |
if (! (listOfThings.isEmpty() || listOfThings.size() == 1)) { |
| 45 | throw new UtilsException("Must be zero or one of this thing, or it's a bug. We found a size of " + listOfThings.size()); | |
| 46 | } | |
| 47 |
1
1. findExactlyOne : negated conditional → KILLED |
if (listOfThings.isEmpty()) { |
| 48 | T returnValue; | |
| 49 | try { | |
| 50 | returnValue = alternate.call(); | |
| 51 | } catch (Exception ex) { | |
| 52 | throw new UtilsException(ex); | |
| 53 | } | |
| 54 |
1
1. findExactlyOne : replaced return value with null for com/renomad/minum/utils/SearchUtils::findExactlyOne → KILLED |
return returnValue; |
| 55 | } else { | |
| 56 |
1
1. findExactlyOne : replaced return value with null for com/renomad/minum/utils/SearchUtils::findExactlyOne → KILLED |
return listOfThings.getFirst(); |
| 57 | } | |
| 58 | } | |
| 59 | } | |
Mutations | ||
| 28 |
1.1 |
|
| 44 |
1.1 2.2 |
|
| 47 |
1.1 |
|
| 54 |
1.1 |
|
| 56 |
1.1 |