KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > collections > CompositeIterator


1 package prefuse.util.collections;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.NoSuchElementException JavaDoc;
5
6 /**
7  * Iterator implementation that combines the results of multiple iterators.
8  *
9  * @author <a HREF="http://jheer.org">jeffrey heer</a>
10  */

11 public class CompositeIterator implements Iterator JavaDoc {
12
13     private Iterator JavaDoc[] m_iters;
14     private int m_cur;
15     
16     public CompositeIterator(int size) {
17         m_iters = new Iterator JavaDoc[size];
18     }
19     
20     public CompositeIterator(Iterator JavaDoc iter1, Iterator JavaDoc iter2) {
21         this(new Iterator JavaDoc[] {iter1, iter2});
22     }
23     
24     public CompositeIterator(Iterator JavaDoc[] iters) {
25         m_iters = iters;
26         m_cur = 0;
27     }
28
29     public void setIterator(int idx, Iterator JavaDoc iter) {
30         m_iters[idx] = iter;
31     }
32     
33     /**
34      * Not supported.
35      * @see java.util.Iterator#remove()
36      */

37     public void remove() {
38         throw new UnsupportedOperationException JavaDoc();
39     }
40     
41     /**
42      * @see java.util.Iterator#next()
43      */

44     public Object JavaDoc next() {
45         if ( hasNext() ) {
46             return m_iters[m_cur].next();
47         } else {
48             throw new NoSuchElementException JavaDoc();
49         }
50     }
51
52     /**
53      * @see java.util.Iterator#hasNext()
54      */

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 } // end of class CompositeIterator
74
Popular Tags