001: /*
002: * Copyright 1999-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jexl.util;
018:
019: import java.util.Iterator;
020: import java.util.NoSuchElementException;
021: import java.lang.reflect.Array;
022:
023: /**
024: * <p>
025: * An Iterator wrapper for an Object[]. This will
026: * allow us to deal with all array like structures
027: * in a consistent manner.
028: * </p>
029: * <p>
030: * WARNING : this class's operations are NOT synchronized.
031: * It is meant to be used in a single thread, newly created
032: * for each use in the #foreach() directive.
033: * If this is used or shared, synchronize in the
034: * next() method.
035: * </p>
036: *
037: * @since 1.0
038: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
039: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
040: * @version $Id: ArrayIterator.java 398329 2006-04-30 12:51:43Z dion $
041: */
042: public class ArrayIterator implements Iterator {
043: /**
044: * The objects to iterate over.
045: */
046: private final Object array;
047:
048: /**
049: * The current position and size in the array.
050: */
051: private int pos;
052:
053: /**
054: * The size of the array.
055: */
056: private final int size;
057:
058: /**
059: * Creates a new iterator instance for the specified array.
060: *
061: * @param arr The array for which an iterator is desired.
062: */
063: public ArrayIterator(Object arr) {
064: /*
065: * if this isn't an array, then throw. Note that this is
066: * for internal use - so this should never happen - if it does
067: * we screwed up.
068: */
069:
070: if (!arr.getClass().isArray()) {
071: throw new IllegalArgumentException("Programmer error :"
072: + " internal ArrayIterator invoked w/o array");
073: }
074:
075: array = arr;
076: pos = 0;
077: size = Array.getLength(array);
078: }
079:
080: /**
081: * Move to next element in the array.
082: *
083: * @return The next object in the array.
084: */
085: public Object next() {
086: if (pos < size) {
087: return Array.get(array, pos++);
088: }
089:
090: /*
091: * we screwed up...
092: */
093:
094: throw new NoSuchElementException("No more elements: " + pos
095: + " / " + size);
096: }
097:
098: /**
099: * Check to see if there is another element in the array.
100: *
101: * @return Whether there is another element.
102: */
103: public boolean hasNext() {
104: return (pos < size);
105: }
106:
107: /**
108: * No op--merely added to satify the <code>Iterator</code> interface.
109: */
110: public void remove() {
111: throw new UnsupportedOperationException();
112: }
113: }
|