@Test public void testSingleOrNull() throws Exception { Integer singleEquals8 = CollectionHelper.singleOrNull(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object == 8; } }); assertThat(singleEquals8, is(8)); Integer singleLargerThan50 = CollectionHelper.singleOrNull(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object >= 50; } }); assertNull(singleLargerThan50); try { CollectionHelper.singleOrNull(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object >= 5; } }); Assert.fail("single should throw exception"); } catch (InvalidOperationException e) { assertThat(e.getMessage(), is("Multiple items match!")); } }
public static <T> T singleOrNull(Collection<T> items, Predicate<T> predicate) { if (isEmpty(items)) { return null; } T result = null; for (T item : items) { if (predicate.apply(item)) { if (result == null) { result = item; } else { throw new InvalidOperationException("Multiple items match!"); } } } return result; }
CollectionHelper { public static <T> T singleOrNull(Collection<T> items, Predicate<T> predicate) { if (isEmpty(items)) { return null; } T result = null; for (T item : items) { if (predicate.apply(item)) { if (result == null) { result = item; } else { throw new InvalidOperationException("Multiple items match!"); } } } return result; } }
CollectionHelper { public static <T> T singleOrNull(Collection<T> items, Predicate<T> predicate) { if (isEmpty(items)) { return null; } T result = null; for (T item : items) { if (predicate.apply(item)) { if (result == null) { result = item; } else { throw new InvalidOperationException("Multiple items match!"); } } } return result; } private  CollectionHelper(); }
CollectionHelper { public static <T> T singleOrNull(Collection<T> items, Predicate<T> predicate) { if (isEmpty(items)) { return null; } T result = null; for (T item : items) { if (predicate.apply(item)) { if (result == null) { result = item; } else { throw new InvalidOperationException("Multiple items match!"); } } } return result; } private  CollectionHelper(); static List<T> filter(Collection<T> items, Predicate<T> predicate); static T first(Collection<T> items, Predicate<T> predicate); static T firstOrNull(Collection<T> items, Predicate<T> predicate); static int firstIndexOf(Collection<T> items, Predicate<T> predicate); static boolean any(Collection<T> items, Predicate<T> predicate); static boolean all(Collection<T> items, Predicate<T> predicate); static boolean isEmpty(Collection items); static T single(Collection<T> items, Predicate<T> predicate); static T singleOrNull(Collection<T> items, Predicate<T> predicate); static int singleIndexOf(Collection<T> items, Predicate<T> predicate); static int count(Collection<T> items, Predicate<T> predicate); static List<TResult> map(Collection<TSource> items, Mapper<TSource, TResult> mapper); }
CollectionHelper { public static <T> T singleOrNull(Collection<T> items, Predicate<T> predicate) { if (isEmpty(items)) { return null; } T result = null; for (T item : items) { if (predicate.apply(item)) { if (result == null) { result = item; } else { throw new InvalidOperationException("Multiple items match!"); } } } return result; } private  CollectionHelper(); static List<T> filter(Collection<T> items, Predicate<T> predicate); static T first(Collection<T> items, Predicate<T> predicate); static T firstOrNull(Collection<T> items, Predicate<T> predicate); static int firstIndexOf(Collection<T> items, Predicate<T> predicate); static boolean any(Collection<T> items, Predicate<T> predicate); static boolean all(Collection<T> items, Predicate<T> predicate); static boolean isEmpty(Collection items); static T single(Collection<T> items, Predicate<T> predicate); static T singleOrNull(Collection<T> items, Predicate<T> predicate); static int singleIndexOf(Collection<T> items, Predicate<T> predicate); static int count(Collection<T> items, Predicate<T> predicate); static List<TResult> map(Collection<TSource> items, Mapper<TSource, TResult> mapper); static final int NOT_FOUND_INDEX; }