001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.iterators;
018
019import java.lang.reflect.Array;
020import java.util.NoSuchElementException;
021
022import org.apache.commons.collections4.ResettableIterator;
023
024/**
025 * Implements an {@link java.util.Iterator Iterator} over any array.
026 * <p>
027 * The array can be either an array of object or of primitives. If you know
028 * that you have an object array, the
029 * {@link org.apache.commons.collections4.iterators.ObjectArrayIterator ObjectArrayIterator}
030 * class is a better choice, as it will perform better.
031 * <p>
032 * The iterator implements a {@link #reset} method, allowing the reset of
033 * the iterator back to the start if required.
034 *
035 * @since 1.0
036 * @version $Id: ArrayIterator.html 972421 2015-11-14 20:00:04Z tn $
037 */
038public class ArrayIterator<E> implements ResettableIterator<E> {
039
040    /** The array to iterate over */
041    final Object array;
042    /** The start index to loop from */
043    final int startIndex;
044    /** The end index to loop to */
045    final int endIndex;
046    /** The current iterator index */
047    int index = 0;
048
049    // Constructors
050    // ----------------------------------------------------------------------
051    /**
052     * Constructs an ArrayIterator that will iterate over the values in the
053     * specified array.
054     *
055     * @param array the array to iterate over.
056     * @throws IllegalArgumentException if <code>array</code> is not an array.
057     * @throws NullPointerException if <code>array</code> is <code>null</code>
058     */
059    public ArrayIterator(final Object array) {
060        this(array, 0);
061    }
062
063    /**
064     * Constructs an ArrayIterator that will iterate over the values in the
065     * specified array from a specific start index.
066     *
067     * @param array  the array to iterate over.
068     * @param startIndex  the index to start iterating at.
069     * @throws IllegalArgumentException if <code>array</code> is not an array.
070     * @throws NullPointerException if <code>array</code> is <code>null</code>
071     * @throws IndexOutOfBoundsException if the index is invalid
072     */
073    public ArrayIterator(final Object array, final int startIndex) {
074        this(array, startIndex, Array.getLength(array));
075    }
076
077    /**
078     * Construct an ArrayIterator that will iterate over a range of values
079     * in the specified array.
080     *
081     * @param array  the array to iterate over.
082     * @param startIndex  the index to start iterating at.
083     * @param endIndex  the index to finish iterating at.
084     * @throws IllegalArgumentException if <code>array</code> is not an array.
085     * @throws NullPointerException if <code>array</code> is <code>null</code>
086     * @throws IndexOutOfBoundsException if either index is invalid
087     */
088    public ArrayIterator(final Object array, final int startIndex, final int endIndex) {
089        super();
090
091        this.array = array;
092        this.startIndex = startIndex;
093        this.endIndex = endIndex;
094        this.index = startIndex;
095
096        final int len = Array.getLength(array);
097        checkBound(startIndex, len, "start");
098        checkBound(endIndex, len, "end");
099        if (endIndex < startIndex) {
100            throw new IllegalArgumentException("End index must not be less than start index.");
101        }
102    }
103
104    /**
105     * Checks whether the index is valid or not.
106     *
107     * @param bound  the index to check
108     * @param len  the length of the array
109     * @param type  the index type (for error messages)
110     * @throws IndexOutOfBoundsException if the index is invalid
111     */
112    protected void checkBound(final int bound, final int len, final String type ) {
113        if (bound > len) {
114            throw new ArrayIndexOutOfBoundsException(
115              "Attempt to make an ArrayIterator that " + type +
116              "s beyond the end of the array. "
117            );
118        }
119        if (bound < 0) {
120            throw new ArrayIndexOutOfBoundsException(
121              "Attempt to make an ArrayIterator that " + type +
122              "s before the start of the array. "
123            );
124        }
125    }
126
127    // Iterator interface
128    //-----------------------------------------------------------------------
129    /**
130     * Returns true if there are more elements to return from the array.
131     *
132     * @return true if there is a next element to return
133     */
134    public boolean hasNext() {
135        return index < endIndex;
136    }
137
138    /**
139     * Returns the next element in the array.
140     *
141     * @return the next element in the array
142     * @throws NoSuchElementException if all the elements in the array
143     *  have already been returned
144     */
145    @SuppressWarnings("unchecked")
146    public E next() {
147        if (hasNext() == false) {
148            throw new NoSuchElementException();
149        }
150        return (E) Array.get(array, index++);
151    }
152
153    /**
154     * Throws {@link UnsupportedOperationException}.
155     *
156     * @throws UnsupportedOperationException always
157     */
158    public void remove() {
159        throw new UnsupportedOperationException("remove() method is not supported");
160    }
161
162    // Properties
163    //-----------------------------------------------------------------------
164    /**
165     * Gets the array that this iterator is iterating over.
166     *
167     * @return the array this iterator iterates over.
168     */
169    public Object getArray() {
170        return array;
171    }
172
173    /**
174     * Gets the start index to loop from.
175     *
176     * @return the start index
177     * @since 4.0
178     */
179    public int getStartIndex() {
180        return this.startIndex;
181    }
182
183    /**
184     * Gets the end index to loop to.
185     *
186     * @return the end index
187     * @since 4.0
188     */
189    public int getEndIndex() {
190        return this.endIndex;
191    }
192
193    /**
194     * Resets the iterator back to the start index.
195     */
196    public void reset() {
197        this.index = this.startIndex;
198    }
199
200}