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.jexl.util;
19
20
21
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
24 import java.lang.reflect.Array;
25
26
27 /**
28 * <p>
29 * An Iterator wrapper for an Object[]. This will
30 * allow us to deal with all array like structures
31 * in a consistent manner.
32 * </p>
33 * <p>
34 * WARNING : this class's operations are NOT synchronized.
35 * It is meant to be used in a single thread, newly created
36 * for each use in the #foreach() directive.
37 * If this is used or shared, synchronize in the
38 * next() method.
39 * </p>
40 *
41 * @since 1.0
42 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
43 * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
44 * @version $Id: ArrayIterator.java 480412 2006-11-29 05:11:23Z bayard $
45 */
46 public class ArrayIterator implements Iterator {
47 /**
48 * The objects to iterate over.
49 */
50 private final Object array;
51
52 /**
53 * The current position and size in the array.
54 */
55 private int pos;
56
57 /**
58 * The size of the array.
59 */
60 private final int size;
61
62 /**
63 * Creates a new iterator instance for the specified array.
64 *
65 * @param arr The array for which an iterator is desired.
66 */
67 public ArrayIterator(Object arr) {
68 /*
69 * if this isn't an array, then throw. Note that this is
70 * for internal use - so this should never happen - if it does
71 * we screwed up.
72 */
73
74 if (!arr.getClass().isArray()) {
75 throw new IllegalArgumentException("Programmer error :"
76 + " internal ArrayIterator invoked w/o array");
77 }
78
79 array = arr;
80 pos = 0;
81 size = Array.getLength(array);
82 }
83
84 /**
85 * Move to next element in the array.
86 *
87 * @return The next object in the array.
88 */
89 public Object next() {
90 if (pos < size) {
91 return Array.get(array, pos++);
92 }
93
94 /*
95 * we screwed up...
96 */
97
98 throw new NoSuchElementException("No more elements: " + pos
99 + " / " + size);
100 }
101
102 /**
103 * Check to see if there is another element in the array.
104 *
105 * @return Whether there is another element.
106 */
107 public boolean hasNext() {
108 return (pos < size);
109 }
110
111 /**
112 * No op--merely added to satify the <code>Iterator</code> interface.
113 */
114 public void remove() {
115 throw new UnsupportedOperationException();
116 }
117 }