FilteringIterator implements Iterator<T> { @Override public T next() { if (state == State.FRESH || state == State.NEXT_RETRIEVED) { advance(); } if (state == State.DONE) { throw new NoSuchElementException(); } assert state == State.NEXT_PENDING : state; state = State.NEXT_RETRIEVED; return next; } FilteringIterator(Iterator<T> delegate, boolean isMutable); @Override boolean hasNext(); @Override T next(); @Override void remove();  }
@Test(expected=NoSuchElementException.class) public void nextOnEmpty() { Iterator<Integer> iterator = onlyEvens(); iterator.next(); }
@Test(expected=NoSuchElementException.class) public void nextOnEmptyAfterFilter() { Iterator<Integer> iterator = onlyEvens(1); iterator.next(); }