LazyCachingIteratorToIterable implements Iterable<T> { @Override public Iterator<T> iterator() { return new CachingIterator(); } LazyCachingIteratorToIterable(Iterator<T> iterator); @Override Iterator<T> iterator(); @Override int hashCode(); @Override boolean equals(@Nullable Object obj); @Override String toString();  }
@Test public void testEmptyIterator() { Iterable<Object> iterable = new LazyCachingIteratorToIterable<>(Iterators.forArray()); assertArrayEquals(new Object[0], Iterables.toArray(iterable, Object.class)); assertArrayEquals(new Object[0], Iterables.toArray(iterable, Object.class)); thrown.expect(NoSuchElementException.class); iterable.iterator().next(); }
@Test public void testInterleavedIteration() { Iterable<String> iterable = new LazyCachingIteratorToIterable<>(Iterators.forArray("A", "B", "C")); Iterator<String> iterator1 = iterable.iterator(); assertTrue(iterator1.hasNext()); assertEquals("A", iterator1.next()); Iterator<String> iterator2 = iterable.iterator(); assertTrue(iterator2.hasNext()); assertEquals("A", iterator2.next()); assertTrue(iterator2.hasNext()); assertEquals("B", iterator2.next()); assertTrue(iterator1.hasNext()); assertEquals("B", iterator1.next()); assertTrue(iterator1.hasNext()); assertEquals("C", iterator1.next()); assertFalse(iterator1.hasNext()); assertTrue(iterator2.hasNext()); assertEquals("C", iterator2.next()); assertFalse(iterator2.hasNext()); thrown.expect(NoSuchElementException.class); iterator1.next(); }