You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/06/20 04:51:18 UTC
cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestArrayIterator2.java
bayard 2002/06/19 19:51:18
Modified: collections/src/java/org/apache/commons/collections
ArrayIterator.java
collections/src/test/org/apache/commons/collections
TestArrayIterator2.java
Log:
Added a start and end index to the ArrayIterator. Unit tests also added and
currently passing.
Revision Changes Path
1.14 +65 -4 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.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- ArrayIterator.java 12 Jun 2002 03:59:15 -0000 1.13
+++ ArrayIterator.java 20 Jun 2002 02:51:18 -0000 1.14
@@ -103,6 +103,67 @@
setArray( array );
}
+ /**
+ * Construct an ArrayIterator that will iterate over the values in the
+ * specified array.
+ *
+ * @param array the array to iterate over.
+ * @param start the index to start iterating at.
+ *
+ * @exception IllegalArgumentException if <code>array</code> is not an
+ * array.
+ *
+ * @exception NullPointerException
+ * if <code>array</code> is <code>null</code>
+ **/
+ public ArrayIterator(Object array, int start) {
+ setArray( array );
+ checkBound(start, "start");
+ this.index = start;
+ }
+
+ /**
+ * Construct an ArrayIterator that will iterate over the values in the
+ * specified array.
+ *
+ * @param array the array to iterate over.
+ * @param start the index to start iterating at.
+ * @param end the index to finish iterating at.
+ *
+ * @exception IllegalArgumentException if <code>array</code> is not an
+ * array.
+ *
+ * @exception NullPointerException
+ * if <code>array</code> is <code>null</code>
+ **/
+ public ArrayIterator(Object array, int start, int end) {
+ setArray( array );
+ checkBound(start, "start");
+ checkBound(end, "end");
+ if(end <= start) {
+ throw new IllegalArgumentException(
+ "End index must be greater than start index. "
+ );
+ }
+ this.index = start;
+ this.length = end;
+ }
+
+ private void checkBound(int bound, String type ) {
+ if(bound > this.length) {
+ throw new ArrayIndexOutOfBoundsException(
+ "Attempt to make an ArrayIterator that "+type+
+ "s beyond the end of the array. "
+ );
+ }
+ if(bound < 0) {
+ throw new ArrayIndexOutOfBoundsException(
+ "Attempt to make an ArrayIterator that "+type+
+ "s before the start of the array. "
+ );
+ }
+ }
+
// Iterator interface
//-------------------------------------------------------------------------
public boolean hasNext() {
1.4 +74 -4 jakarta-commons/collections/src/test/org/apache/commons/collections/TestArrayIterator2.java
Index: TestArrayIterator2.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestArrayIterator2.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestArrayIterator2.java 19 Mar 2002 00:05:16 -0000 1.3
+++ TestArrayIterator2.java 20 Jun 2002 02:51:18 -0000 1.4
@@ -144,5 +144,75 @@
assertEquals("the count should be right using setArray(Object)",
count2,testArray.length);
}
+
+ public void testIndexedArray() {
+ Iterator iter = new ArrayIterator(testArray,2);
+ int count = 0;
+ while (iter.hasNext()) {
+ ++count;
+ iter.next();
+ }
+
+ assertEquals("the count should be right using ArrayIterator(Object,2) ",
+ count,testArray.length-2);
+
+ iter = new ArrayIterator(testArray,1,testArray.length-1);
+ count = 0;
+ while (iter.hasNext()) {
+ ++count;
+ iter.next();
+ }
+
+ assertEquals("the count should be right using ArrayIterator(Object,1,"+
+ (testArray.length-1)+") ", count, testArray.length-2);
+
+ try {
+ iter = new ArrayIterator(testArray,-1);
+ fail("new ArrayIterator(Object,-1) should throw an "+
+ "ArrayIndexOutOfBoundsException");
+ } catch(ArrayIndexOutOfBoundsException aioobe) {
+ // expected
+ }
+
+ try {
+ iter = new ArrayIterator(testArray,testArray.length+1);
+ fail("new ArrayIterator(Object,length+1) should throw an "+
+ "ArrayIndexOutOfBoundsException");
+ } catch(ArrayIndexOutOfBoundsException aioobe) {
+ // expected
+ }
+
+ try {
+ iter = new ArrayIterator(testArray,0,-1);
+ fail("new ArrayIterator(Object,0,-1) should throw an "+
+ "ArrayIndexOutOfBoundsException");
+ } catch(ArrayIndexOutOfBoundsException aioobe) {
+ // expected
+ }
+
+ try {
+ iter = new ArrayIterator(testArray,0,testArray.length+1);
+ fail("new ArrayIterator(Object,0,length+1) should throw an "+
+ "ArrayIndexOutOfBoundsException");
+ } catch(ArrayIndexOutOfBoundsException aioobe) {
+ // expected
+ }
+
+ try {
+ iter = new ArrayIterator(testArray,1,1);
+ fail("new ArrayIterator(Object,1,1) should throw an "+
+ "IllegalArgumentException");
+ } catch(IllegalArgumentException iae) {
+ // expected
+ }
+
+ try {
+ iter = new ArrayIterator(testArray,testArray.length-1,testArray.length-2);
+ fail("new ArrayIterator(Object,length-2,length-1) should throw an "+
+ "IllegalArgumentException");
+ } catch(IllegalArgumentException iae) {
+ // expected
+ }
+ }
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>