Pages { public static <E, X extends Exception> Stream<E> streamLazily( PageSupplier<E, X> supplier, int size) { return StreamSupport.stream(new PagedIterable<>(supplier, size).spliterator(), false); } 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 lazyStreamsAllElements() { List<String> result = Pages.streamLazily(testSource::getStrings, 2).collect(Collectors.toList()); assertEquals(result, testSource.strings); }
@Test public void lazyStreamingDoesNotPollNextPageUntilNeeded() { TestPagesSupplier src = spy(new TestPagesSupplier("string1", "string2", "string3")); assertTrue(Pages.streamLazily(src::getStrings, 1).anyMatch(s -> s.equals("string2"))); verify(src, times(2)).getStrings(anyInt(), anyLong()); verify(src).getStrings(1, 0); verify(src).getStrings(1, 1); }
@Test public void returnsEmptyStreamWhenFetchingLazily() { Stream<String> stream = Pages.streamLazily(new TestPagesSupplier()::getStrings); assertFalse(stream.findAny().isPresent()); }