View Javadoc

1   /*
2    * Copyright 1999-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jexl.util;
18  
19  
20  
21  import java.util.Iterator;
22  import java.util.NoSuchElementException;
23  import java.lang.reflect.Array;
24  
25  
26  /***
27   *  <p>
28   *  An Iterator wrapper for an Object[]. This will
29   *  allow us to deal with all array like structures
30   *  in a consistent manner.
31   *  </p>
32   *  <p>
33   *  WARNING : this class's operations are NOT synchronized.
34   *  It is meant to be used in a single thread, newly created
35   *  for each use in the #foreach() directive.
36   *  If this is used or shared, synchronize in the
37   *  next() method.
38   *  </p>
39   *
40   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
41   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
42   * @version $Id: ArrayIterator.java,v 1.3 2004/02/28 13:45:21 yoavs Exp $
43   */
44  public class ArrayIterator implements Iterator
45  {
46      /***
47       * The objects to iterate.
48       */
49      private Object array;
50  
51      /***
52       * The current position and size in the array.
53       */
54      private int pos;
55      private int size;
56  
57      /***
58       * Creates a new iterator instance for the specified array.
59       *
60       * @param array The array for which an iterator is desired.
61       */
62      public ArrayIterator(Object array)
63      {
64          /*
65           * if this isn't an array, then throw.  Note that this is 
66           * for internal use - so this should never happen - if it does
67           *  we screwed up.
68           */
69           
70          if ( !array.getClass().isArray() )
71          {   
72              throw new IllegalArgumentException( 
73                  "Programmer error : internal ArrayIterator invoked w/o array");
74          }
75              
76          this.array = array;
77          pos = 0;
78          size = Array.getLength( this.array );
79      }
80  
81      /***
82       * Move to next element in the array.
83       *
84       * @return The next object in the array.
85       */
86      public Object next()
87      {
88          if (pos < size )
89              return Array.get( array, pos++);
90                  
91          /*
92           *  we screwed up...
93           */
94           
95          throw new NoSuchElementException("No more elements: " + pos +
96                                           " / " + size);
97      }
98      
99      /***
100      * Check to see if there is another element in the array.
101      *
102      * @return Whether there is another element.
103      */
104     public boolean hasNext()
105     {
106         return (pos < size );
107     }
108 
109     /***
110      * No op--merely added to satify the <code>Iterator</code> interface.
111      */
112     public void remove()
113     {
114         throw new UnsupportedOperationException();
115     }
116 }