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

  20. /**
  21.  * Implements a {@link java.util.ListIterator} over an array of objects.
  22.  * <p>
  23.  * This iterator does not support {@link #add} or {@link #remove}, as the object array
  24.  * cannot be structurally modified. The {@link #set} method is supported however.
  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.  * @see org.apache.commons.collections4.iterators.ObjectArrayIterator
  33.  * @see java.util.Iterator
  34.  * @see java.util.ListIterator
  35.  * @since 3.0
  36.  */
  37. public class ObjectArrayListIterator<E> extends ObjectArrayIterator<E>
  38.         implements ResettableListIterator<E> {

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

  46.     /**
  47.      * Constructs an ObjectArrayListIterator that will iterate over the values in the
  48.      * specified array.
  49.      *
  50.      * @param array the array to iterate over
  51.      * @throws NullPointerException if {@code array} is {@code null}
  52.      */
  53.     public ObjectArrayListIterator(final E... array) {
  54.         super(array);
  55.     }

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

  68.     /**
  69.      * Constructs an ObjectArrayListIterator that will iterate over a range of values
  70.      * in the specified array.
  71.      *
  72.      * @param array  the array to iterate over
  73.      * @param start  the index to start iterating at
  74.      * @param end  the index (exclusive) to finish iterating at
  75.      * @throws IndexOutOfBoundsException if the start or end index is out of bounds
  76.      * @throws IllegalArgumentException if end index is before the start
  77.      * @throws NullPointerException if {@code array} is {@code null}
  78.      */
  79.     public ObjectArrayListIterator(final E[] array, final int start, final int end) {
  80.         super(array, start, end);
  81.     }

  82.     /**
  83.      * This iterator does not support modification of its backing array's size, and so will
  84.      * always throw an {@link UnsupportedOperationException} when this method is invoked.
  85.      *
  86.      * @param obj  the object to add
  87.      * @throws UnsupportedOperationException always thrown.
  88.      */
  89.     @Override
  90.     public void add(final E obj) {
  91.         throw new UnsupportedOperationException("add() method is not supported");
  92.     }

  93.     /**
  94.      * Returns true if there are previous elements to return from the array.
  95.      *
  96.      * @return true if there is a previous element to return
  97.      */
  98.     @Override
  99.     public boolean hasPrevious() {
  100.         return index > getStartIndex();
  101.     }

  102.     /**
  103.      * Gets the next element from the array.
  104.      *
  105.      * @return the next element
  106.      * @throws NoSuchElementException if there is no next element
  107.      */
  108.     @Override
  109.     public E next() {
  110.         if (!hasNext()) {
  111.             throw new NoSuchElementException();
  112.         }
  113.         lastItemIndex = index;
  114.         return array[index++];
  115.     }

  116.     /**
  117.      * Gets the next index to be retrieved.
  118.      *
  119.      * @return the index of the item to be retrieved next
  120.      */
  121.     @Override
  122.     public int nextIndex() {
  123.         return index - getStartIndex();
  124.     }

  125.     /**
  126.      * Gets the previous element from the array.
  127.      *
  128.      * @return the previous element
  129.      * @throws NoSuchElementException if there is no previous element
  130.      */
  131.     @Override
  132.     public E previous() {
  133.         if (!hasPrevious()) {
  134.             throw new NoSuchElementException();
  135.         }
  136.         lastItemIndex = --index;
  137.         return array[index];
  138.     }

  139.     /**
  140.      * Gets the index of the item to be retrieved if {@link #previous()} is called.
  141.      *
  142.      * @return the index of the item to be retrieved next
  143.      */
  144.     @Override
  145.     public int previousIndex() {
  146.         return index - getStartIndex() - 1;
  147.     }

  148.     /**
  149.      * Resets the iterator back to the start index.
  150.      */
  151.     @Override
  152.     public void reset() {
  153.         super.reset();
  154.         lastItemIndex = -1;
  155.     }

  156.     /**
  157.      * Sets the element under the cursor.
  158.      * <p>
  159.      * This method sets the element that was returned by the last call
  160.      * to {@link #next()} of {@link #previous()}.
  161.      * </p>
  162.      * <p>
  163.      * <strong>Note:</strong> {@link java.util.ListIterator} implementations that support {@code add()}
  164.      * and {@code remove()} only allow {@code set()} to be called once per call
  165.      * to {@code next()} or {@code previous} (see the {@link java.util.ListIterator}
  166.      * Javadoc for more details). Since this implementation does not support
  167.      * {@code add()} or {@code remove()}, {@code set()} may be
  168.      * called as often as desired.
  169.      * </p>
  170.      *
  171.      * @param obj  the object to set into the array
  172.      * @throws IllegalStateException if next() has not yet been called.
  173.      * @throws ClassCastException if the object type is unsuitable for the array
  174.      */
  175.     @Override
  176.     public void set(final E obj) {
  177.         if (lastItemIndex == -1) {
  178.             throw new IllegalStateException("must call next() or previous() before a call to set()");
  179.         }
  180.         array[lastItemIndex] = obj;
  181.     }

  182. }