Class BatchingVisitables
- java.lang.Object
-
- com.palantir.common.base.BatchingVisitables
-
public final class BatchingVisitables extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_BATCH_SIZE
static int
KEEP_ALL_BATCH_SIZE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> BatchingVisitableView<T>
concat(BatchingVisitable<? extends T>... inputs)
static <T> BatchingVisitableView<T>
concat(Iterable<? extends BatchingVisitable<? extends T>> inputs)
static <T> List<T>
copyToList(BatchingVisitable<T> v)
static long
count(BatchingVisitable<?> visitable)
static long
count(BatchingVisitable<?> visitable, int batchSize)
static <T> BatchingVisitableView<T>
emptyBatchingVisitable()
static <T> BatchingVisitableView<T>
filter(BatchingVisitable<T> visitable, com.google.common.base.Predicate<? super T> pred)
static <T> BatchingVisitableView<T>
flatten(int outerBatchHint, BatchingVisitable<? extends BatchingVisitable<? extends T>> inputs)
static <T> T
getFirst(BatchingVisitable<T> visitable)
static <T> T
getFirst(BatchingVisitable<T> visitable, T defaultElement)
static <T> TokenBackedBasicResultsPage<T,T>
getFirstPage(BatchingVisitable<T> v, int numToVisitArg)
static <T,TOKEN>
TokenBackedBasicResultsPage<T,TOKEN>getFirstPage(BatchingVisitable<T> v, int numToVisitArg, com.google.common.base.Function<T,TOKEN> tokenExtractor)
static <T> T
getLast(BatchingVisitable<T> visitable)
static <T> T
getLast(BatchingVisitable<T> visitable, T defaultElement)
static <T extends Comparable<? super T>>
TgetMax(BatchingVisitable<T> v)
This method will throw if there are nulls in the visitable.static <T> T
getMax(BatchingVisitable<T> v, com.google.common.collect.Ordering<? super T> o, T defaultElement)
This will return the first maximal element in the visitable.static <T extends Comparable<? super T>>
TgetMin(BatchingVisitable<T> v)
This method will throw if there are nulls in the visitable.static <T> T
getMin(BatchingVisitable<T> v, com.google.common.collect.Ordering<? super T> o, T defaultElement)
This will return the first maximal element in the visitable.static BatchingVisitable<Long>
getOrderedVisitableUsingSublists(OrderedSublistProvider<Long> sublistProvider, long startId)
static <T> BatchingVisitable<T>
getOrderedVisitableUsingSublists(OrderedSublistProvider<T> sublistProvider, long startId, com.google.common.base.Function<T,Long> idFunction)
static <T> BatchingVisitableView<T>
hintPageSize(BatchingVisitable<T> bv, int pageSize)
This method will wrap the passed visitable so it is called with the passed pageSize when it is called.static <T> boolean
isEmpty(BatchingVisitable<T> v)
static <T> boolean
isEqual(BatchingVisitable<T> v, Iterable<T> it)
static <T> boolean
isEqual(BatchingVisitable<T> v, Iterator<T> it)
static <T> BatchingVisitableView<T>
limit(BatchingVisitable<T> visitable, long limit)
static <T> BatchingVisitableView<T>
singleton(T t)
static <T> BatchingVisitableView<T>
skip(BatchingVisitable<T> visitable, long toSkip)
static <T> List<T>
take(BatchingVisitable<T> v, int howMany)
static <T> List<T>
take(BatchingVisitable<T> v, int howMany, boolean includeFirst)
static <F,T>
BatchingVisitableView<T>transform(BatchingVisitable<F> visitable, com.google.common.base.Function<? super F,? extends T> f)
static <F,T>
BatchingVisitableView<T>transformBatch(BatchingVisitable<F> visitable, com.google.common.base.Function<? super List<F>,? extends List<T>> f)
static <T> BatchingVisitableView<T>
unique(BatchingVisitable<T> visitable)
This is similar to the unix uniq command.static <T> BatchingVisitableView<T>
uniqueOn(BatchingVisitable<T> visitable, com.google.common.base.Function<T,?> function)
static <T> BatchingVisitableView<T>
visitWhile(BatchingVisitable<T> visitable, com.google.common.base.Predicate<T> condition)
static <T,S extends T>
BatchingVisitable<T>wrap(BatchingVisitable<S> visitable)
This will wrap the passed visitor and return a more generic type.
-
-
-
Field Detail
-
DEFAULT_BATCH_SIZE
public static final int DEFAULT_BATCH_SIZE
- See Also:
- Constant Field Values
-
KEEP_ALL_BATCH_SIZE
public static final int KEEP_ALL_BATCH_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
emptyBatchingVisitable
public static <T> BatchingVisitableView<T> emptyBatchingVisitable()
-
singleton
public static <T> BatchingVisitableView<T> singleton(T t)
-
count
public static long count(BatchingVisitable<?> visitable)
-
count
public static long count(BatchingVisitable<?> visitable, int batchSize)
-
isEmpty
public static <T> boolean isEmpty(BatchingVisitable<T> v)
-
visitWhile
public static <T> BatchingVisitableView<T> visitWhile(BatchingVisitable<T> visitable, com.google.common.base.Predicate<T> condition)
- Returns:
- Visitable containing elements from the start of the visitable for which the predicate holds true. Once boundingPredicate returns false for an element of visitable, no more elements will be included in the returned visitable.
-
getFirst
@Nullable public static <T> T getFirst(BatchingVisitable<T> visitable)
- Returns:
- the first element or null if the visitable is empty
-
getFirst
@Nullable public static <T> T getFirst(BatchingVisitable<T> visitable, @Nullable T defaultElement)
-
isEqual
public static <T> boolean isEqual(BatchingVisitable<T> v, Iterable<T> it)
-
isEqual
public static <T> boolean isEqual(BatchingVisitable<T> v, Iterator<T> it)
-
getMax
public static <T extends Comparable<? super T>> T getMax(BatchingVisitable<T> v)
This method will throw if there are nulls in the visitable.- Returns:
- null if the visitable is empty, otherwise return the smallest value.
-
getMax
public static <T> T getMax(BatchingVisitable<T> v, com.google.common.collect.Ordering<? super T> o, @Nullable T defaultElement)
This will return the first maximal element in the visitable. This method takes a default element and will return that if the visitable is empty. If the visitable is non-empty it will return the largest value in the visitable.A common way to use this would be to pass
null
as the defaultElement and have the ordering throw on null elements so you know the visitable doesn't have any nulls.
-
getMin
public static <T extends Comparable<? super T>> T getMin(BatchingVisitable<T> v)
This method will throw if there are nulls in the visitable.- Returns:
- null if the visitable is empty, otherwise return the smallest value.
-
getMin
public static <T> T getMin(BatchingVisitable<T> v, com.google.common.collect.Ordering<? super T> o, @Nullable T defaultElement)
This will return the first maximal element in the visitable. This method takes a default element and will return that if the visitable is empty. If the visitable is non-empty it will return the largest value in the visitable.A common way to use this would be to pass
null
as the defaultElement and have the ordering throw on null elements so you know the visitable doesn't have any nulls.
-
getLast
@Nullable public static <T> T getLast(BatchingVisitable<T> visitable)
-
getLast
@Nullable public static <T> T getLast(BatchingVisitable<T> visitable, @Nullable T defaultElement)
-
filter
public static <T> BatchingVisitableView<T> filter(BatchingVisitable<T> visitable, com.google.common.base.Predicate<? super T> pred)
-
transform
public static <F,T> BatchingVisitableView<T> transform(BatchingVisitable<F> visitable, com.google.common.base.Function<? super F,? extends T> f)
-
transformBatch
public static <F,T> BatchingVisitableView<T> transformBatch(BatchingVisitable<F> visitable, com.google.common.base.Function<? super List<F>,? extends List<T>> f)
-
limit
public static <T> BatchingVisitableView<T> limit(BatchingVisitable<T> visitable, long limit)
-
skip
public static <T> BatchingVisitableView<T> skip(BatchingVisitable<T> visitable, long toSkip)
-
unique
public static <T> BatchingVisitableView<T> unique(BatchingVisitable<T> visitable)
This is similar to the unix uniq command.If there is a sequence of identical values, it will be replaced by one value. For example "AAABABBB" will become "ABAB" If the list passed in is sorted, then it will actually result in a list of unique elements
This uses
Objects#equal(Object, Object)
to do comparisons.null is supported bug discouraged
-
uniqueOn
public static <T> BatchingVisitableView<T> uniqueOn(BatchingVisitable<T> visitable, com.google.common.base.Function<T,?> function)
-
copyToList
public static <T> List<T> copyToList(BatchingVisitable<T> v)
-
take
public static <T> List<T> take(BatchingVisitable<T> v, int howMany)
-
take
public static <T> List<T> take(BatchingVisitable<T> v, int howMany, boolean includeFirst)
-
getFirstPage
public static <T,TOKEN> TokenBackedBasicResultsPage<T,TOKEN> getFirstPage(BatchingVisitable<T> v, int numToVisitArg, com.google.common.base.Function<T,TOKEN> tokenExtractor)
-
getFirstPage
public static <T> TokenBackedBasicResultsPage<T,T> getFirstPage(BatchingVisitable<T> v, int numToVisitArg)
-
hintPageSize
public static <T> BatchingVisitableView<T> hintPageSize(BatchingVisitable<T> bv, int pageSize)
This method will wrap the passed visitable so it is called with the passed pageSize when it is called.This can be used to make the performance of batching visitables better. One example of where this is useful is if I just visit the results one at a time, but I know that I will visit 100 results, then I can save a lot of potential round trips by hinting a page size of 100.
-
wrap
public static <T,S extends T> BatchingVisitable<T> wrap(BatchingVisitable<S> visitable)
This will wrap the passed visitor and return a more generic type. Since Visitables just produce values, their type can be made more generic because a Visitable<Long> can safely be cast to a Visitable<Object>.- See Also:
for more helper methods
-
concat
public static <T> BatchingVisitableView<T> concat(BatchingVisitable<? extends T>... inputs)
-
concat
public static <T> BatchingVisitableView<T> concat(Iterable<? extends BatchingVisitable<? extends T>> inputs)
-
flatten
public static <T> BatchingVisitableView<T> flatten(int outerBatchHint, BatchingVisitable<? extends BatchingVisitable<? extends T>> inputs)
-
getOrderedVisitableUsingSublists
public static BatchingVisitable<Long> getOrderedVisitableUsingSublists(OrderedSublistProvider<Long> sublistProvider, @Inclusive long startId)
-
getOrderedVisitableUsingSublists
public static <T> BatchingVisitable<T> getOrderedVisitableUsingSublists(OrderedSublistProvider<T> sublistProvider, @Inclusive long startId, com.google.common.base.Function<T,Long> idFunction)
-
-