001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.util;
024:
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Stack;
030:
031: /**
032: * Provides visiting stack information.
033: * @author Pavel Vlasov
034: * @version $Revision: 1.3 $
035: */
036: public class VisitorStack {
037: VisitorStack() {
038: // Default constructor
039: super ();
040: }
041:
042: private Stack stack = new Stack();
043: private List unmodifiableStack = Collections
044: .unmodifiableList(stack);
045:
046: void push(Object o) {
047: // tab();
048: // System.out.println("-> "+o);
049: stack.push(o);
050: }
051:
052: /**
053: *
054: */
055: // private void tab() {
056: // for (int i=0; i<stack.size(); i++) {
057: // System.out.print(" ");
058: // }
059: // }
060: /**
061: * Removes objects before this object and the object itself from the stack.
062: * == operator used for objects comparison
063: */
064: void pop(Object o) {
065: Iterator it = stack.iterator();
066: while (it.hasNext()) {
067: if (o == it.next()) {
068: while (!stack.isEmpty() && stack.pop() != o)
069: ;
070: break;
071: }
072: }
073: // tab();
074: // System.out.println("<- "+o);
075: }
076:
077: /**
078: *
079: * @return Object from the top of the stack.
080: */
081: public Object peek() {
082: return stack.peek();
083: }
084:
085: /**
086: *
087: * @return Unmodifiable stack
088: */
089: public List getStack() {
090: return unmodifiableStack;
091: }
092:
093: /**
094: * @param clazz
095: * @return true if stack contains objects of type of the class.
096: */
097: public boolean isIn(Class clazz) {
098: Iterator it = stack.iterator();
099: while (it.hasNext()) {
100: if (clazz.isInstance(it.next())) {
101: return true;
102: }
103: }
104: return false;
105: }
106:
107: /**
108: * @param clases
109: * @return true if stack contains objects of type of one of the classes.
110: */
111: public boolean isIn(Class[] classes) {
112: Iterator it = stack.iterator();
113: while (it.hasNext()) {
114: Object next = it.next();
115: for (int i = 0; i < classes.length; i++) {
116: if (classes[i].isInstance(next)) {
117: return true;
118: }
119: }
120: }
121: return false;
122: }
123:
124: /**
125: * @param clazz
126: * @return list of stack entries of given type.
127: */
128: public List getStack(Class clazz) {
129: List ret = new ArrayList();
130: Iterator it = stack.iterator();
131: while (it.hasNext()) {
132: Object next = it.next();
133: if (clazz.isInstance(next)) {
134: ret.add(next);
135: }
136: }
137: return ret;
138: }
139:
140: /**
141: * @param clases
142: * @return list of stack entries of given types.
143: */
144: public List getStack(Class[] classes) {
145: List ret = new ArrayList();
146: Iterator it = stack.iterator();
147: while (it.hasNext()) {
148: for (int i = 0; i < classes.length; i++) {
149: Object next = it.next();
150: if (classes[i].isInstance(next)) {
151: ret.add(next);
152: }
153: }
154: }
155: return ret;
156: }
157:
158: public void print() {
159: Iterator it = stack.iterator();
160: while (it.hasNext()) {
161: System.out.println(it.next());
162: }
163: }
164:
165: }
|