1 package prefuse.util.collections; 2 3 import java.util.Iterator ; 4 import java.util.NoSuchElementException ; 5 6 11 public class CompositeIterator implements Iterator { 12 13 private Iterator [] m_iters; 14 private int m_cur; 15 16 public CompositeIterator(int size) { 17 m_iters = new Iterator [size]; 18 } 19 20 public CompositeIterator(Iterator iter1, Iterator iter2) { 21 this(new Iterator [] {iter1, iter2}); 22 } 23 24 public CompositeIterator(Iterator [] iters) { 25 m_iters = iters; 26 m_cur = 0; 27 } 28 29 public void setIterator(int idx, Iterator iter) { 30 m_iters[idx] = iter; 31 } 32 33 37 public void remove() { 38 throw new UnsupportedOperationException (); 39 } 40 41 44 public Object next() { 45 if ( hasNext() ) { 46 return m_iters[m_cur].next(); 47 } else { 48 throw new NoSuchElementException (); 49 } 50 } 51 52 55 public boolean hasNext() { 56 if ( m_iters == null ) 57 return false; 58 59 while ( true ) { 60 if ( m_cur >= m_iters.length ) { 61 m_iters = null; 62 return false; 63 } if ( m_iters[m_cur] == null ) { 64 ++m_cur; 65 } else if ( m_iters[m_cur].hasNext() ) { 66 return true; 67 } else { 68 ++m_cur; 69 } 70 } 71 } 72 73 } | Popular Tags |