ObjectArrayIterator.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.util.NoSuchElementException;

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

  20. /**
  21.  * An {@link java.util.Iterator Iterator} over an array of objects.
  22.  * <p>
  23.  * This iterator does not support {@link #remove}, as the object array cannot be
  24.  * structurally modified.
  25.  * </p>
  26.  * <p>
  27.  * The iterator implements a {@link #reset} method, allowing the reset of the iterator
  28.  * back to the start if required.
  29.  * </p>
  30.  *
  31.  * @param <E> the type of elements returned by this iterator.
  32.  * @since 3.0
  33.  */
  34. public class ObjectArrayIterator<E> implements ResettableIterator<E> {

  35.     /** The array */
  36.     final E[] array;
  37.     /** The start index to loop from */
  38.     final int startIndex;
  39.     /** The end index to loop to */
  40.     final int endIndex;
  41.     /** The current iterator index */
  42.     int index;

  43.     /**
  44.      * Constructs an ObjectArrayIterator that will iterate over the values in the
  45.      * specified array.
  46.      *
  47.      * @param array the array to iterate over
  48.      * @throws NullPointerException if {@code array} is {@code null}
  49.      */
  50.     public ObjectArrayIterator(final E... array) {
  51.         this(array, 0, array.length);
  52.     }

  53.     /**
  54.      * Constructs an ObjectArrayIterator that will iterate over the values in the
  55.      * specified array from a specific start index.
  56.      *
  57.      * @param array  the array to iterate over
  58.      * @param start  the index to start iterating at
  59.      * @throws NullPointerException if {@code array} is {@code null}
  60.      * @throws IndexOutOfBoundsException if the start index is out of bounds
  61.      */
  62.     public ObjectArrayIterator(final E[] array, final int start) {
  63.         this(array, start, array.length);
  64.     }

  65.     /**
  66.      * Constructs an ObjectArrayIterator that will iterate over a range of values
  67.      * in the specified array.
  68.      *
  69.      * @param array  the array to iterate over
  70.      * @param start  the index to start iterating at
  71.      * @param end  the index (exclusive) to finish iterating at
  72.      * @throws IndexOutOfBoundsException if the start or end index is out of bounds
  73.      * @throws IllegalArgumentException if end index is before the start
  74.      * @throws NullPointerException if {@code array} is {@code null}
  75.      */
  76.     public ObjectArrayIterator(final E[] array, final int start, final int end) {
  77.         if (start < 0) {
  78.             throw new ArrayIndexOutOfBoundsException("Start index must not be less than zero");
  79.         }
  80.         if (end > array.length) {
  81.             throw new ArrayIndexOutOfBoundsException("End index must not be greater than the array length");
  82.         }
  83.         if (start > array.length) {
  84.             throw new ArrayIndexOutOfBoundsException("Start index must not be greater than the array length");
  85.         }
  86.         if (end < start) {
  87.             throw new IllegalArgumentException("End index must not be less than start index");
  88.         }
  89.         this.array = array;
  90.         startIndex = start;
  91.         endIndex = end;
  92.         index = start;
  93.     }

  94.     /**
  95.      * Gets the array that this iterator is iterating over.
  96.      *
  97.      * @return the array this iterator iterates over
  98.      */
  99.     public E[] getArray() {
  100.         return array;
  101.     }

  102.     /**
  103.      * Gets the end index to loop to.
  104.      *
  105.      * @return the end index
  106.      */
  107.     public int getEndIndex() {
  108.         return endIndex;
  109.     }

  110.     /**
  111.      * Gets the start index to loop from.
  112.      *
  113.      * @return the start index
  114.      */
  115.     public int getStartIndex() {
  116.         return startIndex;
  117.     }

  118.     /**
  119.      * Returns true if there are more elements to return from the array.
  120.      *
  121.      * @return true if there is a next element to return
  122.      */
  123.     @Override
  124.     public boolean hasNext() {
  125.         return index < endIndex;
  126.     }

  127.     /**
  128.      * Returns the next element in the array.
  129.      *
  130.      * @return the next element in the array
  131.      * @throws NoSuchElementException if all the elements in the array
  132.      *    have already been returned
  133.      */
  134.     @Override
  135.     public E next() {
  136.         if (!hasNext()) {
  137.             throw new NoSuchElementException();
  138.         }
  139.         return array[index++];
  140.     }

  141.     /**
  142.      * Throws {@link UnsupportedOperationException}.
  143.      *
  144.      * @throws UnsupportedOperationException always
  145.      */
  146.     @Override
  147.     public void remove() {
  148.         throw new UnsupportedOperationException("remove() method is not supported for an ObjectArrayIterator");
  149.     }

  150.     /**
  151.      * Resets the iterator back to the start index.
  152.      */
  153.     @Override
  154.     public void reset() {
  155.         index = startIndex;
  156.     }

  157. }