Pages { public static <E, X extends Exception> Iterable<E> iterateLazily( PageSupplier<E, X> supplier, int size) { return new PagedIterable<>(supplier, size); } private  Pages(); static Stream<E> stream(PageSupplier<E, X> supplier, int size); static Stream<E> stream(PageSupplier<E, X> supplier); static Iterable<E> iterate(PageSupplier<E, X> supplier, int size); static Iterable<E> iterate(PageSupplier<E, X> supplier); static Stream<E> streamLazily(
      PageSupplier<E, X> supplier, int size); static Stream<E> streamLazily(PageSupplier<E, X> supplier); static Iterable<E> iterateLazily(
      PageSupplier<E, X> supplier, int size); static Iterable<E> iterateLazily(PageSupplier<E, X> supplier); static final int DEFAULT_PAGE_SIZE; }
@Test public void lazyIteratesAllElements() { ArrayList<String> result = Lists.newArrayList(Pages.iterateLazily(testSource::getStrings, 2)); assertEquals(result, testSource.strings); }
@Test public void lazyIteratingDoesNotPollNextPageUntilNeeded() { TestPagesSupplier src = spy(new TestPagesSupplier("string1", "string2", "string3")); Iterator<String> it = Pages.iterateLazily(src::getStrings, 1).iterator(); it.next(); it.next(); verify(src, times(2)).getStrings(anyInt(), anyLong()); verify(src).getStrings(1, 0); verify(src).getStrings(1, 1); }
@Test public void returnsIterableWithNoeElementsWhileFetchingLazily() { Iterator<String> it = Pages.iterateLazily(new TestPagesSupplier()::getStrings).iterator(); assertFalse(it.hasNext()); }