Traversers { @Nonnull public static <T> Traverser<T> traverseStream(@Nonnull Stream<T> stream) { return traverseSpliterator(stream.spliterator()).onFirstNull(stream::close); } private  Traversers(); @SuppressWarnings("unchecked") @Nonnull static Traverser<T> empty(); @Nonnull static Traverser<T> singleton(@Nonnull T item); @Nonnull static Traverser<T> traverseIterator(@Nonnull Iterator<? extends T> iterator); @Nonnull static Traverser<T> traverseIterator(@Nonnull Iterator<? extends T> iterator, boolean ignoreNulls); @Nonnull static Traverser<T> traverseSpliterator(@Nonnull Spliterator<T> spliterator); @Nonnull static Traverser<T> traverseEnumeration(@Nonnull Enumeration<T> enumeration); @Nonnull static Traverser<T> traverseStream(@Nonnull Stream<T> stream); @Nonnull static Traverser<T> traverseIterable(@Nonnull Iterable<? extends T> iterable); @Nonnull static Traverser<T> traverseArray(@Nonnull T[] array); @SafeVarargs static Traverser<T> traverseItems(T... items); @Nonnull static Traverser<T> lazy(@Nonnull Supplier<Traverser<T>> supplierOfTraverser);  }
@Test public void when_traverseStream_then_seeAllItems() { validateTraversal(traverseStream(of(1, 2))); }
@Test public void when_traverseStreamConsumed_then_streamClosed() { boolean[] closed = {false}; Traverser<Integer> trav = traverseStream(of(1).onClose(() -> closed[0] = true)); trav.next(); assertFalse(closed[0]); trav.next(); assertTrue(closed[0]); }