1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.BitSet ; 20 import java.util.Collection ; 21 import java.util.Comparator ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.NoSuchElementException ; 25 26 import org.apache.commons.collections.list.UnmodifiableList; 27 28 42 public class CollatingIterator implements Iterator { 43 44 45 private Comparator comparator = null; 46 47 48 private ArrayList iterators = null; 49 50 51 private ArrayList values = null; 52 53 54 private BitSet valueSet = null; 55 56 57 private int lastReturned = -1; 58 59 66 public CollatingIterator() { 67 this(null,2); 68 } 69 70 77 public CollatingIterator(final Comparator comp) { 78 this(comp,2); 79 } 80 81 91 public CollatingIterator(final Comparator comp, final int initIterCapacity) { 92 iterators = new ArrayList (initIterCapacity); 93 setComparator(comp); 94 } 95 96 106 public CollatingIterator(final Comparator comp, final Iterator a, final Iterator b) { 107 this(comp,2); 108 addIterator(a); 109 addIterator(b); 110 } 111 112 121 public CollatingIterator(final Comparator comp, final Iterator [] iterators) { 122 this(comp, iterators.length); 123 for (int i = 0; i < iterators.length; i++) { 124 addIterator(iterators[i]); 125 } 126 } 127 128 139 public CollatingIterator(final Comparator comp, final Collection iterators) { 140 this(comp, iterators.size()); 141 for (Iterator it = iterators.iterator(); it.hasNext();) { 142 Iterator item = (Iterator ) it.next(); 143 addIterator(item); 144 } 145 } 146 147 156 public void addIterator(final Iterator iterator) { 157 checkNotStarted(); 158 if (iterator == null) { 159 throw new NullPointerException ("Iterator must not be null"); 160 } 161 iterators.add(iterator); 162 } 163 164 173 public void setIterator(final int index, final Iterator iterator) { 174 checkNotStarted(); 175 if (iterator == null) { 176 throw new NullPointerException ("Iterator must not be null"); 177 } 178 iterators.set(index, iterator); 179 } 180 181 186 public List getIterators() { 187 return UnmodifiableList.decorate(iterators); 188 } 189 190 193 public Comparator getComparator() { 194 return comparator; 195 } 196 197 202 public void setComparator(final Comparator comp) { 203 checkNotStarted(); 204 comparator = comp; 205 } 206 207 214 public boolean hasNext() { 215 start(); 216 return anyValueSet(valueSet) || anyHasNext(iterators); 217 } 218 219 225 public Object next() throws NoSuchElementException { 226 if (hasNext() == false) { 227 throw new NoSuchElementException (); 228 } 229 int leastIndex = least(); 230 if (leastIndex == -1) { 231 throw new NoSuchElementException (); 232 } else { 233 Object val = values.get(leastIndex); 234 clear(leastIndex); 235 lastReturned = leastIndex; 236 return val; 237 } 238 } 239 240 247 public void remove() { 248 if (lastReturned == -1) { 249 throw new IllegalStateException ("No value can be removed at present"); 250 } 251 Iterator it = (Iterator ) (iterators.get(lastReturned)); 252 it.remove(); 253 } 254 255 260 private void start() { 261 if (values == null) { 262 values = new ArrayList (iterators.size()); 263 valueSet = new BitSet (iterators.size()); 264 for (int i = 0; i < iterators.size(); i++) { 265 values.add(null); 266 valueSet.clear(i); 267 } 268 } 269 } 270 271 280 private boolean set(int i) { 281 Iterator it = (Iterator )(iterators.get(i)); 282 if (it.hasNext()) { 283 values.set(i, it.next()); 284 valueSet.set(i); 285 return true; 286 } else { 287 values.set(i,null); 288 valueSet.clear(i); 289 return false; 290 } 291 } 292 293 297 private void clear(int i) { 298 values.set(i,null); 299 valueSet.clear(i); 300 } 301 302 308 private void checkNotStarted() throws IllegalStateException { 309 if (values != null) { 310 throw new IllegalStateException ("Can't do that after next or hasNext has been called."); 311 } 312 } 313 314 320 private int least() { 321 int leastIndex = -1; 322 Object leastObject = null; 323 for (int i = 0; i < values.size(); i++) { 324 if (valueSet.get(i) == false) { 325 set(i); 326 } 327 if (valueSet.get(i)) { 328 if (leastIndex == -1) { 329 leastIndex = i; 330 leastObject = values.get(i); 331 } else { 332 Object curObject = values.get(i); 333 if (comparator.compare(curObject,leastObject) < 0) { 334 leastObject = curObject; 335 leastIndex = i; 336 } 337 } 338 } 339 } 340 return leastIndex; 341 } 342 343 347 private boolean anyValueSet(BitSet set) { 348 for (int i = 0; i < set.size(); i++) { 349 if (set.get(i)) { 350 return true; 351 } 352 } 353 return false; 354 } 355 356 360 private boolean anyHasNext(ArrayList iters) { 361 for (int i = 0; i < iters.size(); i++) { 362 Iterator it = (Iterator ) iters.get(i); 363 if (it.hasNext()) { 364 return true; 365 } 366 } 367 return false; 368 } 369 370 } 371 | Popular Tags |