1 3 package jodd.util.collection; 4 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 import java.util.NoSuchElementException ; 8 9 public class JoinedIterator implements Iterator { 10 11 private Iterator mIterators[]; 12 private int mCurrentIterator; 13 14 public JoinedIterator(Collection collection) { 15 mIterators = new Iterator [collection.size()]; 16 mIterators = (Iterator [])collection.toArray(mIterators); 17 } 18 19 public JoinedIterator(Iterator iterator, Iterator iterator1) { 20 mIterators = (new Iterator [] {iterator, iterator1}); 21 } 22 23 public JoinedIterator(Iterator aiterator[]) { 24 mIterators = aiterator; 25 } 26 27 public boolean hasNext() { 28 if (mCurrentIterator >= mIterators.length) return false; 29 if (mIterators[mCurrentIterator].hasNext()) return true; 30 mCurrentIterator++; 31 if (mCurrentIterator >= mIterators.length) return false; 32 else return mIterators[mCurrentIterator].hasNext(); 33 } 34 35 public Object next() { 36 if (!hasNext()) { 37 throw new NoSuchElementException (); 38 } else { 39 return mIterators[mCurrentIterator].next(); 40 } 41 } 42 43 public void remove() { 44 if (mCurrentIterator >= mIterators.length) { 45 throw new NoSuchElementException (); 46 } else { 47 mIterators[mCurrentIterator].remove(); 48 } 49 } 50 } 51 | Popular Tags |