BatchingVisitables { public static <T, TOKEN> TokenBackedBasicResultsPage<T, TOKEN> getFirstPage(BatchingVisitable<T> v, int numToVisitArg, Function<T, TOKEN> tokenExtractor) { Preconditions.checkArgument(numToVisitArg >= 0, "numToVisit cannot be negative. Value was: %d", numToVisitArg); if (numToVisitArg == Integer.MAX_VALUE) { numToVisitArg--; } final int numToVisit = numToVisitArg + 1; ImmutableList<T> list = BatchingVisitableView.of(v).limit(numToVisit).immutableCopy(); com.palantir.logsafe.Preconditions.checkState(list.size() <= numToVisit); if (list.size() >= numToVisit) { TOKEN token = tokenExtractor.apply(list.get(list.size() - 1)); list = list.subList(0, numToVisit-1); return new SimpleTokenBackedResultsPage<T, TOKEN>(token, list, true); } return new SimpleTokenBackedResultsPage<T, TOKEN>(null, list, false); } private  BatchingVisitables(); static BatchingVisitableView<T> emptyBatchingVisitable(); static BatchingVisitableView<T> singleton(final T t); static long count(BatchingVisitable<?> visitable); static long count(BatchingVisitable<?> visitable, int batchSize); static boolean isEmpty(BatchingVisitable<T> v); static BatchingVisitableView<T> visitWhile(final BatchingVisitable<T> visitable,
            final Predicate<T> condition); @Nullable static T getFirst(BatchingVisitable<T> visitable); @Nullable static T getFirst(BatchingVisitable<T> visitable, @Nullable T defaultElement); static boolean isEqual(BatchingVisitable<T> v, final Iterable<T> it); static boolean isEqual(BatchingVisitable<T> v, final Iterator<T> it); static T getMax(BatchingVisitable<T> v); static T getMax(BatchingVisitable<T> v,
                               final Ordering<? super T> o,
                               @Nullable T defaultElement); static T getMin(BatchingVisitable<T> v); static T getMin(BatchingVisitable<T> v,
                               final Ordering<? super T> o,
                               @Nullable T defaultElement); @Nullable static T getLast(BatchingVisitable<T> visitable); @Nullable static T getLast(BatchingVisitable<T> visitable, @Nullable T defaultElement); static BatchingVisitableView<T> filter(BatchingVisitable<T> visitable, final Predicate<? super T> pred); static BatchingVisitableView<T> transform(BatchingVisitable<F> visitable, final Function<? super F, ? extends T> f); static BatchingVisitableView<T> transformBatch(final BatchingVisitable<F> visitable,
            final Function<? super List<F>, ? extends List<T>> f); static BatchingVisitableView<T> limit(final BatchingVisitable<T> visitable, final long limit); static BatchingVisitableView<T> skip(final BatchingVisitable<T> visitable, final long toSkip); static BatchingVisitableView<T> unique(final BatchingVisitable<T> visitable); static BatchingVisitableView<T> uniqueOn(final BatchingVisitable<T> visitable, final Function<T, ?> function); static List<T> copyToList(BatchingVisitable<T> v); static List<T> take(BatchingVisitable<T> v, int howMany); static List<T> take(BatchingVisitable<T> v, final int howMany, final boolean includeFirst); static TokenBackedBasicResultsPage<T, TOKEN> getFirstPage(BatchingVisitable<T> v,
                                                                                int numToVisitArg,
                                                                                Function<T, TOKEN> tokenExtractor); static TokenBackedBasicResultsPage<T, T> getFirstPage(BatchingVisitable<T> v, int numToVisitArg); static BatchingVisitableView<T> hintPageSize(final BatchingVisitable<T> bv, final int pageSize); static BatchingVisitable<T> wrap(final BatchingVisitable<S> visitable); static BatchingVisitableView<T> concat(BatchingVisitable<? extends T>... inputs); static BatchingVisitableView<T> concat(final Iterable<? extends BatchingVisitable<? extends T>> inputs); static BatchingVisitableView<T> flatten(final int outerBatchHint, final BatchingVisitable<? extends BatchingVisitable<? extends T>> inputs); static BatchingVisitable<Long> getOrderedVisitableUsingSublists(
            final OrderedSublistProvider<Long> sublistProvider, @Inclusive final long startId); static BatchingVisitable<T> getOrderedVisitableUsingSublists(
            final OrderedSublistProvider<T> sublistProvider, @Inclusive final long startId,
            final Function<T, Long> idFunction); static final int DEFAULT_BATCH_SIZE; @SuppressWarnings("unused") // Used in internal legacy code
    static final int KEEP_ALL_BATCH_SIZE; }
@Test public void testGetFirstPage() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L)); TokenBackedBasicResultsPage<Long, Long> page = BatchingVisitables.getFirstPage(visitor, 0); assertEquals("page results had wrong size!", 0, page.getResults().size()); assertEquals("page.moreResultsAvailable was wrong", true, page.moreResultsAvailable()); page = BatchingVisitables.getFirstPage(visitor, 1); assertEquals("page results had wrong size!", 1, page.getResults().size()); assertEquals("page.getResults was wrong", Lists.newArrayList(0L), page.getResults()); assertEquals("page.moreResultsAvailable was wrong", true, page.moreResultsAvailable()); page = BatchingVisitables.getFirstPage(visitor, 3); assertEquals("page results had wrong size!", 3, page.getResults().size()); assertEquals("page.getResults was wrong", Lists.newArrayList(0L, 1L, 2L), page.getResults()); assertEquals("page.moreResultsAvailable was wrong", true, page.moreResultsAvailable()); page = BatchingVisitables.getFirstPage(visitor, 4); assertEquals("page results had wrong size!", 4, page.getResults().size()); assertEquals("page.getResults was wrong", Lists.newArrayList(0L, 1L, 2L, 3L), page.getResults()); assertEquals("page.moreResultsAvailable was wrong", false, page.moreResultsAvailable()); page = BatchingVisitables.getFirstPage(visitor, 7); assertEquals("page results had wrong size!", 4, page.getResults().size()); assertEquals("page.getResults was wrong", Lists.newArrayList(0L, 1L, 2L, 3L), page.getResults()); assertEquals("page.moreResultsAvailable was wrong", false, page.moreResultsAvailable()); try { BatchingVisitables.getFirstPage(visitor, -1); fail("Should not allow visiting -1 elements."); } catch (IllegalArgumentException e) { } }