ArrayListIterator.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.ResettableListIterator;

  21. /**
  22.  * Implements a {@link java.util.ListIterator} over an 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 {@link ObjectArrayListIterator}
  26.  * class is a better choice, as it will perform better.
  27.  * </p>
  28.  * <p>
  29.  * This iterator does not support {@link #add(Object)} or {@link #remove()}, as the array
  30.  * cannot be changed in size. The {@link #set(Object)} method is supported however.
  31.  * </p>
  32.  *
  33.  * @param <E> the type of elements returned by this iterator.
  34.  * @see org.apache.commons.collections4.iterators.ArrayIterator
  35.  * @see java.util.Iterator
  36.  * @see java.util.ListIterator
  37.  * @since 3.0
  38.  */
  39. public class ArrayListIterator<E> extends ArrayIterator<E>
  40.         implements ResettableListIterator<E> {

  41.     /**
  42.      * Holds the index of the last item returned by a call to {@code next()}
  43.      * or {@code previous()}. This is set to {@code -1} if neither method
  44.      * has yet been invoked. {@code lastItemIndex} is used to implement
  45.      * the {@link #set} method.
  46.      */
  47.     private int lastItemIndex = -1;

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

  59.     /**
  60.      * Constructs an ArrayListIterator that will iterate over the values in the
  61.      * specified array from a specific start index.
  62.      *
  63.      * @param array  the array to iterate over
  64.      * @param startIndex  the index to start iterating at
  65.      * @throws IllegalArgumentException if {@code array} is not an array.
  66.      * @throws NullPointerException if {@code array} is {@code null}
  67.      * @throws IndexOutOfBoundsException if the start index is out of bounds
  68.      */
  69.     public ArrayListIterator(final Object array, final int startIndex) {
  70.         super(array, startIndex);
  71.     }

  72.     /**
  73.      * Constructs an ArrayListIterator that will iterate over a range of values
  74.      * in the specified array.
  75.      *
  76.      * @param array  the array to iterate over
  77.      * @param startIndex  the index to start iterating at
  78.      * @param endIndex  the index (exclusive) to finish iterating at
  79.      * @throws IllegalArgumentException if {@code array} is not an array.
  80.      * @throws IndexOutOfBoundsException if the start or end index is out of bounds
  81.      * @throws IllegalArgumentException if end index is before the start
  82.      * @throws NullPointerException if {@code array} is {@code null}
  83.      */
  84.     public ArrayListIterator(final Object array, final int startIndex, final int endIndex) {
  85.         super(array, startIndex, endIndex);
  86.     }

  87.     /**
  88.      * This iterator does not support modification of its backing collection, and so will
  89.      * always throw an {@link UnsupportedOperationException} when this method is invoked.
  90.      *
  91.      * @param o  the element to add
  92.      * @throws UnsupportedOperationException always thrown.
  93.      * @see java.util.ListIterator#set
  94.      */
  95.     @Override
  96.     public void add(final Object o) {
  97.         throw new UnsupportedOperationException("add() method is not supported");
  98.     }

  99.     /**
  100.      * Returns true if there are previous elements to return from the array.
  101.      *
  102.      * @return true if there is a previous element to return
  103.      */
  104.     @Override
  105.     public boolean hasPrevious() {
  106.         return index > startIndex;
  107.     }

  108.     /**
  109.      * Gets the next element from the array.
  110.      *
  111.      * @return the next element
  112.      * @throws NoSuchElementException if there is no next element
  113.      */
  114.     @Override
  115.     @SuppressWarnings("unchecked")
  116.     public E next() {
  117.         if (!hasNext()) {
  118.             throw new NoSuchElementException();
  119.         }
  120.         lastItemIndex = index;
  121.         return (E) Array.get(array, index++);
  122.     }

  123.     /**
  124.      * Gets the next index to be retrieved.
  125.      *
  126.      * @return the index of the item to be retrieved next
  127.      */
  128.     @Override
  129.     public int nextIndex() {
  130.         return index - startIndex;
  131.     }

  132.     /**
  133.      * Gets the previous element from the array.
  134.      *
  135.      * @return the previous element
  136.      * @throws NoSuchElementException if there is no previous element
  137.      */
  138.     @Override
  139.     @SuppressWarnings("unchecked")
  140.     public E previous() {
  141.         if (!hasPrevious()) {
  142.             throw new NoSuchElementException();
  143.         }
  144.         lastItemIndex = --index;
  145.         return (E) Array.get(array, index);
  146.     }

  147.     /**
  148.      * Gets the index of the item to be retrieved if {@link #previous()} is called.
  149.      *
  150.      * @return the index of the item to be retrieved next
  151.      */
  152.     @Override
  153.     public int previousIndex() {
  154.         return index - startIndex - 1;
  155.     }

  156.     /**
  157.      * Resets the iterator back to the start index.
  158.      */
  159.     @Override
  160.     public void reset() {
  161.         super.reset();
  162.         lastItemIndex = -1;
  163.     }

  164.     /**
  165.      * Sets the element under the cursor.
  166.      * <p>
  167.      * This method sets the element that was returned by the last call
  168.      * to {@link #next()} of {@link #previous()}.
  169.      * </p>
  170.      * <p>
  171.      * <strong>Note:</strong> {@link java.util.ListIterator} implementations that support
  172.      * {@code add()} and {@code remove()} only allow {@code set()} to be called
  173.      * once per call to {@code next()} or {@code previous} (see the {@link java.util.ListIterator}
  174.      * Javadoc for more details). Since this implementation does
  175.      * not support {@code add()} or {@code remove()}, {@code set()} may be
  176.      * called as often as desired.
  177.      * </p>
  178.      *
  179.      * @param o  the element to set
  180.      * @throws IllegalStateException if {@link #next()} or {@link #previous()} has not been called
  181.      * before {@link #set(Object)}
  182.      * @see java.util.ListIterator#set
  183.      */
  184.     @Override
  185.     public void set(final Object o) {
  186.         if (lastItemIndex == -1) {
  187.             throw new IllegalStateException("must call next() or previous() before a call to set()");
  188.         }

  189.         Array.set(array, lastItemIndex, o);
  190.     }

  191. }