commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestIteratorUtils.java
Date Mon, 29 Sep 2003 03:56:12 GMT
psteitz     2003/09/28 20:56:12

  Modified:    collections/src/java/org/apache/commons/collections/iterators
                        ObjectArrayIterator.java
                        ObjectArrayListIterator.java ArrayListIterator.java
               collections/src/test/org/apache/commons/collections
                        TestIteratorUtils.java
  Log:
  Fixed previousIndex() and nextIndex() methods in ArrayListIterator and ObjectArrayListIterator
to conform to ListIterator interface specification.
  Modified ObjectArrayIterator constructor to throw ArrayOutOfBoundsException when start index
is out of range (as advertised).
  Added test cases to TestIteratorUtils.
  
  Revision  Changes    Path
  1.7       +7 -3      jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ObjectArrayIterator.java
  
  Index: ObjectArrayIterator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ObjectArrayIterator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ObjectArrayIterator.java	31 Aug 2003 17:25:49 -0000	1.6
  +++ ObjectArrayIterator.java	29 Sep 2003 03:56:12 -0000	1.7
  @@ -60,7 +60,7 @@
   import java.util.NoSuchElementException;
   
   /** 
  - * An {@link Iterator Iterator} over an array of objects.
  + * An {@link Iterator} over an array of objects.
    * <p>
    * This iterator does not support {@link #remove}, as the object array cannot be
    * structurally modified.
  @@ -76,6 +76,7 @@
    * @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
    * @author <a href="mailto:neilotoole@users.sourceforge.net">Neil O'Toole</a>
    * @author Stephen Colebourne
  + * @author Phil Steitz
    */
   public class ObjectArrayIterator implements ResetableIterator {
   
  @@ -140,6 +141,9 @@
           }
           if (end > array.length) {
               throw new ArrayIndexOutOfBoundsException("End index must not be greater than
the array length");
  +        }
  +        if (start > array.length) {
  +            throw new ArrayIndexOutOfBoundsException("Start index must not be greater than
the array length");
           }
           if (end < start) {
               throw new IllegalArgumentException("End index must not be less than start index");
  
  
  
  1.7       +6 -5      jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ObjectArrayListIterator.java
  
  Index: ObjectArrayListIterator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ObjectArrayListIterator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ObjectArrayListIterator.java	31 Aug 2003 17:25:49 -0000	1.6
  +++ ObjectArrayListIterator.java	29 Sep 2003 03:56:12 -0000	1.7
  @@ -60,7 +60,7 @@
   import java.util.NoSuchElementException;
   
   /**
  - * Implements a {@link ListIterator ListIterator} over an array of objects.
  + * Implements a {@link ListIterator} over an array of objects.
    * <p>
    * This iterator does not support {@link #add} or {@link #remove}, as the object array

    * cannot be structurally modified. The {@link #set} method is supported however.
  @@ -77,6 +77,7 @@
    * 
    * @author <a href="mailto:neilotoole@users.sourceforge.net">Neil O'Toole</a>
    * @author Stephen Colebourne
  + * @author Phil Steitz
    */
   public class ObjectArrayListIterator extends ObjectArrayIterator implements ResetableListIterator
{
   
  @@ -183,7 +184,7 @@
        * @return the index of the item to be retrieved next
        */
       public int nextIndex() {
  -        return this.index;
  +        return this.index - this.startIndex;
       }
   
       /**
  @@ -192,7 +193,7 @@
        * @return the index of the item to be retrieved next
        */
       public int previousIndex() {
  -        return this.index - 1;
  +        return this.index - this.startIndex - 1;
       }
   
       /**
  
  
  
  1.5       +5 -4      jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ArrayListIterator.java
  
  Index: ArrayListIterator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ArrayListIterator.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ArrayListIterator.java	31 Aug 2003 17:25:49 -0000	1.4
  +++ ArrayListIterator.java	29 Sep 2003 03:56:12 -0000	1.5
  @@ -81,6 +81,7 @@
    *
    * @author <a href="mailto:neilotoole@users.sourceforge.net">Neil O'Toole</a>
    * @author Stephen Colebourne
  + * @author Phil Steitz
    */
   public class ArrayListIterator extends ArrayIterator implements ResetableListIterator {
   
  @@ -192,7 +193,7 @@
        * @return the index of the item to be retrieved next
        */
       public int nextIndex() {
  -        return this.index;
  +        return this.index - this.startIndex;
       }
   
       /**
  @@ -201,7 +202,7 @@
        * @return the index of the item to be retrieved next
        */
       public int previousIndex() {
  -        return this.index - 1;
  +        return this.index - this.startIndex - 1;
       }
   
       /**
  
  
  
  1.6       +285 -1    jakarta-commons/collections/src/test/org/apache/commons/collections/TestIteratorUtils.java
  
  Index: TestIteratorUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestIteratorUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestIteratorUtils.java	31 Aug 2003 17:28:43 -0000	1.5
  +++ TestIteratorUtils.java	29 Sep 2003 03:56:12 -0000	1.6
  @@ -62,6 +62,7 @@
   import java.util.Iterator;
   import java.util.List;
   import java.util.ListIterator;
  +import java.util.NoSuchElementException;
   
   import junit.framework.Test;
   
  @@ -110,6 +111,289 @@
           String[] result = (String[]) IteratorUtils.toArray(list.iterator(), String.class);
           assertEquals(list, Arrays.asList(result));
       }
  +    
  +    public void testArrayIterator() {
  +        Object[] objArray = {"a", "b", "c"};
  +        ResetableIterator iterator = IteratorUtils.arrayIterator(objArray);
  +        assertTrue(iterator.next().equals("a"));
  +        assertTrue(iterator.next().equals("b"));
  +        iterator.reset();
  +        assertTrue(iterator.next().equals("a"));
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(new Integer(0));
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException ex) {
  +                // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {
  +                // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayIterator(objArray, 1);
  +        assertTrue(iterator.next().equals("b"));
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(objArray, -1);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayIterator(objArray, 3);
  +        assertTrue(!iterator.hasNext());
  +        iterator.reset();
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(objArray, 4);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayIterator(objArray, 2, 3);
  +        assertTrue(iterator.next().equals("c"));
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(objArray, 2, 4);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(objArray, -1, 1);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(objArray, 2, 1);
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException ex) {
  +            // expected
  +        }
  +        
  +        int[] intArray = {0, 1, 2};
  +        iterator = IteratorUtils.arrayIterator(intArray);
  +        assertTrue(iterator.next().equals(new Integer(0)));
  +        assertTrue(iterator.next().equals(new Integer(1)));
  +        iterator.reset();
  +        assertTrue(iterator.next().equals(new Integer(0)));
  +        
  +        iterator = IteratorUtils.arrayIterator(intArray, 1);
  +        assertTrue(iterator.next().equals(new Integer(1)));
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(intArray, -1);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayIterator(intArray, 3);
  +        assertTrue(!iterator.hasNext());
  +        iterator.reset();
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(intArray, 4);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayIterator(intArray, 2, 3);
  +        assertTrue(iterator.next().equals(new Integer(2)));
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(intArray, 2, 4);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(intArray, -1, 1);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayIterator(intArray, 2, 1);
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException ex) {
  +            // expected
  +        }          
  +    }
  +    
  +    public void testArrayListIterator() {
  +        Object[] objArray = {"a", "b", "c", "d"};
  +        ResetableListIterator iterator = IteratorUtils.arrayListIterator(objArray);
  +        assertTrue(!iterator.hasPrevious());
  +        assertTrue(iterator.previousIndex() == -1);
  +        assertTrue(iterator.nextIndex() == 0);
  +        assertTrue(iterator.next().equals("a"));
  +        assertTrue(iterator.previous().equals("a"));
  +        assertTrue(iterator.next().equals("a"));
  +        assertTrue(iterator.previousIndex() == 0);
  +        assertTrue(iterator.nextIndex() == 1);
  +        assertTrue(iterator.next().equals("b"));
  +        assertTrue(iterator.next().equals("c"));
  +        assertTrue(iterator.next().equals("d"));
  +        assertTrue(iterator.nextIndex() == 4); // size of list
  +        assertTrue(iterator.previousIndex() == 3);
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(new Integer(0));
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException ex) {
  +                // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {
  +                // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayListIterator(objArray, 1);
  +        assertTrue(iterator.previousIndex() == -1); 
  +        assertTrue(!iterator.hasPrevious());
  +        assertTrue(iterator.nextIndex() == 0); 
  +        assertTrue(iterator.next().equals("b"));
  +        assertTrue(iterator.previousIndex() == 0);        
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(objArray, -1);
  +            fail("Expecting IndexOutOfBoundsException.");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayListIterator(objArray, 3);
  +        assertTrue(iterator.hasNext());
  +        try {
  +            Object x = iterator.previous();
  +            fail("Expecting NoSuchElementException.");
  +        } catch (NoSuchElementException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(objArray, 5);
  +            fail("Expecting IndexOutOfBoundsException.");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayListIterator(objArray, 2, 3);
  +        assertTrue(iterator.next().equals("c"));
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(objArray, 2, 5);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(objArray, -1, 1);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(objArray, 2, 1);
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException ex) {
  +            // expected
  +        }
  +        
  +        int[] intArray = {0, 1, 2};
  +        iterator = IteratorUtils.arrayListIterator(intArray);
  +        assertTrue(iterator.previousIndex() == -1); 
  +        assertTrue(!iterator.hasPrevious());
  +        assertTrue(iterator.nextIndex() == 0); 
  +        assertTrue(iterator.next().equals(new Integer(0)));
  +        assertTrue(iterator.previousIndex() == 0); 
  +        assertTrue(iterator.nextIndex() == 1); 
  +        assertTrue(iterator.next().equals(new Integer(1)));
  +        assertTrue(iterator.previousIndex() == 1); 
  +        assertTrue(iterator.nextIndex() == 2); 
  +        assertTrue(iterator.previous().equals(new Integer(1)));
  +        assertTrue(iterator.next().equals(new Integer(1)));
  +        
  +        iterator = IteratorUtils.arrayListIterator(intArray, 1);
  +        assertTrue(iterator.previousIndex() == -1); 
  +        assertTrue(!iterator.hasPrevious());
  +        assertTrue(iterator.nextIndex() == 0); 
  +        assertTrue(iterator.next().equals(new Integer(1)));
  +        assertTrue(iterator.previous().equals(new Integer(1)));
  +        assertTrue(iterator.next().equals(new Integer(1)));
  +        assertTrue(iterator.previousIndex() == 0); 
  +        assertTrue(iterator.nextIndex() == 1); 
  +        assertTrue(iterator.next().equals(new Integer(2)));
  +        assertTrue(iterator.previousIndex() == 1); 
  +        assertTrue(iterator.nextIndex() == 2); 
  +        assertTrue(iterator.previous().equals(new Integer(2)));
  +        assertTrue(iterator.previousIndex() == 0); 
  +        assertTrue(iterator.nextIndex() == 1); 
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(intArray, -1);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayListIterator(intArray, 3);
  +        assertTrue(!iterator.hasNext());
  +     
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(intArray, 4);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        iterator = IteratorUtils.arrayListIterator(intArray, 2, 3);
  +        assertTrue(!iterator.hasPrevious());
  +        assertTrue(iterator.previousIndex() == -1);
  +        assertTrue(iterator.next().equals(new Integer(2)));
  +        assertTrue(iterator.hasPrevious());
  +        assertTrue(!iterator.hasNext());
  +        
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(intArray, 2, 4);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(intArray, -1, 1);
  +            fail("Expecting IndexOutOfBoundsException");
  +        } catch (IndexOutOfBoundsException ex) {
  +            // expected
  +        }
  +        
  +        try {
  +            iterator = IteratorUtils.arrayListIterator(intArray, 2, 1);
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException ex) {
  +            // expected
  +        }          
  +    }
  +        
   
       /**
        * Gets an immutable Iterator operating on the elements ["a", "b", "c", "d"].
  
  
  

Mime
View raw message