public class Limit extends Object
A Limit is used to process a stream of data while limiting the results to a given window. This window is described by setting the number of items to skip and the maximal number of items to pass on. If the maximal number of items to pass on is null, no limiting will be performed.
This class can be used to "page" through result lists. Having a page size of 25 and showing the 2nd page, would lead to:
Limit limit = new Limit(25,25); // Skip the 25 items of the first page, and show up to 25 items.
List result = new ArrayList();
for(Object o : someList) {
if (limit.nextRow()) {
result.add(o);
}
// If we already fetched enough items, we can also stop processing further.
if (!limit.shouldContinue()) {
return;
}
}
Modifier and Type | Field and Description |
---|---|
static Limit |
UNLIMITED
Represents a limit which has no upper limit and does not skip any items.
|
Constructor and Description |
---|
Limit(int itemsToSkip,
Integer maxItems)
Creates a new
Limit based on the given parameters. |
Modifier and Type | Method and Description |
---|---|
<C> Predicate<C> |
asPredicate()
Converts the limit into a predicate.
|
int |
getMaxItems()
Returns the max number of items or 0 to indicate that there is no upper limit.
|
Integer |
getRemainingItems()
Returns the remaining number of items to be processed.
|
int |
getTotalItems()
Returns the total number of items to be processed.
|
boolean |
nextRow()
Notifies the limit, that the next item is being processed and determines if this is part of the result.
|
boolean |
shouldContinue()
Determines if already enough items have been processed or if processing should continue.
|
static Limit |
singleItem()
Represents a limit which only accepts the first item.
|
String |
toString() |
public static final Limit UNLIMITED
Although a limit is modified internally, we can use a constant here because a unlimited limit has not internal state.
public Limit(int itemsToSkip, Integer maxItems)
Limit
based on the given parameters.itemsToSkip
- denotes the number of items to skip.maxItems
- determines the max number of items. Can be null or 0 to indicate that no limiting
is active.public static Limit singleItem()
public boolean nextRow()
public boolean shouldContinue()
public <C> Predicate<C> asPredicate()
Note that the limit is stateful and therefore asPredicate should only be called once.
C
- type used by the stream using this predicate. Ignored as we do not operate on the
items itselfpublic int getMaxItems()
public int getTotalItems()
public Integer getRemainingItems()
Copyright © 2018. All rights reserved.