KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > type > TypeTreeIterator


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of GOSSIP nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.type;
30
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.NoSuchElementException JavaDoc;
35 import java.util.Stack JavaDoc;
36
37 /**
38  *
39  *
40  * @author Bruce Lowery
41  */

42 public class TypeTreeIterator implements Iterator JavaDoc
43 {
44     private LinkedList JavaDoc _stack = new LinkedList JavaDoc();
45     private boolean _depthFirst;
46     
47     /**
48      * Create an iterator for a type tree.
49      * @parm t the type tree to iterate over.
50      * @parm depth if true, iterate depth first; otherwise,
51      * iterate breadth first.
52      */

53     public TypeTreeIterator(TypeTree t, boolean depth)
54     {
55         super();
56         _stack.add(t.getRoot());
57         _depthFirst = depth;
58     }
59
60     /**
61      * This operation is not supported.
62      */

63     public void remove()
64     {
65         throw new UnsupportedOperationException JavaDoc();
66     }
67
68     /* (non-Javadoc)
69      * @see java.util.Iterator#hasNext()
70      */

71     public boolean hasNext()
72     {
73         return !_stack.isEmpty();
74     }
75
76     /* (non-Javadoc)
77      * @see java.util.Iterator#next()
78      */

79     public Object JavaDoc next()
80     {
81         Node n = (Node) _stack.removeFirst();
82         Class JavaDoc next = n.getType();
83         for (Iterator JavaDoc it=n.getBranches().iterator(); it.hasNext(); )
84         {
85             if (_depthFirst)
86                 _stack.addFirst(it.next());
87             else
88                 _stack.addLast(it.next());
89         }
90         return next;
91     }
92
93 }
94
Popular Tags