mas 02/03/18 16:54:35
Modified: collections/src/java/org/apache/commons/collections
ArrayIterator.java
Log:
If object is not an array, fail on from the constructor or from
setArray rather than from hasNext or next. This gives a more
fail-fast behavior.
Revision Changes Path
1.11 +17 -6
jakarta-commons/collections/src/java/org/apache/commons/collections/ArrayIterator.java
Index: ArrayIterator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ArrayIterator.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ArrayIterator.java 19 Mar 2002 00:05:10 -0000 1.10
+++ ArrayIterator.java 19 Mar 2002 00:54:34 -0000 1.11
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ArrayIterator.java,v
1.10 2002/03/19 00:05:10 morgand Exp $
- * $Revision: 1.10 $
- * $Date: 2002/03/19 00:05:10 $
+ * $Header:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ArrayIterator.java,v
1.11 2002/03/19 00:54:34 mas Exp $
+ * $Revision: 1.11 $
+ * $Date: 2002/03/19 00:54:34 $
*
* ====================================================================
*
@@ -68,11 +68,13 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
* @author Mauricio S. Moura
- * @version $Revision: 1.10 $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Michael A. Smith</a>
+ * @version $Revision: 1.11 $
*/
public class ArrayIterator implements Iterator {
private Object array;
+ private int length = 0;
private int index = 0;
@@ -80,13 +82,13 @@
}
public ArrayIterator(Object array) {
- this.array = array;
+ setArray( array );
}
// Iterator interface
//-------------------------------------------------------------------------
public boolean hasNext() {
- return index < Array.getLength( array );
+ return index < length;
}
public Object next() {
@@ -106,7 +108,16 @@
return array;
}
+ /**
+ * @exception IllegalArgumentException if <code>array</code> is not an
+ * array.
+ **/
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.
+ this.length = Array.getLength( array );
this.array = array;
this.index = 0;
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>