public class BoundedIterator<E>
extends java.lang.Object
implements java.util.Iterator<E>
The decorated iterator is bounded in the range [offset, offset+max).
The offset
corresponds to the position of the first element to
be returned from the decorated iterator, and max
is the maximum
number of elements to be returned at most.
In case an offset parameter other than 0 is provided, the decorated iterator is immediately advanced to this position, skipping all elements before that position.
Modifier and Type | Field and Description |
---|---|
private java.util.Iterator<? extends E> |
iterator
The iterator being decorated.
|
private long |
max
The max number of elements to return
|
private long |
offset
The offset to bound the first element return
|
private long |
pos
The position of the current element
|
Constructor and Description |
---|
BoundedIterator(java.util.Iterator<? extends E> iterator,
long offset,
long max)
Decorates the specified iterator to return at most the given number of elements,
skipping all elements until the iterator reaches the position at
offset . |
Modifier and Type | Method and Description |
---|---|
private boolean |
checkBounds()
Checks whether the iterator is still within its bounded range.
|
boolean |
hasNext() |
private void |
init()
Advances the underlying iterator to the beginning of the bounded range.
|
E |
next() |
void |
remove() |
private final java.util.Iterator<? extends E> iterator
private final long offset
private final long max
private long pos
public BoundedIterator(java.util.Iterator<? extends E> iterator, long offset, long max)
offset
.
The iterator is immediately advanced until it reaches the position at offset
,
incurring O(n) time.
iterator
- the iterator to be decoratedoffset
- the index of the first element of the decorated iterator to returnmax
- the maximum number of elements of the decorated iterator to returnjava.lang.NullPointerException
- if iterator is nulljava.lang.IllegalArgumentException
- if either offset or max is negativeprivate void init()
public boolean hasNext()
hasNext
in interface java.util.Iterator<E>
private boolean checkBounds()
true
if the iterator is within its bounds, false
otherwisepublic void remove()
In case an offset other than 0 was specified, the underlying iterator will be advanced
to this position upon creation. A call to remove()
will still result in an
IllegalStateException
if no explicit call to next()
has been made prior
to calling remove()
.
remove
in interface java.util.Iterator<E>