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.util.NoSuchElementException;
020
021import org.apache.commons.collections4.ResettableListIterator;
022
023/**
024 * Implements a {@link java.util.ListIterator} over an array of objects.
025 * <p>
026 * This iterator does not support {@link #add} or {@link #remove}, as the object array
027 * cannot be structurally modified. The {@link #set} method is supported however.
028 * <p>
029 * The iterator implements a {@link #reset} method, allowing the reset of the iterator
030 * back to the start if required.
031 *
032 * @param <E> the type of elements returned by this iterator.
033 * @see org.apache.commons.collections4.iterators.ObjectArrayIterator
034 * @see java.util.Iterator
035 * @see java.util.ListIterator
036 *
037 * @since 3.0
038 */
039public class ObjectArrayListIterator<E> extends ObjectArrayIterator<E>
040        implements ResettableListIterator<E> {
041
042    /**
043     * Holds the index of the last item returned by a call to {@code next()}
044     * or {@code previous()}. This is set to {@code -1} if neither method
045     * has yet been invoked. {@code lastItemIndex} is used to implement the
046     * {@link #set} method.
047     */
048    private int lastItemIndex = -1;
049
050    /**
051     * Constructs an ObjectArrayListIterator that will iterate over the values in the
052     * specified array.
053     *
054     * @param array the array to iterate over
055     * @throws NullPointerException if {@code array} is {@code null}
056     */
057    public ObjectArrayListIterator(final E... array) {
058        super(array);
059    }
060
061    /**
062     * Constructs an ObjectArrayListIterator that will iterate over the values in the
063     * specified array from a specific start index.
064     *
065     * @param array  the array to iterate over
066     * @param start  the index to start iterating at
067     * @throws NullPointerException if {@code array} is {@code null}
068     * @throws IndexOutOfBoundsException if the start index is out of bounds
069     */
070    public ObjectArrayListIterator(final E[] array, final int start) {
071        super(array, start);
072    }
073
074    /**
075     * Constructs an ObjectArrayListIterator that will iterate over a range of values
076     * in the specified array.
077     *
078     * @param array  the array to iterate over
079     * @param start  the index to start iterating at
080     * @param end  the index (exclusive) to finish iterating at
081     * @throws IndexOutOfBoundsException if the start or end index is out of bounds
082     * @throws IllegalArgumentException if end index is before the start
083     * @throws NullPointerException if {@code array} is {@code null}
084     */
085    public ObjectArrayListIterator(final E[] array, final int start, final int end) {
086        super(array, start, end);
087    }
088
089    /**
090     * This iterator does not support modification of its backing array's size, and so will
091     * always throw an {@link UnsupportedOperationException} when this method is invoked.
092     *
093     * @param obj  the object to add
094     * @throws UnsupportedOperationException always thrown.
095     */
096    @Override
097    public void add(final E obj) {
098        throw new UnsupportedOperationException("add() method is not supported");
099    }
100
101    /**
102     * Returns true if there are previous elements to return from the array.
103     *
104     * @return true if there is a previous element to return
105     */
106    @Override
107    public boolean hasPrevious() {
108        return this.index > getStartIndex();
109    }
110
111    /**
112     * Gets the next element from the array.
113     *
114     * @return the next element
115     * @throws NoSuchElementException if there is no next element
116     */
117    @Override
118    public E next() {
119        if (!hasNext()) {
120            throw new NoSuchElementException();
121        }
122        this.lastItemIndex = this.index;
123        return this.array[this.index++];
124    }
125
126    /**
127     * Gets the next index to be retrieved.
128     *
129     * @return the index of the item to be retrieved next
130     */
131    @Override
132    public int nextIndex() {
133        return this.index - getStartIndex();
134    }
135
136    /**
137     * Gets the previous element from the array.
138     *
139     * @return the previous element
140     * @throws NoSuchElementException if there is no previous element
141     */
142    @Override
143    public E previous() {
144        if (!hasPrevious()) {
145            throw new NoSuchElementException();
146        }
147        this.lastItemIndex = --this.index;
148        return this.array[this.index];
149    }
150
151    /**
152     * Gets the index of the item to be retrieved if {@link #previous()} is called.
153     *
154     * @return the index of the item to be retrieved next
155     */
156    @Override
157    public int previousIndex() {
158        return this.index - getStartIndex() - 1;
159    }
160
161    /**
162     * Resets the iterator back to the start index.
163     */
164    @Override
165    public void reset() {
166        super.reset();
167        this.lastItemIndex = -1;
168    }
169
170    /**
171     * Sets the element under the cursor.
172     * <p>
173     * This method sets the element that was returned by the last call
174     * to {@link #next()} of {@link #previous()}.
175     *
176     * <b>Note:</b> {@link java.util.ListIterator} implementations that support {@code add()}
177     * and {@code remove()} only allow {@code set()} to be called once per call
178     * to {@code next()} or {@code previous} (see the {@link java.util.ListIterator}
179     * Javadoc for more details). Since this implementation does not support
180     * {@code add()} or {@code remove()}, {@code set()} may be
181     * called as often as desired.
182     *
183     * @param obj  the object to set into the array
184     * @throws IllegalStateException if next() has not yet been called.
185     * @throws ClassCastException if the object type is unsuitable for the array
186     */
187    @Override
188    public void set(final E obj) {
189        if (this.lastItemIndex == -1) {
190            throw new IllegalStateException("must call next() or previous() before a call to set()");
191        }
192
193        this.array[this.lastItemIndex] = obj;
194    }
195
196}