com.ccg.util
Class ArrayIterator

java.lang.Object
  extended by com.ccg.util.ArrayIterator
All Implemented Interfaces:
Iterator

public final class ArrayIterator
extends Object
implements Iterator

Convert any array of objects into an Iterator.

Java probably has this functionality somewhere (I just couldn't seem to find it). This simple class puts the wrapper around an array of objects to make the array behave like a standard Java Iterator.

Some notes:

Since:
1.0
Version:
$Revision: 1.2 $
Author:
$Author: pkb $

Constructor Summary
ArrayIterator(Object[] array, boolean allowNullRemove)
          Contruct the array iterator.
 
Method Summary
 boolean hasNext()
          Determine if there are more elements.
 Object next()
          Get next element in Iterator.
 void remove()
          Attempt to remove object from the iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayIterator

public ArrayIterator(Object[] array,
                     boolean allowNullRemove)
Contruct the array iterator. This is the method used to construct an array iterator, you simply pass the array of elements which you want to iterate over. Currently this iterator keeps a light copy of the array passed (hence you shouldn't modify the array passed until you are done with the iterator).

Parameters:
array - Array of objects to appear when one enumerates. You may pass null.
allowNullRemove - If you pass true, we will support the "remove()" operation by setting the corresponding entry in the array to null. If you pass false, the "remove()" operation will not be supported and a exception will be thrown if you attempt to use it.
Since:
1.0
Method Detail

hasNext

public boolean hasNext()
Determine if there are more elements. This routine indicates whether the Iterator has more elements left in it to enumerate over.

Specified by:
hasNext in interface Iterator
Returns:
true if there are any elements left, false if not.
See Also:
Iterator

next

public Object next()
Get next element in Iterator. This returns the next item in the Iterator. Only call this if hasNext() returns true.

Specified by:
next in interface Iterator
Returns:
Returns next element in iterator - will throw an exception on you if you attempt to fetch the next element once the "hasNext()" returns false.
See Also:
Iterator

remove

public void remove()
            throws UnsupportedOperationException,
                   IllegalStateException
Attempt to remove object from the iterator.

When you construct this object you have the option of allowing the "remove()" method to function. If you didn't enable the "remove()" method at the time of construction, any invocation will throw the UnsupportedOperationException.

The "next()" method must be used PRIOR to the invocation of the "remove()" method (as we remove the item returned by the last invocation of "next").

The successful removal of a item results in its corresponding entry in the original array being set to null.

Specified by:
remove in interface Iterator
Throws:
UnsupportedOperationException - The "remove" operation is not supported by this iterator.
IllegalStateException - If an attempt to use the "remove" operation was made PRIOR to use of the "next" method.
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.