KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > filter > DestinationMapNode


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.filter;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * An implementation class used to implement {@link DestinationMap}
31  *
32  * @version $Revision: 1.2 $
33  */

34 public class DestinationMapNode implements DestinationNode {
35     protected static final String JavaDoc ANY_CHILD = DestinationMap.ANY_CHILD;
36     protected static final String JavaDoc ANY_DESCENDENT = DestinationMap.ANY_DESCENDENT;
37
38     // we synchornize at the DestinationMap level
39
private DestinationMapNode parent;
40     private List JavaDoc values = new ArrayList JavaDoc();
41     private Map JavaDoc childNodes = new HashMap JavaDoc();
42     private String JavaDoc path = "Root";
43 // private DestinationMapNode anyChild;
44
private int pathLength;
45
46     public DestinationMapNode(DestinationMapNode parent) {
47         this.parent = parent;
48         if (parent == null) {
49             pathLength = 0;
50         }
51         else {
52             pathLength = parent.pathLength + 1;
53         }
54     }
55
56     /**
57      * Returns the child node for the given named path or null if it does not
58      * exist
59      */

60     public DestinationMapNode getChild(String JavaDoc path) {
61         return (DestinationMapNode) childNodes.get(path);
62     }
63
64     /**
65      * Returns the child nodes
66      */

67     public Collection JavaDoc getChildren() {
68         return childNodes.values();
69     }
70     
71     public int getChildCount() {
72         return childNodes.size();
73     }
74
75     /**
76      * Returns the child node for the given named path, lazily creating one if
77      * it does not yet exist
78      */

79     public DestinationMapNode getChildOrCreate(String JavaDoc path) {
80         DestinationMapNode answer = (DestinationMapNode) childNodes.get(path);
81         if (answer == null) {
82             answer = createChildNode();
83             answer.path = path;
84             childNodes.put(path, answer);
85         }
86         return answer;
87     }
88
89     /**
90      * Returns the node which represents all children (i.e. the * node)
91      */

92 // public DestinationMapNode getAnyChildNode() {
93
// if (anyChild == null) {
94
// anyChild = createChildNode();
95
// }
96
// return anyChild;
97
// }
98

99     /**
100      * Returns a mutable List of the values available at this node in the tree
101      */

102     public List JavaDoc getValues() {
103         return values;
104     }
105
106     /**
107      * Returns a mutable List of the values available at this node in the tree
108      */

109     public List JavaDoc removeValues() {
110         ArrayList JavaDoc v = new ArrayList JavaDoc(values);
111 // parent.getAnyChildNode().getValues().removeAll(v);
112
values.clear();
113         pruneIfEmpty();
114         return v;
115     }
116     
117     
118     public Set JavaDoc removeDesendentValues() {
119         Set JavaDoc answer = new HashSet JavaDoc();
120         removeDesendentValues(answer);
121         return answer;
122     }
123     
124     protected void removeDesendentValues(Set JavaDoc answer) {
125 // if (anyChild != null) {
126
// anyChild.removeDesendentValues(answer);
127
// }
128
answer.addAll(removeValues());
129     }
130
131     /**
132      * Returns a list of all the values from this node down the tree
133      */

134     public Set JavaDoc getDesendentValues() {
135         Set JavaDoc answer = new HashSet JavaDoc();
136         appendDescendantValues(answer);
137         return answer;
138     }
139
140     public void add(String JavaDoc[] paths, int idx, Object JavaDoc value) {
141         if (idx >= paths.length) {
142             values.add(value);
143         }
144         else {
145 // if (idx == paths.length - 1) {
146
// getAnyChildNode().getValues().add(value);
147
// }
148
// else {
149
// getAnyChildNode().add(paths, idx + 1, value);
150
// }
151
getChildOrCreate(paths[idx]).add(paths, idx + 1, value);
152         }
153     }
154
155     public void remove(String JavaDoc[] paths, int idx, Object JavaDoc value) {
156         if (idx >= paths.length) {
157             values.remove(value);
158             pruneIfEmpty();
159         }
160         else {
161 // if (idx == paths.length - 1) {
162
// getAnyChildNode().getValues().remove(value);
163
// }
164
// else {
165
// getAnyChildNode().remove(paths, idx + 1, value);
166
// }
167
getChildOrCreate(paths[idx]).remove(paths, ++idx, value);
168         }
169     }
170
171     public void removeAll(Set JavaDoc answer, String JavaDoc[] paths, int startIndex) {
172         DestinationNode node = this;
173         for (int i = startIndex, size = paths.length; i < size && node != null; i++) {
174
175             String JavaDoc path = paths[i];
176             if (path.equals(ANY_DESCENDENT)) {
177                 answer.addAll(node.removeDesendentValues());
178                 break;
179             }
180
181             node.appendMatchingWildcards(answer, paths, i);
182             if (path.equals(ANY_CHILD)) {
183                 //node = node.getAnyChildNode();
184
node = new AnyChildDestinationNode(node);
185             }
186             else {
187                 node = node.getChild(path);
188             }
189         }
190         
191         if (node != null) {
192             answer.addAll(node.removeValues());
193         }
194
195         
196     }
197
198     public void appendDescendantValues(Set JavaDoc answer) {
199         answer.addAll(values);
200
201         // lets add all the children too
202
Iterator JavaDoc iter = childNodes.values().iterator();
203         while (iter.hasNext()) {
204             DestinationNode child = (DestinationNode) iter.next();
205             child.appendDescendantValues(answer);
206         }
207         
208         // TODO???
209
// if (anyChild != null) {
210
// anyChild.appendDescendantValues(answer);
211
// }
212
}
213
214     /**
215      * Factory method to create a child node
216      */

217     protected DestinationMapNode createChildNode() {
218         return new DestinationMapNode(this);
219     }
220
221     /**
222      * Matches any entries in the map containing wildcards
223      */

224     public void appendMatchingWildcards(Set JavaDoc answer, String JavaDoc[] paths, int idx) {
225         if (idx - 1 > pathLength) {
226             return;
227         }
228         DestinationMapNode wildCardNode = getChild(ANY_CHILD);
229         if (wildCardNode != null) {
230             wildCardNode.appendMatchingValues(answer, paths, idx+1);
231         }
232         wildCardNode = getChild(ANY_DESCENDENT);
233         if (wildCardNode != null) {
234             answer.addAll(wildCardNode.getDesendentValues());
235         }
236     }
237
238     public void appendMatchingValues(Set JavaDoc answer, String JavaDoc[] paths, int startIndex) {
239         DestinationNode node = this;
240         boolean couldMatchAny = true;
241         for (int i = startIndex, size = paths.length; i < size && node != null; i++) {
242             String JavaDoc path = paths[i];
243             if (path.equals(ANY_DESCENDENT)) {
244                 answer.addAll(node.getDesendentValues());
245                 couldMatchAny = false;
246                 break;
247             }
248
249             node.appendMatchingWildcards(answer, paths, i);
250
251
252             if (path.equals(ANY_CHILD)) {
253                 node = new AnyChildDestinationNode(node);
254             }
255             else {
256                 node = node.getChild(path);
257             }
258         }
259         if (node != null) {
260             answer.addAll(node.getValues());
261             if (couldMatchAny) {
262                 // lets allow FOO.BAR to match the FOO.BAR.> entry in the map
263
DestinationNode child = node.getChild(ANY_DESCENDENT);
264                 if (child != null) {
265                     answer.addAll(child.getValues());
266                 }
267             }
268         }
269     }
270
271     public String JavaDoc getPath() {
272         return path;
273     }
274
275     protected void pruneIfEmpty() {
276         if (parent != null && childNodes.isEmpty() && values.isEmpty()) {
277             parent.removeChild(this);
278         }
279     }
280
281     protected void removeChild(DestinationMapNode node) {
282         childNodes.remove(node.getPath());
283         pruneIfEmpty();
284     }
285 }
286
Popular Tags