IteratorUtils { public static Iterator asIterator(final Object o) { final Iterator itty; if (o instanceof Iterable) itty = ((Iterable) o).iterator(); else if (o instanceof Iterator) itty = (Iterator) o; else if (o instanceof Object[]) itty = new ArrayIterator<>((Object[]) o); else if (o instanceof Stream) itty = ((Stream) o).iterator(); else if (o instanceof Map) itty = ((Map) o).entrySet().iterator(); else if (o instanceof Throwable) itty = of(((Throwable) o).getMessage()); else itty = of(o); return itty; } 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 public void shouldConvertIterableToIterator() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.asIterator(iterable), iterable.size()); }
@Test public void shouldConvertIteratorToIterator() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); assertIterator(IteratorUtils.asIterator(iterable.iterator()), iterable.size()); }
@Test public void shouldConvertArrayToIterator() { final String[] iterable = new String[3]; iterable[0] = "test1"; iterable[1] = "test2"; iterable[2] = "test3"; assertIterator(IteratorUtils.asIterator(iterable), iterable.length); }
@Test public void shouldConvertThrowableToIterator() { final Exception ex = new Exception("test1"); assertIterator(IteratorUtils.asIterator(ex), 1); }
@Test public void shouldConvertMapToIterator() { final Map<String,String> m = new HashMap<>(); m.put("key1", "val1"); m.put("key2", "val2"); m.put("key3", "val3"); final Iterator itty = IteratorUtils.asIterator(m); for (int ix = 0; ix < m.size(); ix++) { final Map.Entry entry = (Map.Entry) itty.next(); assertEquals("key" + (ix + 1), entry.getKey()); assertEquals("val" + (ix + 1), entry.getValue()); } assertFalse(itty.hasNext()); }
@Test public void shouldConvertAnythingElseToIteratorByWrapping() { assertIterator(IteratorUtils.asIterator("test1"), 1); }