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.util.Iterator;
  18. import java.util.NoSuchElementException;
  19. import org.apache.commons.collections.ResettableIterator;
  20. /**
  21. * An {@link 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. * The iterator implements a {@link #reset} method, allowing the reset of the iterator
  27. * back to the start if required.
  28. *
  29. * @since Commons Collections 3.0
  30. * @version $Revision: 1.12 $ $Date: 2004/02/18 00:59:50 $
  31. *
  32. * @author James Strachan
  33. * @author Mauricio S. Moura
  34. * @author Michael A. Smith
  35. * @author Neil O'Toole
  36. * @author Stephen Colebourne
  37. * @author Phil Steitz
  38. */
  39. public class ObjectArrayIterator
  40. implements Iterator, ResettableIterator {
  41. /** The array */
  42. protected Object[] array = null;
  43. /** The start index to loop from */
  44. protected int startIndex = 0;
  45. /** The end index to loop to */
  46. protected int endIndex = 0;
  47. /** The current iterator index */
  48. protected int index = 0;
  49. /**
  50. * Constructor for use with <code>setArray</code>.
  51. * <p>
  52. * Using this constructor, the iterator is equivalent to an empty iterator
  53. * until {@link #setArray} is called to establish the array to iterate over.
  54. */
  55. public ObjectArrayIterator() {
  56. super();
  57. }
  58. /**
  59. * Constructs an ObjectArrayIterator that will iterate over the values in the
  60. * specified array.
  61. *
  62. * @param array the array to iterate over
  63. * @throws NullPointerException if <code>array</code> is <code>null</code>
  64. */
  65. public ObjectArrayIterator(Object[] array) {
  66. this(array, 0, array.length);
  67. }
  68. /**
  69. * Constructs an ObjectArrayIterator that will iterate over the values in the
  70. * specified array from a specific start index.
  71. *
  72. * @param array the array to iterate over
  73. * @param start the index to start iterating at
  74. * @throws NullPointerException if <code>array</code> is <code>null</code>
  75. * @throws IndexOutOfBoundsException if the start index is out of bounds
  76. */
  77. public ObjectArrayIterator(Object array[], int start) {
  78. this(array, start, array.length);
  79. }
  80. /**
  81. * Construct an ObjectArrayIterator that will iterate over a range of values
  82. * in the specified array.
  83. *
  84. * @param array the array to iterate over
  85. * @param start the index to start iterating at
  86. * @param end the index (exclusive) to finish iterating at
  87. * @throws IndexOutOfBoundsException if the start or end index is out of bounds
  88. * @throws IllegalArgumentException if end index is before the start
  89. * @throws NullPointerException if <code>array</code> is <code>null</code>
  90. */
  91. public ObjectArrayIterator(Object array[], int start, int end) {
  92. super();
  93. if (start < 0) {
  94. throw new ArrayIndexOutOfBoundsException("Start index must not be less than zero");
  95. }
  96. if (end > array.length) {
  97. throw new ArrayIndexOutOfBoundsException("End index must not be greater than the array length");
  98. }
  99. if (start > array.length) {
  100. throw new ArrayIndexOutOfBoundsException("Start index must not be greater than the array length");
  101. }
  102. if (end < start) {
  103. throw new IllegalArgumentException("End index must not be less than start index");
  104. }
  105. this.array = array;
  106. this.startIndex = start;
  107. this.endIndex = end;
  108. this.index = start;
  109. }
  110. // Iterator interface
  111. //-------------------------------------------------------------------------
  112. /**
  113. * Returns true if there are more elements to return from the array.
  114. *
  115. * @return true if there is a next element to return
  116. */
  117. public boolean hasNext() {
  118. return (this.index < this.endIndex);
  119. }
  120. /**
  121. * Returns the next element in the array.
  122. *
  123. * @return the next element in the array
  124. * @throws NoSuchElementException if all the elements in the array
  125. * have already been returned
  126. */
  127. public Object next() {
  128. if (hasNext() == false) {
  129. throw new NoSuchElementException();
  130. }
  131. return this.array[this.index++];
  132. }
  133. /**
  134. * Throws {@link UnsupportedOperationException}.
  135. *
  136. * @throws UnsupportedOperationException always
  137. */
  138. public void remove() {
  139. throw new UnsupportedOperationException("remove() method is not supported for an ObjectArrayIterator");
  140. }
  141. // Properties
  142. //-------------------------------------------------------------------------
  143. /**
  144. * Gets the array that this iterator is iterating over.
  145. *
  146. * @return the array this iterator iterates over, or <code>null</code> if
  147. * the no-arg constructor was used and {@link #setArray} has never
  148. * been called with a valid array.
  149. */
  150. public Object[] getArray() {
  151. return this.array;
  152. }
  153. /**
  154. * Sets the array that the ArrayIterator should iterate over.
  155. * <p>
  156. * This method may only be called once, otherwise an IllegalStateException
  157. * will occur.
  158. * <p>
  159. * The {@link #reset} method can be used to reset the iterator if required.
  160. *
  161. * @param array the array that the iterator should iterate over
  162. * @throws IllegalStateException if the <code>array</code> was set in the constructor
  163. * @throws NullPointerException if <code>array</code> is <code>null</code>
  164. */
  165. public void setArray(Object[] array) {
  166. if (this.array != null) {
  167. throw new IllegalStateException("The array to iterate over has already been set");
  168. }
  169. this.array = array;
  170. this.startIndex = 0;
  171. this.endIndex = array.length;
  172. this.index = 0;
  173. }
  174. /**
  175. * Gets the start index to loop from.
  176. *
  177. * @return the start index
  178. */
  179. public int getStartIndex() {
  180. return this.startIndex;
  181. }
  182. /**
  183. * Gets the end index to loop to.
  184. *
  185. * @return the end index
  186. */
  187. public int getEndIndex() {
  188. return this.endIndex;
  189. }
  190. /**
  191. * Resets the iterator back to the start index.
  192. */
  193. public void reset() {
  194. this.index = this.startIndex;
  195. }
  196. }