### Question:
Algorithm { static <E, Ite extends Iterable<E>> E findFirst(Ite input, Predicate<E> predicate) { for (E element : input) if(predicate.test(element)) return element; return null; }  static ArrayList<E> findAll(Ite input, Predicate<E> predicate);  }### Answer:
@Test public void testFindFirstWithEmptyIterable() { Iterable<Boolean> iterable = new ArrayList<Boolean>(); Boolean result = findFirst(iterable, new Predicate<Boolean>() { @Override public boolean test(Boolean aBoolean) { fail("The predicate should not be called with empty iterable"); return true; } }); assertThat("The result should be null", result, is(nullValue(Boolean.class))); }
@Test public void testFindFirstWithNonEmptyIterableThatFind() { Iterable<Boolean> iterable = new ArrayList<Boolean>(Arrays.asList(Boolean.FALSE, Boolean.TRUE)); final AtomicBoolean predicateCheck = new AtomicBoolean(false); Boolean result = findFirst(iterable, new Predicate<Boolean>() { @Override public boolean test(Boolean aBoolean) { predicateCheck.set(true); return aBoolean; } }); assertThat("The result should be true element", result, is(true)); assertThat("During evaluation, the predicate should have set check to true", predicateCheck.get(), is(true)); }
@Test public void testFindFirstWithNonEmptyIterableThatNotFind() { Iterable<Boolean> iterable = new ArrayList<Boolean>(Arrays.asList(Boolean.FALSE, Boolean.FALSE)); final AtomicBoolean predicateCheck = new AtomicBoolean(false); Boolean result = findFirst(iterable, new Predicate<Boolean>() { @Override public boolean test(Boolean aBoolean) { predicateCheck.set(true); return aBoolean; } }); assertThat("The result should be null", result, is(nullValue(Boolean.class))); assertThat("During evaluation, the predicate should have set check to true", predicateCheck.get(), is(true)); }