1 25 package org.archive.util.iterator; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.NoSuchElementException ; 30 31 35 public class CompositeIterator implements Iterator { 36 ArrayList <Iterator > iterators = new ArrayList <Iterator >(); 37 Iterator currentIterator; 38 int indexOfCurrentIterator = -1; 39 40 45 private boolean nextIterator() { 46 if (++indexOfCurrentIterator < iterators.size()) { 47 currentIterator = (Iterator ) iterators.get(indexOfCurrentIterator); 48 return hasNext(); 50 } else { 51 currentIterator = null; 52 return false; 53 } 54 } 55 56 59 public boolean hasNext() { 60 if(currentIterator!=null && currentIterator.hasNext()) { 61 return true; 63 } else { 64 return nextIterator(); 66 } 67 } 68 69 72 public Object next() { 73 if(hasNext()) { 74 return currentIterator.next(); 75 } else { 76 throw new NoSuchElementException (); 77 } 78 } 79 80 83 public void remove() { 84 throw new UnsupportedOperationException (); 85 } 86 87 91 public CompositeIterator() { 92 super(); 93 } 94 95 101 public CompositeIterator(Iterator i1, Iterator i2) { 102 this(); 103 add(i1); 104 add(i2); 105 } 106 107 112 public void add(Iterator i) { 113 iterators.add(i); 114 } 115 116 } 117 | Popular Tags |