Package com.palantir.common.base
Class BatchingVisitableView<T>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.palantir.common.base.BatchingVisitableView<T>
-
- Type Parameters:
T
- The contained object type.
- All Implemented Interfaces:
BatchingVisitable<T>
public abstract class BatchingVisitableView<T> extends com.google.common.collect.ForwardingObject implements BatchingVisitable<T>
A wrapper forBatchingVisitable
which adds support for common operations.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
all(com.google.common.base.Predicate<? super T> predicate)
Returnstrue
iff every element satisfies the predicate.boolean
any(com.google.common.base.Predicate<? super T> predicate)
Returnstrue
iff one or more elements satisfy the predicate.<K extends Exception>
booleanbatchAccept(int batchSize, AbortingVisitor<? super List<T>,K> visitor)
This method should be used to visit elements in batches until the visitor returns false or there are no batches left to visit.BatchingVisitableView<T>
concat(BatchingVisitable<? extends T>... inputs)
<S extends Collection<? super T>>
ScopyInto(S collection)
Copies the elements in this visitable into an existing collection.long
count()
protected abstract BatchingVisitable<T>
delegate()
BatchingVisitableView<T>
filter(com.google.common.base.Predicate<? super T> predicate)
<U> BatchingVisitableView<T>
filter(Class<? extends U> type)
T
find(com.google.common.base.Predicate<? super T> predicate, T defaultValue)
void
forEach(int batchSize, Visitor<T> visitor)
void
forEach(Visitor<T> visitor)
T
getFirst()
Gets the first element in the visitable.T
getFirst(T defaultElement)
Gets the first element in the visitable.TokenBackedBasicResultsPage<T,T>
getFirstPage(int numToVisitArg)
T
getLast()
Gets the last element in the visitable.T
getLast(T defaultElement)
Gets the last element in the visitable.T
getMax(com.google.common.collect.Ordering<? super T> ordering, T defaultElement)
This will return the first maximal element in the visitable.T
getMin(com.google.common.collect.Ordering<? super T> ordering, T defaultElement)
This will return the first maximal element in the visitable.BatchingVisitableView<T>
hintBatchSize(int batchSizeHint)
com.google.common.collect.ImmutableList<T>
immutableCopy()
Returns an immutable copy of the elements in this visitable.com.google.common.collect.ImmutableSet<T>
immutableSetCopy()
Returns an immutable copy of the elements in this visitable.boolean
isEmpty()
boolean
isEqual(Iterable<T> it)
boolean
isEqual(Iterator<T> it)
BatchingVisitableView<T>
limit(long limit)
static <T> BatchingVisitableView<T>
of(BatchingVisitable<T> underlyingVisitable)
long
size()
BatchingVisitableView<T>
skip(long toSkip)
<U> BatchingVisitableView<U>
transform(com.google.common.base.Function<? super T,? extends U> fn)
<U> BatchingVisitableView<U>
transformBatch(com.google.common.base.Function<? super List<T>,? extends List<U>> fn)
This can also be used to filter or multiply the input.BatchingVisitableView<T>
unique()
BatchingVisitableView<T>
uniqueOn(com.google.common.base.Function<T,?> function)
BatchingVisitableView<T>
visitWhile(com.google.common.base.Predicate<T> condition)
-
-
-
Method Detail
-
delegate
protected abstract BatchingVisitable<T> delegate()
- Specified by:
delegate
in classcom.google.common.collect.ForwardingObject
-
of
public static <T> BatchingVisitableView<T> of(BatchingVisitable<T> underlyingVisitable)
-
batchAccept
public <K extends Exception> boolean batchAccept(int batchSize, AbortingVisitor<? super List<T>,K> visitor) throws K extends Exception
Description copied from interface:BatchingVisitable
This method should be used to visit elements in batches until the visitor returns false or there are no batches left to visit.- Specified by:
batchAccept
in interfaceBatchingVisitable<T>
- Parameters:
batchSize
- Each list passed to the visitor will be of batchSize except for the last one which could be smaller, but will not be empty;- Returns:
- true if the visitor always returned true or was never called. false if the visitor ever returned false.
- Throws:
K extends Exception
-
concat
public BatchingVisitableView<T> concat(BatchingVisitable<? extends T>... inputs)
-
count
public long count()
-
filter
public BatchingVisitableView<T> filter(com.google.common.base.Predicate<? super T> predicate)
-
filter
public <U> BatchingVisitableView<T> filter(Class<? extends U> type)
-
limit
public BatchingVisitableView<T> limit(long limit)
-
skip
public BatchingVisitableView<T> skip(long toSkip)
-
unique
public BatchingVisitableView<T> unique()
-
uniqueOn
public BatchingVisitableView<T> uniqueOn(com.google.common.base.Function<T,?> function)
-
hintBatchSize
public BatchingVisitableView<T> hintBatchSize(int batchSizeHint)
-
transform
public <U> BatchingVisitableView<U> transform(com.google.common.base.Function<? super T,? extends U> fn)
-
transformBatch
public <U> BatchingVisitableView<U> transformBatch(com.google.common.base.Function<? super List<T>,? extends List<U>> fn)
This can also be used to filter or multiply the input. It is common to return a list of a smaller size than you pass in if you are filtering.In the filtering case, the caller may only want one value, but you may have to visit batches of size 100 to find that value. In this case you should call .transformBatch and then call .hintBatchSize(100) so your function will get 100 at a time even though the final consumer is only requesting a batch of size 1. A good example of this is in
BatchingVisitablesTest#testHintPageSize()
-
visitWhile
public BatchingVisitableView<T> visitWhile(com.google.common.base.Predicate<T> condition)
-
getFirst
@Nullable public T getFirst()
Gets the first element in the visitable.- Returns:
- the first element or
null
if the visitable is empty
-
getFirst
@Nullable public T getFirst(@Nullable T defaultElement)
Gets the first element in the visitable. If a default value ofnull
is wanted, then consider callinggetFirst()
instead.- Returns:
- the first element or
defaultElement
if the visitable is empty
-
getFirstPage
public TokenBackedBasicResultsPage<T,T> getFirstPage(int numToVisitArg)
-
getLast
@Nullable public T getLast()
Gets the last element in the visitable.- Returns:
- the last element or
null
if the visitable is empty
-
getLast
@Nullable public T getLast(@Nullable T defaultElement)
Gets the last element in the visitable. If a default value ofnull
is wanted, then consider callinggetLast()
instead.- Returns:
- the last element or
defaultElement
if the visitable is empty
-
getMax
public T getMax(com.google.common.collect.Ordering<? super T> ordering, @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 T getMin(com.google.common.collect.Ordering<? super T> ordering, @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.
-
immutableCopy
public com.google.common.collect.ImmutableList<T> immutableCopy()
Returns an immutable copy of the elements in this visitable.- Throws:
NullPointerException
- if any elements in the visitable are null
-
immutableSetCopy
public com.google.common.collect.ImmutableSet<T> immutableSetCopy()
Returns an immutable copy of the elements in this visitable.- Throws:
NullPointerException
- if any elements in the visitable are null
-
copyInto
public <S extends Collection<? super T>> S copyInto(S collection)
Copies the elements in this visitable into an existing collection. This method has equivalent behaviour toIterables.addAll(collection, this)
.- Returns:
- a reference to
collection
, for convenience
-
any
public boolean any(com.google.common.base.Predicate<? super T> predicate)
Returnstrue
iff one or more elements satisfy the predicate.
-
all
public boolean all(com.google.common.base.Predicate<? super T> predicate)
Returnstrue
iff every element satisfies the predicate. If empty,true
is returned.
-
isEmpty
public boolean isEmpty()
-
size
public long size()
-
-