commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/iterators IteratorChain.java
Date Sat, 17 Aug 2002 11:28:37 GMT
scolebourne    2002/08/17 04:28:37

  Modified:    collections/src/java/org/apache/commons/collections/iterators
                        IteratorChain.java
  Log:
  Remove List constructor
  Added three new constructors
  Added get for iterator list
  Added extra null validation
  
  Revision  Changes    Path
  1.2       +82 -24    jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/IteratorChain.java
  
  Index: IteratorChain.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/IteratorChain.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IteratorChain.java	15 Aug 2002 23:13:51 -0000	1.1
  +++ IteratorChain.java	17 Aug 2002 11:28:36 -0000	1.2
  @@ -61,10 +61,11 @@
   package org.apache.commons.collections.iterators;
   
   import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.Collections;
   import java.util.Iterator;
   import java.util.List;
   import java.util.NoSuchElementException;
  -
   /**
    * <p>An IteratorChain is an Iterator that wraps one or
    * more Iterators.  When any method from the
  @@ -81,16 +82,17 @@
    * <p>Calling a method that adds new Iterator<i>after
    * a method in the Iterator interface
    * has been called</i> will result in an
  - * UnsupportedOperationException.  However, <i>take care</i>
  + * UnsupportedOperationException.  Subclasses should <i>take care</i>
    * to not alter the underlying List of Iterators.</p>
    * 
  + * @since 2.1
    * @author Morgan Delagrange
  + * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
    * @version $Id$
  - * @since 2.1
    */
   public class IteratorChain implements Iterator {
   
  -    protected List iteratorChain = null;
  +    protected final List iteratorChain = new ArrayList();
       protected int currentIteratorIndex = 0;
       protected Iterator currentIterator = null;
       // the "last used" Iterator is the Iterator upon which
  @@ -102,6 +104,9 @@
       // compare(Object,Object) is called
       protected boolean isLocked = false;
   
  +    // Constructors
  +    // -------------------------------------------------------------------
  +    
       /**
        * Construct an IteratorChain with no Iterators.
        * You must add at least Iterator before calling
  @@ -109,53 +114,106 @@
        * UnsupportedOperationException is thrown
        */
       public IteratorChain() {
  -        this(new ArrayList());
  +        super();
       }
   
       /**
        * Construct an IteratorChain with a single Iterator.
        * 
        * @param iterator first Iterator in the IteratorChain
  +     * @throws NullPointerException if the iterator is null
        */
       public IteratorChain(Iterator iterator) {
  -        iteratorChain = new ArrayList();
  -        iteratorChain.add(iterator);
  +        super();
  +        addIterator(iterator);
       }
   
       /**
  -     * Construct an IteratorChain from the Iterators in the
  -     * List.
  -     * 
  -     * @param list   List of Iterators
  +     * Constructs a new <Code>IteratorChain</Code> over the two
  +     * given iterators.
  +     *
  +     * @param a  the first child iterator
  +     * @param b  the second child iterator
  +     * @throws NullPointerException if either iterator is null
  +     */
  +    public IteratorChain(Iterator a, Iterator b) {
  +        super();
  +        addIterator(a);
  +        addIterator(b);
  +    }
  +
  +    /**
  +     * Constructs a new <Code>IteratorChain</Code> over the array
  +     * of iterators.
  +     *
  +     * @param iterators  the array of iterators
  +     * @throws NullPointerException if iterators array is or contains null
        */
  -    public IteratorChain(List list) {
  -        iteratorChain = list;
  +    public IteratorChain(Iterator[] iterators) {
  +        super();
  +        for (int i = 0; i < iterators.length; i++) {
  +            addIterator(iterators[i]);
  +        }
       }
   
       /**
  +     * Constructs a new <Code>IteratorChain</Code> over the collection
  +     * of iterators.
  +     *
  +     * @param iterators  the collection of iterators
  +     * @throws NullPointerException if iterators collection is or contains null
  +     * @throws ClassCastException if iterators collection doesn't contain an iterator
  +     */
  +    public IteratorChain(Collection iterators) {
  +        super();
  +        for (Iterator it = iterators.iterator(); it.hasNext();) {
  +            Iterator item = (Iterator) it.next();
  +            addIterator(item);
  +        }
  +    }
  +    
  +    // Public Methods
  +    // -------------------------------------------------------------------
  +
  +    /**
        * Add an Iterator to the end of the chain 
        * 
        * @param iterator Iterator to add
  +     * @throws IllegalStateException if I've already started iterating
  +     * @throws NullPointerException if the iterator is null
        */
       public void addIterator(Iterator iterator) {
           checkLocked();
  -
  +        if (iterator == null) {
  +            throw new NullPointerException("Iterator must not be null");
  +        }
           iteratorChain.add(iterator);
       }
   
       /**
  -     * Replace the Iterator at the given index     
  +     * Set the Iterator at the given index     
        * 
        * @param index      index of the Iterator to replace
        * @param iterator   Iterator to place at the given index
  -     * @exception IndexOutOfBoundsException
  -     *                   if index < 0 or index > size()
  +     * @throws IndexOutOfBoundsException if index &lt; 0 or index &gt; size()
  +     * @throws IllegalStateException if I've already started iterating
  +     * @throws NullPointerException if the iterator is null
        */
  -    public void setIterator(int index, Iterator iterator) 
  -    throws IndexOutOfBoundsException {
  +    public void setIterator(int index, Iterator iterator) throws IndexOutOfBoundsException
{
           checkLocked();
  +        if (iterator == null) {
  +            throw new NullPointerException("Iterator must not be null");
  +        }
  +        iteratorChain.set(index, iterator);
  +    }
   
  -        iteratorChain.set(index,iterator);
  +    /**
  +     * Get the list of Iterators (unmodifiable)
  +     * 
  +     * @return the unmodifiable list of iterators added
  +     */
  +    public List getIterators() {
  +        return Collections.unmodifiableList(iteratorChain);
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message