VisitableBase.java

biz/hammurapi/util/VisitableBase.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 070-B Cyclomatic complexity is too high: 24, maximum allowed is 20 1 110:9
Java Inspector 049 Use a Collection instead of arrays Object[] 2 61:17
Java Inspector 049 Use a Collection instead of arrays Object[] 2 172:33
Java Inspector 070-A Cyclomatic complexity is too high: 24, maximum allowed is 12 2 110:9
Java Inspector 089 Undocumented method 2 44:9
Java Inspector 089 Undocumented method 2 60:9
Java Inspector 089 Undocumented method 2 64:25
Java Inspector 089 Undocumented method 2 80:9
Java Inspector 089 Undocumented method 2 84:25
Java Inspector 089 Parameter visitor is not documented 2 99:9
Java Inspector 089 Parameter visitor is not documented 2 110:9
Java Inspector 089 Javadoc contains tag for non-existent parameter child 2 110:9
Java Inspector 089 Undocumented parameter object 2 168:9
Java Inspector 089 Javadoc contains tag for non-existent parameter visitor 2 168:9
Java Inspector 089 Javadoc contains tag for non-existent parameter child 2 168:9
Java Inspector 089 Method return value is not documented 2 168:9
Java Inspector 089 Undocumented method 2 171:25
Java Inspector 089 Undocumented method 2 175:41
Java Inspector 089 Undocumented method 2 191:25
Java Inspector 089 Undocumented method 2 195:41

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.lang.reflect.Array;
26import java.lang.reflect.Method;
27import java.lang.reflect.UndeclaredThrowableException;
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Iterator;
31import java.util.List;
32import java.util.Map;
33
34import biz.hammurapi.convert.ConvertingService;
35
36
37/**
38 * Implements Visitable contract. Subclasses shall implement visitChildren() method.
39 * @author Pavel Vlasov
40 * @version $Revision: 1.4 $
41 */
42public abstract class VisitableBase implements Visitable, Searchable {
43
44 public boolean accept(Visitor visitor) {
45 if (visitor==null) {
46 return false;
47 }
48
49 if (visitor.visit(this)) {
50 acceptChildren(visitor);
51 if (visitor instanceof PoliteVisitor) {
52 ((PoliteVisitor) visitor).leave(this);
53 }
54 return true;
55 }
56
57 return false;
58 }
59
60 public Object find(final Acceptor acceptor) {
61 final Object[] ret = {null};
62 accept(new Visitor() {
63
64 public boolean visit(Object target) {
65 if (ret[0]!=null) {
66 return false;
67 }
68
69 if (acceptor.accept(target)) {
70 ret[0] = target;
71 return false;
72 }
73 return true;
74 }
75
76 });
77 return ret[0];
78 }
79
80 public Collection findAll(final Acceptor acceptor) {
81 final Collection ret = new ArrayList();
82 accept(new Visitor() {
83
84 public boolean visit(Object target) {
85 if (acceptor.accept(target)) {
86 ret.add(target);
87 }
88 return true;
89 }
90
91 });
92 return ret;
93 }
94
95 /**
96 * Override this method to navigate visitor through object constituents.
97 * @param visitor
98 */
99 protected void acceptChildren(Visitor visitor) {
100 // No functionality
101 }
102
103 /**
104 * Properly handles passing visitor to a child - takes into account that child is visitable and visitor may
105 * be an instance of PoliteVisitor.
106 * @param target Object to be visited by the visitor.
107 * @param visitor
108 * @param child
109 */
110 public static void object2visitor(Object target, Visitor visitor) {
111 if (visitor!=null && target!=null) {
112 if (target instanceof Visitable) {
113 ((Visitable) target).accept(visitor);
114 } else if (target instanceof Collection) {
115 if (visitor.visit(target)) {
116 Iterator it = ((Collection) target).iterator();
117 while (it.hasNext()) {
118 object2visitor(it.next(), visitor);
119 }
120 if (visitor instanceof PoliteVisitor) {
121 ((PoliteVisitor) visitor).leave(target);
122 }
123 }
124 } else if (target instanceof Map) {
125 if (visitor.visit(target)) {
126 Iterator it = ((Map) target).entrySet().iterator();
127 while (it.hasNext()) {
128 Map.Entry entry = (Map.Entry) it.next();
129 if (visitor.visit(entry)) {
130 object2visitor(entry.getKey(), visitor);
131 object2visitor(entry.getValue(), visitor);
132 if (visitor instanceof PoliteVisitor) {
133 ((PoliteVisitor) visitor).leave(entry);
134 }
135 }
136 }
137 if (visitor instanceof PoliteVisitor) {
138 ((PoliteVisitor) visitor).leave(target);
139 }
140 }
141 } else if (target.getClass().isArray()) {
142 if (visitor.visit(target)) {
143 for (int i=0, l=Array.getLength(target); i<l; ++i) {
144 object2visitor(Array.get(target, i), visitor);
145 }
146 if (visitor instanceof PoliteVisitor) {
147 ((PoliteVisitor) visitor).leave(target);
148 }
149 }
150 } else {
151 Visitable converted = (Visitable) ConvertingService.convert(target, Visitable.class);
152 if (converted == null) {
153 if (visitor.visit(target) && visitor instanceof PoliteVisitor) {
154 ((PoliteVisitor) visitor).leave(target);
155 }
156 } else {
157 converted.accept(visitor);
158 }
159 }
160 }
161 }
162
163 /**
164 * Wraps object into instance of Searchable
165 * @param visitor
166 * @param child
167 */
168 public static Searchable convert2searchable(final Object object) {
169 return new Searchable() {
170
171 public Object find(final Acceptor acceptor) {
172 final Object[] ret = {null};
173 object2visitor(object, new Visitor() {
174
175 public boolean visit(Object target) {
176 if (ret[0]!=null) {
177 return false;
178 }
179
180 if (acceptor.accept(target)) {
181 ret[0] = target;
182 return false;
183 }
184 return true;
185 }
186
187 });
188 return ret[0];
189 }
190
191 public Collection findAll(final Acceptor acceptor) {
192 final Collection ret = new ArrayList();
193 object2visitor(object, new Visitor() {
194
195 public boolean visit(Object target) {
196 if (acceptor.accept(target)) {
197 ret.add(target);
198 }
199 return true;
200 }
201
202 });
203 return ret;
204 }
205
206 };
207 }
208
209}
210