org.clapper.util.misc
Class MultiIterator<T>

java.lang.Object
  extended by org.clapper.util.misc.MultiIterator<T>
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Iterator<T>

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

The MultiIterator class provides a way to iterate over multiple Collection, Enumeration and Iterator objects at once. You instantiate a MultiIterator object and add one or more Collection, Enumeration or Iterator objects to it; when you use the iterator, it iterates over the contents of each contained composite object, one by one, in the order they were added to the MultiIterator. When the iterator reaches the end of one object's contents, it moves on to the next object, until no more composite objects are left.

Version:
$Revision: 6735 $
Author:
Copyright © 2004-2007 Brian M. Clapper
See Also:
Iterator, Enumeration, Collection

Constructor Summary
MultiIterator()
          Allocate a new MultiIterator object.
MultiIterator(java.util.Collection<java.util.Collection<T>> coll)
          Allocate a new MultiIterator object that will iterate, in turn, over the contents of each Collection in the supplied Collection
MultiIterator(java.util.Collection<T>[] array)
          Allocate a new MultiIterator object that will iterate, in turn, over the contents of each Collection in the supplied array.
 
Method Summary
 void addCollection(java.util.Collection<T> collection)
          Add a Collection to the end of the list of composite objects being iterated over.
 void addEnumeration(java.util.Enumeration<T> enumeration)
          Add an Enumeration to the end of the list of composite objects being iterated over.
 void addIterator(java.util.Iterator<T> iterator)
          Add an Iterator to the end of the list of composite objects being iterated over.
 boolean hasNext()
          Determine whether the underlying Iterator has more elements.
 java.util.Iterator<T> iterator()
          Returns this iterator.
 T next()
          Get the next element from the underlying array.
 void remove()
          Remove the object most recently extracted from the iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiIterator

public MultiIterator()
Allocate a new MultiIterator object.


MultiIterator

public MultiIterator(java.util.Collection<T>[] array)
Allocate a new MultiIterator object that will iterate, in turn, over the contents of each Collection in the supplied array.

Parameters:
array - The Collections over which to iterate
See Also:
addCollection(Collection)

MultiIterator

public MultiIterator(java.util.Collection<java.util.Collection<T>> coll)
Allocate a new MultiIterator object that will iterate, in turn, over the contents of each Collection in the supplied Collection

Parameters:
coll - A Collection of Collection objects
See Also:
addCollection(Collection)
Method Detail

addCollection

public void addCollection(java.util.Collection<T> collection)

Add a Collection to the end of the list of composite objects being iterated over. It's safe to call this method while iterating, as long as you haven't reached the end of the last composite object currently in the iterator.

Note: This method is simply shorthand for:

addIterator (collection.iterator());

Parameters:
collection - The Collection to add.
See Also:
addIterator(java.util.Iterator), addEnumeration(java.util.Enumeration)

addIterator

public void addIterator(java.util.Iterator<T> iterator)
Add an Iterator to the end of the list of composite objects being iterated over. It's safe to call this method while iterating, as long as you haven't reached the end of the last composite object currently in the iterator.

Parameters:
iterator - The Iterator to add.
See Also:
addCollection(java.util.Collection), addEnumeration(java.util.Enumeration)

addEnumeration

public void addEnumeration(java.util.Enumeration<T> enumeration)
Add an Enumeration to the end of the list of composite objects being iterated over. It's safe to call this method while iterating, as long as you haven't reached the end of the last composite object currently in the iterator.

Note: This method is simply shorthand for:

addIterator (new EnumerationIterator (enumeration));

Parameters:
enumeration - The Enumeration to add.
See Also:
addCollection(java.util.Collection), addIterator(java.util.Iterator), EnumerationIterator

hasNext

public boolean hasNext()
Determine whether the underlying Iterator has more elements.

Specified by:
hasNext in interface java.util.Iterator<T>
Returns:
true if and only if a call to nextElement() will return an element, false otherwise.
See Also:
next()

iterator

public java.util.Iterator<T> iterator()
Returns this iterator. Necessary for the Iterable interface.

Specified by:
iterator in interface java.lang.Iterable<T>
Returns:
this object

next

public T next()
       throws java.util.NoSuchElementException
Get the next element from the underlying array.

Specified by:
next in interface java.util.Iterator<T>
Returns:
the next element from the underlying array
Throws:
java.util.NoSuchElementException - No more elements exist
See Also:
Iterator.next()

remove

public void remove()
Remove the object most recently extracted from the iterator. The object is removed from whatever underlying Collection is currently being traversed.

Specified by:
remove in interface java.util.Iterator<T>


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.