@Test public void testLimit() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); BatchingVisitableView<Long> limited = BatchingVisitableView.of(visitor).limit(3); ImmutableList<Long> copy = limited.immutableCopy(); assertEquals("limit produced unexpected result", ImmutableList.of(0L, 1L, 2L), copy); assertFalse("alwaysFalse should be false", limited.batchAccept(1, AbortingVisitors.alwaysFalse())); assertTrue("alwaysTrue should be true", limited.batchAccept(1, AbortingVisitors.alwaysTrue())); assertTrue("alwaysTrue should be true twice", limited.batchAccept(2, AbortingVisitors.alwaysTrue())); assertTrue("alwaysTrue should be true thrice", limited.batchAccept(3, AbortingVisitors.alwaysTrue())); CountingVisitor<Long> cv = new CountingVisitor<>(); assertTrue("batchAccept should be true", limited.batchAccept(2, AbortingVisitors.batching(cv))); assertEquals("CountingVisitor had wrong count", 3, cv.count); assertTrue("batchAccept should be true", limited.batchAccept(3, AbortingVisitors.batching(cv))); assertEquals("CountingVisitor had wrong count", 6, cv.count); assertTrue("batchAccept should be true", limited.batchAccept(4, AbortingVisitors.batching(cv))); assertEquals("CountingVisitor had wrong count", 9, cv.count); assertTrue("batchAccept should be trivially true after everything was skipped", limited.skip(3).batchAccept(1, AbortingVisitors.alwaysFalse())); LimitVisitor<Long> lv = new LimitVisitor<>(3); assertFalse("batchAccept should be false", limited.batchAccept(1, AbortingVisitors.batching(lv))); lv = new LimitVisitor<>(4); assertTrue("batchAccept should be true", limited.batchAccept(1, AbortingVisitors.batching(lv))); lv = new LimitVisitor<>(2); assertFalse("batchAccept should be false", limited.batchAccept(1, AbortingVisitors.batching(lv))); assertFalse("batchAccept should be false", limited.hintBatchSize(10) .batchAccept(1, AbortingVisitors.alwaysFalse())); assertTrue("batchAccept should be trivially true after everything was skipped", limited.hintBatchSize(10).skip(3) .batchAccept(1, AbortingVisitors.alwaysFalse())); assertEquals("smaller limits should precede larger limits", 2, limited.limit(2).limit(4).size()); assertEquals("smaller limits should precede larger limits", 2, limited.limit(4).limit(2).size()); assertEquals("limited size shouldn't be greater than the original size!", 3, limited.limit(4).size()); }
public static <T> BatchingVisitableView<T> limit(final BatchingVisitable<T> visitable, final long limit) { com.palantir.logsafe.Preconditions.checkNotNull(visitable); com.palantir.logsafe.Preconditions.checkArgument(limit >= 0); if (limit == 0) { return emptyBatchingVisitable(); } return BatchingVisitableView.of(new AbstractBatchingVisitable<T>() { @Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { if (batchSizeHint > limit) { batchSizeHint = (int) limit; } visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { long visited = 0; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { if (!v.visitOne(item)) { return false; } visited++; if (visited >= limit) { return false; } } return true; } }); } }); }
BatchingVisitables { public static <T> BatchingVisitableView<T> limit(final BatchingVisitable<T> visitable, final long limit) { com.palantir.logsafe.Preconditions.checkNotNull(visitable); com.palantir.logsafe.Preconditions.checkArgument(limit >= 0); if (limit == 0) { return emptyBatchingVisitable(); } return BatchingVisitableView.of(new AbstractBatchingVisitable<T>() { @Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { if (batchSizeHint > limit) { batchSizeHint = (int) limit; } visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { long visited = 0; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { if (!v.visitOne(item)) { return false; } visited++; if (visited >= limit) { return false; } } return true; } }); } }); } }
BatchingVisitables { public static <T> BatchingVisitableView<T> limit(final BatchingVisitable<T> visitable, final long limit) { com.palantir.logsafe.Preconditions.checkNotNull(visitable); com.palantir.logsafe.Preconditions.checkArgument(limit >= 0); if (limit == 0) { return emptyBatchingVisitable(); } return BatchingVisitableView.of(new AbstractBatchingVisitable<T>() { @Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { if (batchSizeHint > limit) { batchSizeHint = (int) limit; } visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { long visited = 0; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { if (!v.visitOne(item)) { return false; } visited++; if (visited >= limit) { return false; } } return true; } }); } }); } private  BatchingVisitables(); }
BatchingVisitables { public static <T> BatchingVisitableView<T> limit(final BatchingVisitable<T> visitable, final long limit) { com.palantir.logsafe.Preconditions.checkNotNull(visitable); com.palantir.logsafe.Preconditions.checkArgument(limit >= 0); if (limit == 0) { return emptyBatchingVisitable(); } return BatchingVisitableView.of(new AbstractBatchingVisitable<T>() { @Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { if (batchSizeHint > limit) { batchSizeHint = (int) limit; } visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { long visited = 0; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { if (!v.visitOne(item)) { return false; } visited++; if (visited >= limit) { return false; } } return true; } }); } }); } 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); }
BatchingVisitables { public static <T> BatchingVisitableView<T> limit(final BatchingVisitable<T> visitable, final long limit) { com.palantir.logsafe.Preconditions.checkNotNull(visitable); com.palantir.logsafe.Preconditions.checkArgument(limit >= 0); if (limit == 0) { return emptyBatchingVisitable(); } return BatchingVisitableView.of(new AbstractBatchingVisitable<T>() { @Override protected <K extends Exception> void batchAcceptSizeHint(int batchSizeHint, final ConsistentVisitor<T, K> v) throws K { if (batchSizeHint > limit) { batchSizeHint = (int) limit; } visitable.batchAccept(batchSizeHint, new AbortingVisitor<List<T>, K>() { long visited = 0; @Override public boolean visit(List<T> batch) throws K { for (T item : batch) { if (!v.visitOne(item)) { return false; } visited++; if (visited >= limit) { return false; } } return true; } }); } }); } 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; }