001: /*
002: * (C) Copyright 2002 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.util;
022:
023: import java.lang.reflect.Array;
024: import java.util.Iterator;
025: import java.util.NoSuchElementException;
026:
027: /**
028: *
029: *
030: * @author Padmanabh Dabke
031: * (c) 2002 Nabh Information Systems, Inc. All Rights Reserved.
032: */
033: public class ArrayIterator implements Iterator {
034:
035: private Object array;
036: private int length = 0;
037: private int index = 0;
038:
039: /**
040: * Construct an ArrayIterator. Using this constructor, the iterator is
041: * equivalent to an empty iterator until {@link #setArray(Object)} is
042: * called to establish the array to iterate over.
043: **/
044: public ArrayIterator() {
045: }
046:
047: /**
048: * Construct an ArrayIterator that will iterate over the values in the
049: * specified array.
050: *
051: * @param array the array to iterate over.
052: *
053: * @exception IllegalArgumentException if <code>array</code> is not an
054: * array.
055: *
056: * @exception NullPointerException
057: * if <code>array</code> is <code>null</code>
058: **/
059: public ArrayIterator(Object array) {
060: setArray(array);
061: }
062:
063: /**
064: * Construct an ArrayIterator that will iterate over the values in the
065: * specified array.
066: *
067: * @param array the array to iterate over.
068: * @param start the index to start iterating at.
069: *
070: * @exception IllegalArgumentException if <code>array</code> is not an
071: * array.
072: *
073: * @exception NullPointerException
074: * if <code>array</code> is <code>null</code>
075: **/
076: public ArrayIterator(Object array, int start) {
077: setArray(array);
078: checkBound(start, "start");
079: this .index = start;
080: }
081:
082: /**
083: * Construct an ArrayIterator that will iterate over the values in the
084: * specified array.
085: *
086: * @param array the array to iterate over.
087: * @param start the index to start iterating at.
088: * @param end the index to finish iterating at.
089: *
090: * @exception IllegalArgumentException if <code>array</code> is not an
091: * array.
092: *
093: * @exception NullPointerException
094: * if <code>array</code> is <code>null</code>
095: **/
096: public ArrayIterator(Object array, int start, int end) {
097: setArray(array);
098: checkBound(start, "start");
099: checkBound(end, "end");
100: if (end <= start) {
101: throw new IllegalArgumentException(
102: "End index must be greater than start index. ");
103: }
104: this .index = start;
105: this .length = end;
106: }
107:
108: private void checkBound(int bound, String type) {
109: if (bound > this .length) {
110: throw new ArrayIndexOutOfBoundsException(
111: "Attempt to make an ArrayIterator that " + type
112: + "s beyond the end of the array. ");
113: }
114: if (bound < 0) {
115: throw new ArrayIndexOutOfBoundsException(
116: "Attempt to make an ArrayIterator that " + type
117: + "s before the start of the array. ");
118: }
119: }
120:
121: // Iterator interface
122: //-------------------------------------------------------------------------
123:
124: /**
125: * Returns true if there are more elements to return from the array.
126: *
127: * @return true if there is a next element to return
128: */
129: public boolean hasNext() {
130: return index < length;
131: }
132:
133: /**
134: * Returns the next element in the array.
135: *
136: * @return the next element in the array
137: * @throws NoSuchElementException if all the elements in the array
138: * have already been returned
139: */
140: public Object next() {
141: if (!hasNext()) {
142: throw new NoSuchElementException();
143: }
144: return Array.get(array, index++);
145: }
146:
147: /**
148: * Throws {@link UnsupportedOperationException}.
149: *
150: * @throws UnsupportedOperationException always
151: */
152: public void remove() {
153: throw new UnsupportedOperationException(
154: "remove() method is not supported");
155: }
156:
157: // Properties
158: //-------------------------------------------------------------------------
159:
160: /**
161: * Retrieves the array that this iterator is iterating over.
162: *
163: * @return the array this iterator iterates over, or <code>null</code> if
164: * the no-arg constructor was used and {@link #setArray(Object)} has never
165: * been called with a valid array.
166: **/
167: public Object getArray() {
168: return array;
169: }
170:
171: /**
172: * Changes the array that the ArrayIterator should iterate over. If an
173: * array has previously been set (using the single-arg constructor or this
174: * method), that array along with the current iterator position within
175: * that array is discarded in favor of the argument to this method. This
176: * method can be used in combination with {@link #getArray()} to "reset"
177: * the iterator to the beginning of the array:
178: *
179: * <pre>
180: * ArrayIterator iterator = ...
181: * ...
182: * iterator.setArray(iterator.getArray());
183: * </pre>
184: *
185: * Note: Using i.setArray(i.getArray()) may throw a NullPointerException
186: * if no array has ever been set for the iterator (see {@link
187: * #getArray()})
188: *
189: * @param array the array that the iterator should iterate over.
190: *
191: * @exception IllegalArgumentException if <code>array</code> is not an
192: * array.
193: *
194: * @exception NullPointerException
195: * if <code>array</code> is <code>null</code>
196: **/
197: public void setArray(Object array) {
198: // Array.getLength throws IllegalArgumentException if the object is not
199: // an array or NullPointerException if the object is null. This call
200: // is made before saving the array and resetting the index so that the
201: // array iterator remains in a consistent state if the argument is not
202: // an array or is null.
203: this .length = Array.getLength(array);
204: this .array = array;
205: this .index = 0;
206: }
207:
208: }
|