ArrayIterator.java

  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. package org.apache.commons.collections4.iterators;

  18. import java.lang.reflect.Array;
  19. import java.util.NoSuchElementException;

  20. import org.apache.commons.collections4.ResettableIterator;

  21. /**
  22.  * Implements an {@link java.util.Iterator Iterator} over any array.
  23.  * <p>
  24.  * The array can be either an array of object or of primitives. If you know
  25.  * that you have an object array, the
  26.  * {@link org.apache.commons.collections4.iterators.ObjectArrayIterator ObjectArrayIterator}
  27.  * class is a better choice, as it will perform better.
  28.  * </p>
  29.  * <p>
  30.  * The iterator implements a {@link #reset} method, allowing the reset of
  31.  * the iterator back to the start if required.
  32.  * </p>
  33.  *
  34.  * @param <E> the type of elements returned by this iterator.
  35.  * @since 1.0
  36.  */
  37. public class ArrayIterator<E> implements ResettableIterator<E> {

  38.     /** The array to iterate over */
  39.     final Object array;
  40.     /** The start index to loop from */
  41.     final int startIndex;
  42.     /** The end index to loop to */
  43.     final int endIndex;
  44.     /** The current iterator index */
  45.     int index;

  46.     /**
  47.      * Constructs an ArrayIterator that will iterate over the values in the
  48.      * specified array.
  49.      *
  50.      * @param array the array to iterate over.
  51.      * @throws IllegalArgumentException if {@code array} is not an array.
  52.      * @throws NullPointerException if {@code array} is {@code null}
  53.      */
  54.     public ArrayIterator(final Object array) {
  55.         this(array, 0);
  56.     }

  57.     /**
  58.      * Constructs an ArrayIterator that will iterate over the values in the
  59.      * specified array from a specific start index.
  60.      *
  61.      * @param array  the array to iterate over.
  62.      * @param startIndex  the index to start iterating at.
  63.      * @throws IllegalArgumentException if {@code array} is not an array.
  64.      * @throws NullPointerException if {@code array} is {@code null}
  65.      * @throws IndexOutOfBoundsException if the index is invalid
  66.      */
  67.     public ArrayIterator(final Object array, final int startIndex) {
  68.         this(array, startIndex, Array.getLength(array));
  69.     }

  70.     /**
  71.      * Constructs an ArrayIterator that will iterate over a range of values
  72.      * in the specified array.
  73.      *
  74.      * @param array  the array to iterate over.
  75.      * @param startIndex  the index to start iterating at.
  76.      * @param endIndex  the index to finish iterating at.
  77.      * @throws IllegalArgumentException if {@code array} is not an array.
  78.      * @throws NullPointerException if {@code array} is {@code null}
  79.      * @throws IndexOutOfBoundsException if either index is invalid
  80.      */
  81.     public ArrayIterator(final Object array, final int startIndex, final int endIndex) {
  82.         this.array = array;
  83.         this.startIndex = startIndex;
  84.         this.endIndex = endIndex;
  85.         this.index = startIndex;

  86.         final int len = Array.getLength(array);
  87.         checkBound(startIndex, len, "start");
  88.         checkBound(endIndex, len, "end");
  89.         if (endIndex < startIndex) {
  90.             throw new IllegalArgumentException("End index must not be less than start index.");
  91.         }
  92.     }

  93.     /**
  94.      * Checks whether the index is valid or not.
  95.      *
  96.      * @param bound  the index to check
  97.      * @param len  the length of the array
  98.      * @param type  the index type (for error messages)
  99.      * @throws IndexOutOfBoundsException if the index is invalid
  100.      */
  101.     protected void checkBound(final int bound, final int len, final String type) {
  102.         if (bound > len) {
  103.             throw new ArrayIndexOutOfBoundsException("Attempt to make an ArrayIterator that " + type + "s beyond the end of the array. ");
  104.         }
  105.         if (bound < 0) {
  106.             throw new ArrayIndexOutOfBoundsException("Attempt to make an ArrayIterator that " + type + "s before the start of the array. ");
  107.         }
  108.     }

  109.     /**
  110.      * Gets the array that this iterator is iterating over.
  111.      *
  112.      * @return the array this iterator iterates over.
  113.      */
  114.     public Object getArray() {
  115.         return array;
  116.     }

  117.     /**
  118.      * Gets the end index to loop to.
  119.      *
  120.      * @return the end index
  121.      * @since 4.0
  122.      */
  123.     public int getEndIndex() {
  124.         return endIndex;
  125.     }

  126.     /**
  127.      * Gets the start index to loop from.
  128.      *
  129.      * @return the start index
  130.      * @since 4.0
  131.      */
  132.     public int getStartIndex() {
  133.         return startIndex;
  134.     }

  135.     /**
  136.      * Returns true if there are more elements to return from the array.
  137.      *
  138.      * @return true if there is a next element to return
  139.      */
  140.     @Override
  141.     public boolean hasNext() {
  142.         return index < endIndex;
  143.     }

  144.     /**
  145.      * Returns the next element in the array.
  146.      *
  147.      * @return the next element in the array
  148.      * @throws NoSuchElementException if all the elements in the array
  149.      *  have already been returned
  150.      */
  151.     @Override
  152.     @SuppressWarnings("unchecked")
  153.     public E next() {
  154.         if (!hasNext()) {
  155.             throw new NoSuchElementException();
  156.         }
  157.         return (E) Array.get(array, index++);
  158.     }

  159.     /**
  160.      * Throws {@link UnsupportedOperationException}.
  161.      *
  162.      * @throws UnsupportedOperationException always
  163.      */
  164.     @Override
  165.     public void remove() {
  166.         throw new UnsupportedOperationException("remove() method is not supported");
  167.     }

  168.     /**
  169.      * Resets the iterator back to the start index.
  170.      */
  171.     @Override
  172.     public void reset() {
  173.         index = startIndex;
  174.     }

  175. }