IteratorUtils { public static final <S> Iterator<S> filter(final Iterator<S> iterator, final Predicate<S> predicate) { return new Iterator<S>() { S nextResult = null; @Override public boolean hasNext() { if (null != this.nextResult) { return true; } else { advance(); return null != this.nextResult; } } @Override public void remove() { iterator.remove(); } @Override public S next() { try { if (null != this.nextResult) { return this.nextResult; } else { advance(); if (null != this.nextResult) return this.nextResult; else throw FastNoSuchElementException.instance(); } } finally { this.nextResult = null; } } private final void advance() { this.nextResult = null; while (iterator.hasNext()) { final S s = iterator.next(); if (predicate.test(s)) { this.nextResult = s; return; } } } }; } private  IteratorUtils(); static final Iterator<S> of(final S a); static final Iterator<S> of(final S a, S b); static final S fill(final Iterator<T> iterator, final S collection); static void iterate(final Iterator iterator); static final long count(final Iterator iterator); static final long count(final Iterable iterable); static List<S> list(final Iterator<S> iterator); static List<S> list(final Iterator<S> iterator, final Comparator comparator); static Set<S> set(final Iterator<S> iterator); static Iterator<S> limit(final Iterator<S> iterator, final int limit); static boolean allMatch(final Iterator<T> iterator, final Predicate<T> predicate); static boolean anyMatch(final Iterator<T> iterator, final Predicate<T> predicate); static boolean noneMatch(final Iterator<T> iterator, final Predicate<T> predicate); static Map<K, S> collectMap(final Iterator<S> iterator, final Function<S, K> key); static Map<K, V> collectMap(final Iterator<S> iterator, final Function<S, K> key, final Function<S, V> value); static Map<K, List<S>> groupBy(final Iterator<S> iterator, final Function<S, K> groupBy); static S reduce(final Iterator<S> iterator, final S identity, final BinaryOperator<S> accumulator); static S reduce(final Iterable<S> iterable, final S identity, final BinaryOperator<S> accumulator); static E reduce(final Iterator<S> iterator, final E identity, final BiFunction<E, S, E> accumulator); static E reduce(final Iterable<S> iterable, final E identity, final BiFunction<E, S, E> accumulator); static final Iterator<S> consume(final Iterator<S> iterator, final Consumer<S> consumer); static final Iterable<S> consume(final Iterable<S> iterable, final Consumer<S> consumer); static final Iterator<E> map(final Iterator<S> iterator, final Function<S, E> function); static final Iterable<E> map(final Iterable<S> iterable, final Function<S, E> function); static final Iterator<S> filter(final Iterator<S> iterator, final Predicate<S> predicate); static final Iterable<S> filter(final Iterable<S> iterable, final Predicate<S> predicate); static final Iterator<E> flatMap(final Iterator<S> iterator, final Function<S, Iterator<E>> function); static final Iterator<S> concat(final Iterator<S>... iterators); static Iterator asIterator(final Object o); static List asList(final Object o); static Stream<T> stream(final Iterator<T> iterator); static Stream<T> stream(final Iterable<T> iterable); static Iterator<T> noRemove(final Iterator<T> iterator); static Iterator<T> removeOnNext(final Iterator<T> iterator);  }
@Test(expected = FastNoSuchElementException.class) public void shouldThrowOnFilterIfNextedPastAvailable() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); final Iterator<String> filteredItty = IteratorUtils.filter(iterable.iterator(), s -> s.startsWith("dfaa")); filteredItty.next(); }
@Test public void shouldFilterAllFromIterator() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.filter(iterable.iterator(), s -> s.startsWith("dfaa")), 0); }
@Test public void shouldFilterNoneFromIterator() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.filter(iterable.iterator(), s -> s.startsWith("test")), 3); }
@Test public void shouldFilterNoneFromIteratorWithoutHasNextCheckFirst() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); final Iterator<String> filteredItty = IteratorUtils.filter(iterable.iterator(), s -> s.startsWith("test")); assertEquals("test1", filteredItty.next()); assertEquals("test2", filteredItty.next()); assertEquals("test3", filteredItty.next()); }
@Test public void shouldFilterNoneFromIteratorWithMultipleChecksOfHasNext() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); final Iterator<String> filteredItty = IteratorUtils.filter(iterable.iterator(), s -> s.startsWith("test")); assertThat(filteredItty.hasNext(), is(true)); assertThat(filteredItty.hasNext(), is(true)); assertThat(filteredItty.hasNext(), is(true)); assertIterator(filteredItty, 3); }
@Test public void shouldFilterSomeFromIterator() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.filter(iterable.iterator(), s -> s.equals("test1")), 1); }
@Test public void shouldFilterAllFromIterable() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.filter(iterable, s -> s.startsWith("dfaa")).iterator(), 0); }
@Test public void shouldFilterNoneFromIterable() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.filter(iterable, s -> s.startsWith("test")).iterator(), 3); }
@Test public void shouldFilterSomeFromIterable() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.filter(iterable, s -> s.equals("test1")).iterator(), 1); }