Added a start and end index to the ArrayIterator. Unit tests also added and

currently passing.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130729 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2002-06-20 02:51:18 +00:00
parent 486972dbc7
commit f405dbaea6
2 changed files with 139 additions and 8 deletions

View File

@ -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.13 2002/06/12 03:59:15 mas Exp $
* $Revision: 1.13 $
* $Date: 2002/06/12 03:59:15 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/ArrayIterator.java,v 1.14 2002/06/20 02:51:18 bayard Exp $
* $Revision: 1.14 $
* $Date: 2002/06/20 02:51:18 $
*
* ====================================================================
*
@ -70,7 +70,7 @@ import java.util.NoSuchElementException;
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author Mauricio S. Moura
* @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
* @version $Revision: 1.13 $
* @version $Revision: 1.14 $
*/
public class ArrayIterator implements Iterator {
@ -103,6 +103,67 @@ public class ArrayIterator implements Iterator {
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() {

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestArrayIterator2.java,v 1.3 2002/03/19 00:05:16 morgand Exp $
* $Revision: 1.3 $
* $Date: 2002/03/19 00:05:16 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestArrayIterator2.java,v 1.4 2002/06/20 02:51:18 bayard Exp $
* $Revision: 1.4 $
* $Date: 2002/06/20 02:51:18 $
*
* ====================================================================
*
@ -70,7 +70,7 @@ import java.util.NoSuchElementException;
*
* @author Morgan Delagrange
* @author James Strachan
* @version $Id: TestArrayIterator2.java,v 1.3 2002/03/19 00:05:16 morgand Exp $
* @version $Id: TestArrayIterator2.java,v 1.4 2002/06/20 02:51:18 bayard Exp $
*/
public class TestArrayIterator2 extends TestIterator {
@ -144,5 +144,75 @@ public class TestArrayIterator2 extends TestIterator {
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
}
}
}