VisitorStack.java

biz/hammurapi/util/VisitorStack.java

Violations

Inspector Message Severity Location
Java Inspector 039 Avoid "for", "do", "while", "if" and "if ... else" statements with empty bodies 1 68:33
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 67:30
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 68:71
Java Inspector 068 Do not use System.out and System.err to output logging messages. Use loggers instead. 2 160:43
Java Inspector 089 Method is not properly documented 2 80:9
Java Inspector 089 Method is not properly documented 2 88:9
Java Inspector 089 Method is not properly documented 2 96:9
Java Inspector 089 Parameter clazz is not documented 2 96:9
Java Inspector 089 Method is not properly documented 2 110:9
Java Inspector 089 Undocumented parameter classes 2 110:9
Java Inspector 089 Javadoc contains tag for non-existent parameter clases 2 110:9
Java Inspector 089 Method is not properly documented 2 127:9
Java Inspector 089 Parameter clazz is not documented 2 127:9
Java Inspector 089 Method is not properly documented 2 143:9
Java Inspector 089 Undocumented parameter classes 2 143:9
Java Inspector 089 Javadoc contains tag for non-existent parameter clases 2 143:9
Java Inspector 089 Undocumented method 2 157:9
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 68:75
Java Inspector 046 Empty statements 3 68:75

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.Iterator;
28import java.util.List;
29import java.util.Stack;
30
31/**
32 * Provides visiting stack information.
33 * @author Pavel Vlasov
34 * @version $Revision: 1.3 $
35 */
36public class VisitorStack {
37 VisitorStack() {
38 // Default constructor
39 super();
40 }
41
42 private Stack stack=new Stack();
43 private List unmodifiableStack = Collections.unmodifiableList(stack);
44
45 void push(Object o) {
46// tab();
47// System.out.println("-> "+o);
48 stack.push(o);
49 }
50
51 /**
52 *
53 */
54// private void tab() {
55// for (int i=0; i<stack.size(); i++) {
56// System.out.print(" ");
57// }
58// }
59
60 /**
61 * Removes objects before this object and the object itself from the stack.
62 * == operator used for objects comparison
63 */
64 void pop(Object o) {
65 Iterator it=stack.iterator();
66 while (it.hasNext()) {
67 if (o==it.next()) {
68 while (!stack.isEmpty() && stack.pop()!=o);
69 break;
70 }
71 }
72// tab();
73// System.out.println("<- "+o);
74 }
75
76 /**
77 *
78 * @return Object from the top of the stack.
79 */
80 public Object peek() {
81 return stack.peek();
82 }
83
84 /**
85 *
86 * @return Unmodifiable stack
87 */
88 public List getStack() {
89 return unmodifiableStack;
90 }
91
92 /**
93 * @param clazz
94 * @return true if stack contains objects of type of the class.
95 */
96 public boolean isIn(Class clazz) {
97 Iterator it=stack.iterator();
98 while (it.hasNext()) {
99 if (clazz.isInstance(it.next())) {
100 return true;
101 }
102 }
103 return false;
104 }
105
106 /**
107 * @param clases
108 * @return true if stack contains objects of type of one of the classes.
109 */
110 public boolean isIn(Class[] classes) {
111 Iterator it=stack.iterator();
112 while (it.hasNext()) {
113 Object next = it.next();
114 for (int i=0; i<classes.length; i++) {
115 if (classes[i].isInstance(next)) {
116 return true;
117 }
118 }
119 }
120 return false;
121 }
122
123 /**
124 * @param clazz
125 * @return list of stack entries of given type.
126 */
127 public List getStack(Class clazz) {
128 List ret=new ArrayList();
129 Iterator it=stack.iterator();
130 while (it.hasNext()) {
131 Object next = it.next();
132 if (clazz.isInstance(next)) {
133 ret.add(next);
134 }
135 }
136 return ret;
137 }
138
139 /**
140 * @param clases
141 * @return list of stack entries of given types.
142 */
143 public List getStack(Class[] classes) {
144 List ret=new ArrayList();
145 Iterator it=stack.iterator();
146 while (it.hasNext()) {
147 for (int i=0; i<classes.length; i++) {
148 Object next = it.next();
149 if (classes[i].isInstance(next)) {
150 ret.add(next);
151 }
152 }
153 }
154 return ret;
155 }
156
157 public void print() {
158 Iterator it=stack.iterator();
159 while (it.hasNext()) {
160 System.out.println(it.next());
161 }
162 }
163
164}
165