1 18 package org.apache.activemq.filter; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.Iterator ; 28 29 34 public class DestinationMapNode implements DestinationNode { 35 protected static final String ANY_CHILD = DestinationMap.ANY_CHILD; 36 protected static final String ANY_DESCENDENT = DestinationMap.ANY_DESCENDENT; 37 38 private DestinationMapNode parent; 40 private List values = new ArrayList (); 41 private Map childNodes = new HashMap (); 42 private String path = "Root"; 43 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 60 public DestinationMapNode getChild(String path) { 61 return (DestinationMapNode) childNodes.get(path); 62 } 63 64 67 public Collection getChildren() { 68 return childNodes.values(); 69 } 70 71 public int getChildCount() { 72 return childNodes.size(); 73 } 74 75 79 public DestinationMapNode getChildOrCreate(String 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 92 99 102 public List getValues() { 103 return values; 104 } 105 106 109 public List removeValues() { 110 ArrayList v = new ArrayList (values); 111 values.clear(); 113 pruneIfEmpty(); 114 return v; 115 } 116 117 118 public Set removeDesendentValues() { 119 Set answer = new HashSet (); 120 removeDesendentValues(answer); 121 return answer; 122 } 123 124 protected void removeDesendentValues(Set answer) { 125 answer.addAll(removeValues()); 129 } 130 131 134 public Set getDesendentValues() { 135 Set answer = new HashSet (); 136 appendDescendantValues(answer); 137 return answer; 138 } 139 140 public void add(String [] paths, int idx, Object value) { 141 if (idx >= paths.length) { 142 values.add(value); 143 } 144 else { 145 getChildOrCreate(paths[idx]).add(paths, idx + 1, value); 152 } 153 } 154 155 public void remove(String [] paths, int idx, Object value) { 156 if (idx >= paths.length) { 157 values.remove(value); 158 pruneIfEmpty(); 159 } 160 else { 161 getChildOrCreate(paths[idx]).remove(paths, ++idx, value); 168 } 169 } 170 171 public void removeAll(Set answer, String [] paths, int startIndex) { 172 DestinationNode node = this; 173 for (int i = startIndex, size = paths.length; i < size && node != null; i++) { 174 175 String 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 = 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 answer) { 199 answer.addAll(values); 200 201 Iterator iter = childNodes.values().iterator(); 203 while (iter.hasNext()) { 204 DestinationNode child = (DestinationNode) iter.next(); 205 child.appendDescendantValues(answer); 206 } 207 208 } 213 214 217 protected DestinationMapNode createChildNode() { 218 return new DestinationMapNode(this); 219 } 220 221 224 public void appendMatchingWildcards(Set answer, String [] 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 answer, String [] 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 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 DestinationNode child = node.getChild(ANY_DESCENDENT); 264 if (child != null) { 265 answer.addAll(child.getValues()); 266 } 267 } 268 } 269 } 270 271 public String 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 |