FilteringIterator extends UnmodifiableIterator<O> { @Override public O next() { if(!hasNext()) { throw new NoSuchElementException(); } O objectToReturn = nextObject; nextObject = null; nextObjectIsNull = false; return objectToReturn; } FilteringIterator(Iterator<O> wrappedIterator, QueryOptions queryOptions); @Override boolean hasNext(); @Override O next(); abstract boolean isValid(O object, QueryOptions queryOptions);  }
@Test public void testNextPopulatedWithoutCallingHasNext(){ List<String> testList = Arrays.asList("abc", "bcd", "cde"); FilteringIterator<String> iterator = new FilteringIterator<String>(testList.iterator(), noQueryOptions()) { @Override public boolean isValid(String object, QueryOptions queryOptions) { return true; } }; assertThat(iterator.next(), is("abc")); }
@Test(expected = NoSuchElementException.class) public void testEmptyDelegate() { List<String> testList = Arrays.asList(); FilteringIterator<String> iterator = new FilteringIterator<String>(testList.iterator(), noQueryOptions()) { @Override public boolean isValid(String object, QueryOptions queryOptions) { return true; } }; iterator.next(); }