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.jexl3.internal.introspection;
19  
20  import java.lang.reflect.Array;
21  import java.util.Iterator;
22  import java.util.NoSuchElementException;
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(final 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       * Check to see if there is another element in the array.
68       *
69       * @return Whether there is another element.
70       */
71      @Override
72      public boolean hasNext() {
73          return pos < size;
74      }
75  
76      /**
77       * Move to next element in the array.
78       *
79       * @return The next object in the array.
80       */
81      @Override
82      public Object next() {
83          if (pos < size) {
84              return Array.get(array, pos++);
85          }
86          // we screwed up...
87          throw new NoSuchElementException("No more elements: " + pos
88                                           + " / " + size);
89      }
90  }