geirm       01/10/23 20:05:25

  Modified:    src/java/org/apache/velocity/util ArrayIterator.java
  Log:
  Added changes to support primitive arrays as suggested by dale king
  ([EMAIL PROTECTED]) in bug #4370.
  
  There is really no cost to this, it's backwards compatible with adjustment
  in #foreach() and addresses certain problems that people have run into.
  
  Revision  Changes    Path
  1.6       +43 -14    
jakarta-velocity/src/java/org/apache/velocity/util/ArrayIterator.java
  
  Index: ArrayIterator.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/ArrayIterator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ArrayIterator.java        2001/10/22 03:53:27     1.5
  +++ ArrayIterator.java        2001/10/24 03:05:25     1.6
  @@ -59,36 +59,62 @@
   
   import java.util.Iterator;
   import java.util.NoSuchElementException;
  +import java.lang.reflect.Array;
   
  +
   /**
  - * An Iterator wrapper for an Object[]. This will
  - * allow us to deal with all array like structures
  - * in a consistent manner.
  + *  <p>
  + *  An Iterator wrapper for an Object[]. This will
  + *  allow us to deal with all array like structures
  + *  in a consistent manner.
  + *  </p>
  + *  <p>
  + *  WARNING : this class's operations are NOT synchronized.
  + *  It is meant to be used in a single thread, newly created
  + *  for each use in the #foreach() directive.
  + *  If this is used or shared, synchronize in the
  + *  next() method.
  + *  </p>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
  - * @version $Id: ArrayIterator.java,v 1.5 2001/10/22 03:53:27 jon Exp $
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
  + * @version $Id: ArrayIterator.java,v 1.6 2001/10/24 03:05:25 geirm Exp $
    */
   public class ArrayIterator implements Iterator
   {
       /**
        * The objects to iterate.
        */
  -    private Object[] array;
  +    private Object array;
   
       /**
  -     * The current position in the array.
  +     * The current position and size in the array.
        */
       private int pos;
  +    private int size;
   
       /**
        * Creates a new iterator instance for the specified array.
        *
        * @param array The array for which an iterator is desired.
        */
  -    public ArrayIterator(Object[] array)
  +    public ArrayIterator(Object array)
       {
  +        /*
  +         * if this isn't an array, then throw.  Note that this is 
  +         * for internal use - so this should never happen - if it does
  +         *  we screwed up.
  +         */
  +         
  +        if ( !array.getClass().isArray() )
  +        {   
  +            throw new IllegalArgumentException( 
  +                "Programmer error : internal ArrayIterator invoked w/o array");
  +        }
  +            
           this.array = array;
           pos = 0;
  +        size = Array.getLength( this.array );
       }
   
       /**
  @@ -98,13 +124,15 @@
        */
       public Object next()
       {
  -        synchronized (array)
  -        {
  -            if (pos < array.length)
  -                return array[pos++];
  -        }
  +        if (pos < size )
  +            return Array.get( array, pos++);
  +                
  +        /*
  +         *  we screwed up...
  +         */
  +         
           throw new NoSuchElementException("No more elements: " + pos +
  -                                         " / " + array.length);
  +                                         " / " + size);
       }
       
       /**
  @@ -114,7 +142,7 @@
        */
       public boolean hasNext()
       {
  -        return (pos < array.length);
  +        return (pos < size );
       }
   
       /**
  @@ -122,5 +150,6 @@
        */
       public void remove()
       {
  +        throw new UnsupportedOperationException();
       }
   }
  
  
  


Reply via email to