diff --git a/src/java/org/apache/commons/collections/ArrayIterator.java b/src/java/org/apache/commons/collections/ArrayIterator.java index ba864366f..3ce933fa2 100644 --- a/src/java/org/apache/commons/collections/ArrayIterator.java +++ b/src/java/org/apache/commons/collections/ArrayIterator.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/ArrayIterator.java,v 1.11 2002/03/19 00:54:34 mas Exp $ - * $Revision: 1.11 $ - * $Date: 2002/03/19 00:54:34 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/ArrayIterator.java,v 1.12 2002/03/19 01:33:12 mas Exp $ + * $Revision: 1.12 $ + * $Date: 2002/03/19 01:33:12 $ * * ==================================================================== * @@ -64,12 +64,12 @@ import java.lang.reflect.Array; import java.util.Iterator; import java.util.NoSuchElementException; -/** Implements {@link Iterator} over an array of objects +/** Implements an {@link Iterator} over an array of objects. * * @author James Strachan * @author Mauricio S. Moura * @author Michael A. Smith - * @version $Revision: 1.11 $ + * @version $Revision: 1.12 $ */ public class ArrayIterator implements Iterator { @@ -78,9 +78,26 @@ public class ArrayIterator implements Iterator { private int index = 0; + /** + * Construct an ArrayIterator. Using this constructor, the iterator is + * equivalent to an empty iterator until {@link #setArray(Object)} is + * called to establish the array to iterate over. + **/ public ArrayIterator() { } - + + /** + * Construct an ArrayIterator that will iterate over the values in the + * specified array. + * + * @param array the array to iterate over. + * + * @exception IllegalArgumentException if array is not an + * array. + * + * @exception NullPointerException + * if array is null + **/ public ArrayIterator(Object array) { setArray( array ); } @@ -104,19 +121,50 @@ public class ArrayIterator implements Iterator { // Properties //------------------------------------------------------------------------- + + /** + * Retrieves the array that this iterator is iterating over. + * + * @return the array this iterator iterates over, or null if + * the no-arg constructor was used and {@link #setArray(Object)} has never + * been called with a valid array. + **/ public Object getArray() { return array; } /** + * Changes the array that the ArrayIterator should iterate over. If an + * array has previously been set (using the single-arg constructor or this + * method), that array along with the current iterator position within + * that array is discarded in favor of the argument to this method. This + * method can be used in combination with {@link #getArray()} to "reset" + * the iterator to the beginning of the array: + * + *
+     *    ArrayIterator iterator = ...
+     *    ...
+     *    iterator.setArray(iterator.getArray());
+     *  
+ * + * Note: Using i.setArray(i.getArray()) may throw a NullPointerException + * if no array has ever been set for the iterator (see {@link + * #getArray()}) + * + * @param array the array that the iterator should iterate over. + * * @exception IllegalArgumentException if array is not an * array. + * + * @exception NullPointerException + * if array is null **/ public void setArray( Object array ) { // Array.getLength throws IllegalArgumentException if the object is not - // an array. This call is made before saving the array and resetting - // the index so that the array iterator remains in a consistent state - // if the argument is not an array. + // an array or NullPointerException if the object is null. This call + // is made before saving the array and resetting the index so that the + // array iterator remains in a consistent state if the argument is not + // an array or is null. this.length = Array.getLength( array ); this.array = array; this.index = 0; diff --git a/src/test/org/apache/commons/collections/TestArrayIterator.java b/src/test/org/apache/commons/collections/TestArrayIterator.java index 3efe94edf..beb9040e5 100644 --- a/src/test/org/apache/commons/collections/TestArrayIterator.java +++ b/src/test/org/apache/commons/collections/TestArrayIterator.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestArrayIterator.java,v 1.6 2002/02/25 22:48:52 morgand Exp $ - * $Revision: 1.6 $ - * $Date: 2002/02/25 22:48:52 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestArrayIterator.java,v 1.7 2002/03/19 01:33:12 mas Exp $ + * $Revision: 1.7 $ + * $Date: 2002/03/19 01:33:12 $ * * ==================================================================== * @@ -73,7 +73,7 @@ import java.util.NoSuchElementException; * @author James Strachan * @author Mauricio S. Moura * @author Morgan Delagrange - * @version $Id: TestArrayIterator.java,v 1.6 2002/02/25 22:48:52 morgand Exp $ + * @version $Id: TestArrayIterator.java,v 1.7 2002/03/19 01:33:12 mas Exp $ */ public class TestArrayIterator extends TestIterator { @@ -122,5 +122,16 @@ public class TestArrayIterator extends TestIterator { e.getClass().equals((new NoSuchElementException()).getClass())); } } + + public void testNullToConstructor() { + try { + Iterator iter = new ArrayIterator(null); + + fail("Constructor should throw a NullPointerException when " + + "constructed with a null array"); + } catch (NullPointerException e) { + // expected + } + } }