1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 21 import org.apache.commons.collections.ResettableIterator; 22 23 42 public class ObjectArrayIterator 43 implements Iterator , ResettableIterator { 44 45 46 protected Object [] array = null; 47 48 protected int startIndex = 0; 49 50 protected int endIndex = 0; 51 52 protected int index = 0; 53 54 60 public ObjectArrayIterator() { 61 super(); 62 } 63 64 71 public ObjectArrayIterator(Object [] array) { 72 this(array, 0, array.length); 73 } 74 75 84 public ObjectArrayIterator(Object array[], int start) { 85 this(array, start, array.length); 86 } 87 88 99 public ObjectArrayIterator(Object array[], int start, int end) { 100 super(); 101 if (start < 0) { 102 throw new ArrayIndexOutOfBoundsException ("Start index must not be less than zero"); 103 } 104 if (end > array.length) { 105 throw new ArrayIndexOutOfBoundsException ("End index must not be greater than the array length"); 106 } 107 if (start > array.length) { 108 throw new ArrayIndexOutOfBoundsException ("Start index must not be greater than the array length"); 109 } 110 if (end < start) { 111 throw new IllegalArgumentException ("End index must not be less than start index"); 112 } 113 this.array = array; 114 this.startIndex = start; 115 this.endIndex = end; 116 this.index = start; 117 } 118 119 122 127 public boolean hasNext() { 128 return (this.index < this.endIndex); 129 } 130 131 138 public Object next() { 139 if (hasNext() == false) { 140 throw new NoSuchElementException (); 141 } 142 return this.array[this.index++]; 143 } 144 145 150 public void remove() { 151 throw new UnsupportedOperationException ("remove() method is not supported for an ObjectArrayIterator"); 152 } 153 154 157 164 public Object [] getArray() { 165 return this.array; 166 } 167 168 180 public void setArray(Object [] array) { 181 if (this.array != null) { 182 throw new IllegalStateException ("The array to iterate over has already been set"); 183 } 184 this.array = array; 185 this.startIndex = 0; 186 this.endIndex = array.length; 187 this.index = 0; 188 } 189 190 195 public int getStartIndex() { 196 return this.startIndex; 197 } 198 199 204 public int getEndIndex() { 205 return this.endIndex; 206 } 207 208 211 public void reset() { 212 this.index = this.startIndex; 213 } 214 215 } 216 | Popular Tags |