net.sf.jga.util

Class LookAheadIterator<T>

public class LookAheadIterator<T> extends Object implements Iterator<T>

Iterator that allows the program to look at and operate on the next few elements without consuming them.

Copyright © 2003 David A. Hall

Author: David A. Hall

Constructor Summary
LookAheadIterator(Iterator<? extends T> base)
Builds a LookAheadIterator that can look ahead 1 element.
LookAheadIterator(Iterator<? extends T> base, int max)
Builds a LookAheadIterator that can look ahead the given number of elements.
Method Summary
intgetMaxPeekSize()
Returns the maximum offset that may be peeked.
booleanhasNext()
booleanhasNextPlus(int n)
Returns true if there is an element at the Nth position.
Tnext()
Tpeek(int n)
Returns the element at the Nth position.
voidremove()
static <T> LookAheadIterator<T>wrapIfNecessary(Iterator<? extends T> base, int max)
Creates a LookAheadIterator of the given size to wrap the given iterator, if the given iterator isn't already of the proper type and size

Constructor Detail

LookAheadIterator

public LookAheadIterator(Iterator<? extends T> base)
Builds a LookAheadIterator that can look ahead 1 element.

LookAheadIterator

public LookAheadIterator(Iterator<? extends T> base, int max)
Builds a LookAheadIterator that can look ahead the given number of elements.

Throws: IllegalArgumentException if max <= 0.

Method Detail

getMaxPeekSize

public int getMaxPeekSize()
Returns the maximum offset that may be peeked.

hasNext

public boolean hasNext()

Returns:

hasNextPlus

public boolean hasNextPlus(int n)
Returns true if there is an element at the Nth position. Put another way, returns true if there are enough elements remaining in the iterator that next() could be called N times without having a NoSuchElementException thrown.

Returns: true if there is an element at the Nth position

Throws: IllegalArgumentException if n < 0 or n > max lookahead

next

public T next()

Returns:

peek

public T peek(int n)
Returns the element at the Nth position. Put another way, returns the element that the Nth call to next() would return. The current position of the iteration is not modified.

Returns: the element at the Nth position

Throws: IllegalArgumentException if n < 0 or n > max lookahead NoSuchElementException if the Nth position is off the end of the iteration

remove

public void remove()

wrapIfNecessary

public static <T> LookAheadIterator<T> wrapIfNecessary(Iterator<? extends T> base, int max)
Creates a LookAheadIterator of the given size to wrap the given iterator, if the given iterator isn't already of the proper type and size