001    /*
002     @license.text@
003      */
004    package biz.hammurapi.util;
005    
006    import java.util.ArrayList;
007    import java.util.Collections;
008    import java.util.Iterator;
009    import java.util.List;
010    import java.util.Stack;
011    
012    /**
013     * Provides visiting stack information.
014     * @author Pavel Vlasov
015     * @version $Revision: 1.3 $
016     */
017    public class VisitorStack {
018            VisitorStack() {
019                    // Default constructor
020                    super();
021            }
022            
023            private Stack stack=new Stack();
024            private List unmodifiableStack = Collections.unmodifiableList(stack);
025            
026            void push(Object o) {
027    //              tab();
028    //              System.out.println("-> "+o);
029                    stack.push(o);          
030            }
031    
032            /**
033             * 
034             */
035    //      private void tab() {
036    //              for (int i=0; i<stack.size(); i++) {
037    //                      System.out.print("  ");
038    //              }
039    //      }
040            
041            /**
042             * Removes objects before this object and the object itself from the stack. 
043             * == operator used for objects comparison
044             */
045            void pop(Object o) {
046                    Iterator it=stack.iterator();
047                    while (it.hasNext()) {
048                            if (o==it.next()) {
049                                    while (!stack.isEmpty() && stack.pop()!=o);
050                                    break;
051                            }
052                    }
053    //              tab();
054    //              System.out.println("<- "+o);
055            }
056            
057            /**
058             * 
059             * @return Object from the top of the stack.
060             */     
061            public Object peek() {
062                    return stack.peek();            
063            }
064            
065            /**
066             * 
067             * @return Unmodifiable stack
068             */
069            public List getStack() {
070                    return unmodifiableStack;
071            }
072            
073            /**
074             * @param clazz
075             * @return true if stack contains objects of type of the class.
076             */
077            public boolean isIn(Class clazz) {
078                    Iterator it=stack.iterator();
079                    while (it.hasNext()) {
080                            if (clazz.isInstance(it.next())) {
081                                    return true;
082                            }
083                    }
084                    return false;
085            }
086    
087            /**
088             * @param clases
089             * @return true if stack contains objects of type of one of the classes.
090             */
091            public boolean isIn(Class[] classes) {
092                    Iterator it=stack.iterator();
093                    while (it.hasNext()) {
094                            Object next = it.next();
095                            for (int i=0; i<classes.length; i++) {
096                                    if (classes[i].isInstance(next)) {
097                                            return true;
098                                    }
099                            }
100                    }
101                    return false;
102            }
103            
104            /**
105             * @param clazz
106             * @return list of stack entries of given type.
107             */
108            public List getStack(Class clazz) {
109                    List ret=new ArrayList();
110                    Iterator it=stack.iterator();
111                    while (it.hasNext()) {
112                            Object next = it.next();
113                            if (clazz.isInstance(next)) {
114                                    ret.add(next);
115                            }
116                    }
117                    return ret;
118            }
119    
120            /**
121             * @param clases
122             * @return list of stack entries of given types.
123             */
124            public List getStack(Class[] classes) {
125                    List ret=new ArrayList();
126                    Iterator it=stack.iterator();
127                    while (it.hasNext()) {
128                            for (int i=0; i<classes.length; i++) {
129                                    Object next = it.next();
130                                    if (classes[i].isInstance(next)) {
131                                            ret.add(next);
132                                    }
133                            }
134                    }
135                    return ret;
136            }
137            
138            public void print() {
139                    Iterator it=stack.iterator();
140                    while (it.hasNext()) {
141                            System.out.println(it.next());
142                    }
143            }       
144            
145    }