1 4 5 package polyglot.util; 6 7 import java.util.Iterator ; 8 9 18 public final class ConcatenatedIterator implements Iterator { 19 23 public ConcatenatedIterator(Iterator iter1, Iterator iter2) { 24 this(new Iterator []{iter1, iter2}); 25 } 26 27 31 public ConcatenatedIterator(Iterator [] iters) { 32 this.backing_iterators = (Iterator []) iters.clone(); 33 findNextItem(); 34 } 35 36 40 public ConcatenatedIterator(java.util.Collection iters) { 41 this.backing_iterators = (Iterator [])iters.toArray(new Iterator [0]); 42 findNextItem(); 43 } 44 45 public Object next() { 46 Object res = next_item; 47 if (res == null) 48 throw new java.util.NoSuchElementException (); 49 findNextItem(); 50 return res; 51 } 52 53 public boolean hasNext() { 54 return next_item != null; 55 } 56 57 public void remove() { 58 throw new UnsupportedOperationException ("ConcatenatedIterator.remove"); 59 } 60 61 private void findNextItem() { 63 while(index < backing_iterators.length) { 64 Iterator it = backing_iterators[index]; 65 if (it.hasNext()) { 66 next_item = it.next(); 67 return; 68 } else { 69 index++; 70 } 71 } 72 next_item = null; 73 } 74 75 Object next_item; 80 Iterator [] backing_iterators; 81 int index; 82 } 83 84 85 | Popular Tags |