1 29 package jegg.type; 30 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.NoSuchElementException ; 35 import java.util.Stack ; 36 37 42 public class TypeTreeIterator implements Iterator 43 { 44 private LinkedList _stack = new LinkedList (); 45 private boolean _depthFirst; 46 47 53 public TypeTreeIterator(TypeTree t, boolean depth) 54 { 55 super(); 56 _stack.add(t.getRoot()); 57 _depthFirst = depth; 58 } 59 60 63 public void remove() 64 { 65 throw new UnsupportedOperationException (); 66 } 67 68 71 public boolean hasNext() 72 { 73 return !_stack.isEmpty(); 74 } 75 76 79 public Object next() 80 { 81 Node n = (Node) _stack.removeFirst(); 82 Class next = n.getType(); 83 for (Iterator 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 |