View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jexl2.internal;
19  
20  import java.util.Iterator;
21  import java.util.NoSuchElementException;
22  import java.lang.reflect.Array;
23  
24  /**
25   *  <p>
26   *  An Iterator wrapper for an Object[]. This will
27   *  allow us to deal with all array like structures
28   *  in a consistent manner.
29   *  </p>
30   *  <p>
31   *  WARNING : this class's operations are NOT synchronized.
32   *  It is meant to be used in a single thread, newly created
33   *  for each use in the #foreach() directive.
34   *  If this is used or shared, synchronize in the
35   *  next() method.
36   *  </p>
37   *
38   * @since 1.0
39   */
40  public class ArrayIterator implements Iterator<Object> {
41      /** The objects to iterate over. */
42      private final Object array;
43      /** The size of the array. */
44      private final int size;
45      /** The current position and size in the array. */
46      private int pos;
47  
48      /**
49       * Creates a new iterator instance for the specified array.
50       * @param arr The array for which an iterator is desired.
51       */
52      public ArrayIterator(Object arr) {
53          if (arr == null) {
54              array = null;
55              pos = 0;
56              size = 0;
57          } else if (!arr.getClass().isArray()) {
58              throw new IllegalArgumentException(arr.getClass() + " is not an array");
59          } else {
60              array = arr;
61              pos = 0;
62              size = Array.getLength(array);
63          }
64      }
65  
66      /**
67       * Move to next element in the array.
68       *
69       * @return The next object in the array.
70       */
71      public Object next() {
72          if (pos < size) {
73              return Array.get(array, pos++);
74          }    
75          // we screwed up...
76          throw new NoSuchElementException("No more elements: " + pos
77                                           + " / " + size);
78      }
79      
80      /**
81       * Check to see if there is another element in the array.
82       *
83       * @return Whether there is another element.
84       */
85      public boolean hasNext() {
86          return (pos < size);
87      }
88  
89      /**
90       * No op--merely added to satify the <code>Iterator</code> interface.
91       */
92      public void remove() {
93          throw new UnsupportedOperationException();
94      }
95  }