1 18 19 package org.apache.jorphan.collections; 20 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 55 public class SearchByClass implements HashTreeTraverser 56 { 57 List objectsOfClass = new LinkedList (); 58 Map subTrees = new HashMap (); 59 Class searchClass = null; 60 61 65 public SearchByClass() 66 { 67 } 68 69 75 public SearchByClass(Class searchClass) 76 { 77 this.searchClass = searchClass; 78 } 79 80 86 public Collection getSearchResults() 87 { 88 return objectsOfClass; 89 } 90 91 98 public HashTree getSubTree(Object root) 99 { 100 return (HashTree) subTrees.get(root); 101 } 102 103 public void addNode(Object node, HashTree subTree) 104 { 105 if (searchClass.isAssignableFrom(node.getClass())) 106 { 107 objectsOfClass.add(node); 108 ListedHashTree tree = new ListedHashTree(node); 109 tree.set(node, subTree); 110 subTrees.put(node, tree); 111 } 112 } 113 114 public static class Test extends junit.framework.TestCase 115 { 116 public Test(String name) 117 { 118 super(name); 119 } 120 public void testSearch() throws Exception 121 { 122 ListedHashTree tree = new ListedHashTree(); 123 SearchByClass searcher = new SearchByClass(Integer .class); 124 String one = "one"; 125 String two = "two"; 126 Integer o = new Integer (1); 127 tree.add(one, o); 128 tree.getTree(one).add(o, two); 129 tree.traverse(searcher); 130 assertEquals(1, searcher.getSearchResults().size()); 131 } 132 } 133 134 public void subtractNode() 135 { 136 } 137 138 public void processPath() 139 { 140 } 141 } | Popular Tags |