net.sf.jga.util
Class LookAheadIterator<T>

java.lang.Object
  extended by net.sf.jga.util.LookAheadIterator<T>
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Iterator<T>

public class LookAheadIterator<T>
extends java.lang.Object
implements java.util.Iterator<T>, java.lang.Iterable<T>

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

Copyright © 2003-2005 David A. Hall

Author:
David A. Hall

Constructor Summary
LookAheadIterator(java.util.Iterator<? extends T> base)
          Builds a LookAheadIterator that can look ahead 1 element.
LookAheadIterator(java.util.Iterator<? extends T> base, int max)
          Builds a LookAheadIterator that can look ahead the given number of elements.
 
Method Summary
 int getMaxPeekSize()
          Returns the maximum offset that may be peeked.
 boolean hasNext()
           
 boolean hasNextPlus(int n)
          Returns true if there is an element at the Nth position.
 java.util.Iterator<T> iterator()
           
 T next()
           
 T peek(int n)
          Returns the element at the Nth position.
 void remove()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LookAheadIterator

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


LookAheadIterator

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

Throws:
java.lang.IllegalArgumentException - if max <= 0.
Method Detail

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:
java.lang.IllegalArgumentException - if n < 0 or n > max lookahead

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:
java.lang.IllegalArgumentException - if n < 0 or n > max lookahead
java.util.NoSuchElementException - if the Nth position is off the end of the iteration

getMaxPeekSize

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


iterator

public java.util.Iterator<T> iterator()
Specified by:
iterator in interface java.lang.Iterable<T>

hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator<T>

next

public T next()
Specified by:
next in interface java.util.Iterator<T>

remove

public void remove()
Specified by:
remove in interface java.util.Iterator<T>


Copyright © 2002-2006 David A. Hall. All Rights Reserved.