1 16 package org.apache.commons.collections.iterators; 17 18 import java.lang.reflect.Array ; 19 import java.util.ListIterator ; 20 import java.util.NoSuchElementException ; 21 22 import org.apache.commons.collections.ResettableListIterator; 23 24 46 public class ArrayListIterator extends ArrayIterator 47 implements ListIterator , ResettableListIterator { 48 49 56 protected int lastItemIndex = -1; 57 58 66 public ArrayListIterator() { 67 super(); 68 } 69 70 78 public ArrayListIterator(Object array) { 79 super(array); 80 } 81 82 92 public ArrayListIterator(Object array, int startIndex) { 93 super(array, startIndex); 94 this.startIndex = startIndex; 95 } 96 97 109 public ArrayListIterator(Object array, int startIndex, int endIndex) { 110 super(array, startIndex, endIndex); 111 this.startIndex = startIndex; 112 } 113 114 121 public boolean hasPrevious() { 122 return (this.index > this.startIndex); 123 } 124 125 131 public Object previous() { 132 if (hasPrevious() == false) { 133 throw new NoSuchElementException (); 134 } 135 this.lastItemIndex = --this.index; 136 return Array.get(this.array, this.index); 137 } 138 139 145 public Object next() { 146 if (hasNext() == false) { 147 throw new NoSuchElementException (); 148 } 149 this.lastItemIndex = this.index; 150 return Array.get(this.array, this.index++); 151 } 152 153 158 public int nextIndex() { 159 return this.index - this.startIndex; 160 } 161 162 167 public int previousIndex() { 168 return this.index - this.startIndex - 1; 169 } 170 171 178 public void add(Object o) { 179 throw new UnsupportedOperationException ("add() method is not supported"); 180 } 181 182 197 public void set(Object o) { 198 if (this.lastItemIndex == -1) { 199 throw new IllegalStateException ("must call next() or previous() before a call to set()"); 200 } 201 202 Array.set(this.array, this.lastItemIndex, o); 203 } 204 205 208 public void reset() { 209 super.reset(); 210 this.lastItemIndex = -1; 211 } 212 213 } 214 | Popular Tags |