From f405dbaea6b68e5954a750ec0c2b144a2d478df8 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 20 Jun 2002 02:51:18 +0000 Subject: [PATCH] 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 --- .../commons/collections/ArrayIterator.java | 69 +++++++++++++++- .../collections/TestArrayIterator2.java | 78 ++++++++++++++++++- 2 files changed, 139 insertions(+), 8 deletions(-) diff --git a/src/java/org/apache/commons/collections/ArrayIterator.java b/src/java/org/apache/commons/collections/ArrayIterator.java index ef14ccaa1..89e72ad95 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.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 James Strachan * @author Mauricio S. Moura * @author Michael A. Smith - * @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 array is not an + * array. + * + * @exception NullPointerException + * if array is null + **/ + 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 array is not an + * array. + * + * @exception NullPointerException + * if array is null + **/ + 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() { diff --git a/src/test/org/apache/commons/collections/TestArrayIterator2.java b/src/test/org/apache/commons/collections/TestArrayIterator2.java index 82dfbd898..67a9d2306 100644 --- a/src/test/org/apache/commons/collections/TestArrayIterator2.java +++ b/src/test/org/apache/commons/collections/TestArrayIterator2.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/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 + } + } }