Top Description Fields Constructors Methods
org.apache.commons.collections4.iterators

public Class BoundedIterator<E>

extends Object
implements Iterator<E>
Class Inheritance
All Implemented Interfaces
java.util.Iterator
Imports
java.util.Iterator, .NoSuchElementException

Decorates another iterator to return elements in a specific range.

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.

Since
4.1

Field Summary

Modifier and TypeField and Description
private final Iterator<? extends E>
iterator

The iterator being decorated.

private final long
max

The max number of elements to return

private final long
offset

The offset to bound the first element return

private long
pos

The position of the current element

Constructor Summary

AccessConstructor and Description
public
BoundedIterator(final Iterator<? extends E>
the iterator to be decorated
iterator
,
final long
the index of the first element of the decorated iterator to return
offset
,
final long
the maximum number of elements of the decorated iterator to return
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.

Method Summary

Modifier and TypeMethod and Description
private boolean

Returns:

true if the iterator is within its bounds, false otherwise
checkBounds
()

Checks whether the iterator is still within its bounded range.

public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
private void
init()

Advances the underlying iterator to the beginning of the bounded range.

public E
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iteratorback to summary
private final Iterator<? extends E> iterator

The iterator being decorated.

maxback to summary
private final long max

The max number of elements to return

offsetback to summary
private final long offset

The offset to bound the first element return

posback to summary
private long pos

The position of the current element

Constructor Detail

BoundedIteratorback to summary
public BoundedIterator(final Iterator<? extends E> iterator, final long offset, final 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.

The iterator is immediately advanced until it reaches the position at offset, incurring O(n) time.

Parameters
iterator:Iterator<? extends E>

the iterator to be decorated

offset:long

the index of the first element of the decorated iterator to return

max:long

the maximum number of elements of the decorated iterator to return

Exceptions
NullPointerException:
if iterator is null
IllegalArgumentException:
if either offset or max is negative

Method Detail

checkBoundsback to summary
private boolean checkBounds()

Checks whether the iterator is still within its bounded range.

Returns:boolean

true if the iterator is within its bounds, false otherwise

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
initback to summary
private void init()

Advances the underlying iterator to the beginning of the bounded range.

nextback to summary
public E next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:E

the next element in the iteration

Annotations
@Override
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

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().

Annotations
@Override