1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.iterators;
  17. import java.lang.reflect.Array;
  18. import java.util.ListIterator;
  19. import java.util.NoSuchElementException;
  20. import org.apache.commons.collections.ResettableListIterator;
  21. /**
  22. * Implements a {@link 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. *
  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. *
  32. * @see org.apache.commons.collections.iterators.ArrayIterator
  33. * @see java.util.Iterator
  34. * @see java.util.ListIterator
  35. *
  36. * @since Commons Collections 3.0
  37. * @version $Revision: 1.12 $ $Date: 2004/02/18 00:59:50 $
  38. *
  39. * @author Neil O'Toole
  40. * @author Stephen Colebourne
  41. * @author Phil Steitz
  42. */
  43. public class ArrayListIterator extends ArrayIterator
  44. implements ListIterator, ResettableListIterator {
  45. /**
  46. * Holds the index of the last item returned by a call to <code>next()</code>
  47. * or <code>previous()</code>. This is set to <code>-1</code> if neither method
  48. * has yet been invoked. <code>lastItemIndex</code> is used to to implement
  49. * the {@link #set} method.
  50. *
  51. */
  52. protected int lastItemIndex = -1;
  53. // Constructors
  54. // ----------------------------------------------------------------------
  55. /**
  56. * Constructor for use with <code>setArray</code>.
  57. * <p>
  58. * Using this constructor, the iterator is equivalent to an empty iterator
  59. * until {@link #setArray(Object)} is called to establish the array to iterate over.
  60. */
  61. public ArrayListIterator() {
  62. super();
  63. }
  64. /**
  65. * Constructs an ArrayListIterator that will iterate over the values in the
  66. * specified array.
  67. *
  68. * @param array the array to iterate over
  69. * @throws IllegalArgumentException if <code>array</code> is not an array.
  70. * @throws NullPointerException if <code>array</code> is <code>null</code>
  71. */
  72. public ArrayListIterator(Object array) {
  73. super(array);
  74. }
  75. /**
  76. * Constructs an ArrayListIterator that will iterate over the values in the
  77. * specified array from a specific start index.
  78. *
  79. * @param array the array to iterate over
  80. * @param startIndex the index to start iterating at
  81. * @throws IllegalArgumentException if <code>array</code> is not an array.
  82. * @throws NullPointerException if <code>array</code> is <code>null</code>
  83. * @throws IndexOutOfBoundsException if the start index is out of bounds
  84. */
  85. public ArrayListIterator(Object array, int startIndex) {
  86. super(array, startIndex);
  87. this.startIndex = startIndex;
  88. }
  89. /**
  90. * Construct an ArrayListIterator that will iterate over a range of values
  91. * in the specified array.
  92. *
  93. * @param array the array to iterate over
  94. * @param startIndex the index to start iterating at
  95. * @param endIndex the index (exclusive) to finish iterating at
  96. * @throws IllegalArgumentException if <code>array</code> is not an array.
  97. * @throws IndexOutOfBoundsException if the start or end index is out of bounds
  98. * @throws IllegalArgumentException if end index is before the start
  99. * @throws NullPointerException if <code>array</code> is <code>null</code>
  100. */
  101. public ArrayListIterator(Object array, int startIndex, int endIndex) {
  102. super(array, startIndex, endIndex);
  103. this.startIndex = startIndex;
  104. }
  105. // ListIterator interface
  106. //-----------------------------------------------------------------------
  107. /**
  108. * Returns true if there are previous elements to return from the array.
  109. *
  110. * @return true if there is a previous element to return
  111. */
  112. public boolean hasPrevious() {
  113. return (this.index > this.startIndex);
  114. }
  115. /**
  116. * Gets the previous element from the array.
  117. *
  118. * @return the previous element
  119. * @throws NoSuchElementException if there is no previous element
  120. */
  121. public Object previous() {
  122. if (hasPrevious() == false) {
  123. throw new NoSuchElementException();
  124. }
  125. this.lastItemIndex = --this.index;
  126. return Array.get(this.array, this.index);
  127. }
  128. /**
  129. * Gets the next element from the array.
  130. *
  131. * @return the next element
  132. * @throws NoSuchElementException if there is no next element
  133. */
  134. public Object next() {
  135. if (hasNext() == false) {
  136. throw new NoSuchElementException();
  137. }
  138. this.lastItemIndex = this.index;
  139. return Array.get(this.array, this.index++);
  140. }
  141. /**
  142. * Gets the next index to be retrieved.
  143. *
  144. * @return the index of the item to be retrieved next
  145. */
  146. public int nextIndex() {
  147. return this.index - this.startIndex;
  148. }
  149. /**
  150. * Gets the index of the item to be retrieved if {@link #previous()} is called.
  151. *
  152. * @return the index of the item to be retrieved next
  153. */
  154. public int previousIndex() {
  155. return this.index - this.startIndex - 1;
  156. }
  157. /**
  158. * This iterator does not support modification of its backing collection, and so will
  159. * always throw an {@link UnsupportedOperationException} when this method is invoked.
  160. *
  161. * @throws UnsupportedOperationException always thrown.
  162. * @see java.util.ListIterator#set
  163. */
  164. public void add(Object o) {
  165. throw new UnsupportedOperationException("add() method is not supported");
  166. }
  167. /**
  168. * Sets the element under the cursor.
  169. * <p>
  170. * This method sets the element that was returned by the last call
  171. * to {@link #next()} of {@link #previous()}.
  172. * <p>
  173. * <b>Note:</b> {@link ListIterator} implementations that support
  174. * <code>add()</code> and <code>remove()</code> only allow <code>set()</code> to be called
  175. * once per call to <code>next()</code> or <code>previous</code> (see the {@link ListIterator}
  176. * javadoc for more details). Since this implementation does
  177. * not support <code>add()</code> or <code>remove()</code>, <code>set()</code> may be
  178. * called as often as desired.
  179. *
  180. * @see java.util.ListIterator#set
  181. */
  182. public void set(Object o) {
  183. if (this.lastItemIndex == -1) {
  184. throw new IllegalStateException("must call next() or previous() before a call to set()");
  185. }
  186. Array.set(this.array, this.lastItemIndex, o);
  187. }
  188. /**
  189. * Resets the iterator back to the start index.
  190. */
  191. public void reset() {
  192. super.reset();
  193. this.lastItemIndex = -1;
  194. }
  195. }