1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.collections.list.UnmodifiableList; 24 25 50 public class IteratorChain implements Iterator { 51 52 53 protected final List iteratorChain = new ArrayList (); 54 55 protected int currentIteratorIndex = 0; 56 57 protected Iterator currentIterator = null; 58 63 protected Iterator lastUsedIterator = null; 64 68 protected boolean isLocked = false; 69 70 77 public IteratorChain() { 78 super(); 79 } 80 81 87 public IteratorChain(Iterator iterator) { 88 super(); 89 addIterator(iterator); 90 } 91 92 100 public IteratorChain(Iterator a, Iterator b) { 101 super(); 102 addIterator(a); 103 addIterator(b); 104 } 105 106 113 public IteratorChain(Iterator [] iterators) { 114 super(); 115 for (int i = 0; i < iterators.length; i++) { 116 addIterator(iterators[i]); 117 } 118 } 119 120 128 public IteratorChain(Collection iterators) { 129 super(); 130 for (Iterator it = iterators.iterator(); it.hasNext();) { 131 Iterator item = (Iterator ) it.next(); 132 addIterator(item); 133 } 134 } 135 136 144 public void addIterator(Iterator iterator) { 145 checkLocked(); 146 if (iterator == null) { 147 throw new NullPointerException ("Iterator must not be null"); 148 } 149 iteratorChain.add(iterator); 150 } 151 152 161 public void setIterator(int index, Iterator iterator) throws IndexOutOfBoundsException { 162 checkLocked(); 163 if (iterator == null) { 164 throw new NullPointerException ("Iterator must not be null"); 165 } 166 iteratorChain.set(index, iterator); 167 } 168 169 174 public List getIterators() { 175 return UnmodifiableList.decorate(iteratorChain); 176 } 177 178 183 public int size() { 184 return iteratorChain.size(); 185 } 186 187 194 public boolean isLocked() { 195 return isLocked; 196 } 197 198 201 private void checkLocked() { 202 if (isLocked == true) { 203 throw new UnsupportedOperationException ("IteratorChain cannot be changed after the first use of a method from the Iterator interface"); 204 } 205 } 206 207 211 private void lockChain() { 212 if (isLocked == false) { 213 isLocked = true; 214 } 215 } 216 217 221 protected void updateCurrentIterator() { 222 if (currentIterator == null) { 223 if (iteratorChain.isEmpty()) { 224 currentIterator = EmptyIterator.INSTANCE; 225 } else { 226 currentIterator = (Iterator ) iteratorChain.get(0); 227 } 228 lastUsedIterator = currentIterator; 231 } 232 233 while (currentIterator.hasNext() == false && currentIteratorIndex < iteratorChain.size() - 1) { 234 currentIteratorIndex++; 235 currentIterator = (Iterator ) iteratorChain.get(currentIteratorIndex); 236 } 237 } 238 239 245 public boolean hasNext() { 246 lockChain(); 247 updateCurrentIterator(); 248 lastUsedIterator = currentIterator; 249 250 return currentIterator.hasNext(); 251 } 252 253 259 public Object next() { 260 lockChain(); 261 updateCurrentIterator(); 262 lastUsedIterator = currentIterator; 263 264 return currentIterator.next(); 265 } 266 267 281 public void remove() { 282 lockChain(); 283 updateCurrentIterator(); 284 285 lastUsedIterator.remove(); 286 } 287 288 } 289 | Popular Tags |