au.com.zip.cs
Class CounterIterator

java.lang.Object
  extended by au.com.zip.cs.UselessIterator
      extended by au.com.zip.cs.CounterIterator
All Implemented Interfaces:
Iterator

public class CounterIterator
extends UselessIterator

An Iterator that counts from zero to a bound. Handy to subclass to iterate over arrays or Lists.

Author:
Cameron Simpson <cs@zip.com.au> 3aug2003

Constructor Summary
CounterIterator(int size)
          Construct a CounterIterator that counts from 0 to size-1.
 
Method Summary
protected  void bump()
          A hack to bump the iterator index by one.
 int getBound()
          Return the bound of this CounterIterator.
 int getIndex()
          Return the index of the last Object returned by next().
 boolean hasNext()
           
 Object next()
          Return the next element.
 
Methods inherited from class au.com.zip.cs.UselessIterator
remove
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CounterIterator

public CounterIterator(int size)
Construct a CounterIterator that counts from 0 to size-1.

Parameters:
size - the Iterator bound
Method Detail

getBound

public int getBound()
Return the bound of this CounterIterator.

Returns:
the bound

getIndex

public int getIndex()
Return the index of the last Object returned by next().

Returns:
the last Object index

hasNext

public boolean hasNext()

bump

protected void bump()
A hack to bump the iterator index by one. Usually called by subclasses when overriding next() to avoid the wasted construction of an Integer.
Example:
// A next() that returns List elements.
 public Object next()
 bump();
 return stateList.get(getIndex());
 }


next

public Object next()
Return the next element. This returns a new Integer.

Returns:
the next element