KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > collections > SearchByClass


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/collections/SearchByClass.java,v 1.4 2004/02/11 23:46:32 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.collections;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * Useful for finding all nodes in the tree that represent objects of a
29  * particular type. For instance, if your tree contains all strings, and a few
30  * StringBuffer objects, you can use the SearchByClass traverser to find all
31  * the StringBuffer objects in your tree.
32  * <p>
33  * Usage is simple. Given a {@link HashTree} object "tree", and a
34  * SearchByClass object:
35  * <pre>
36  * HashTree tree = new HashTree();
37  * // ... tree gets filled with objects
38  * SearchByClass searcher = new SearchByClass(StringBuffer.class);
39  * tree.traverse(searcher);
40  * Iterator iter = searcher.getSearchResults().iterator();
41  * while(iter.hasNext())
42  * {
43  * StringBuffer foundNode = (StringBuffer)iter.next();
44  * HashTree subTreeOfFoundNode = searcher.getSubTree(foundNode);
45  * // .... do something with node and subTree...
46  * }
47  * </pre>
48  *
49  * @see HashTree
50  * @see HashTreeTraverser
51  *
52  * @author Michael Stover (mstover1 at apache.org)
53  * @version $Revision: 1.4 $
54  */

55 public class SearchByClass implements HashTreeTraverser
56 {
57     List JavaDoc objectsOfClass = new LinkedList JavaDoc();
58     Map JavaDoc subTrees = new HashMap JavaDoc();
59     Class JavaDoc searchClass = null;
60     
61     /**
62      * Creates an instance of SearchByClass. However, without setting the
63      * Class to search for, it will be a useless object.
64      */

65     public SearchByClass()
66     {
67     }
68
69     /**
70      * Creates an instance of SearchByClass, and sets the Class to be searched
71      * for.
72      *
73      * @param searchClass
74      */

75     public SearchByClass(Class JavaDoc searchClass)
76     {
77         this.searchClass = searchClass;
78     }
79     
80     /**
81      * After traversing the HashTree, call this method to get a collection of
82      * the nodes that were found.
83      *
84      * @return Collection All found nodes of the requested type
85      */

86     public Collection JavaDoc getSearchResults()
87     {
88         return objectsOfClass;
89     }
90     
91     /**
92      * Given a specific found node, this method will return the sub tree of
93      * that node.
94      *
95      * @param root the node for which the sub tree is requested
96      * @return HashTree
97      */

98     public HashTree getSubTree(Object JavaDoc root)
99     {
100         return (HashTree) subTrees.get(root);
101     }
102     
103     public void addNode(Object JavaDoc 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 JavaDoc name)
117         {
118             super(name);
119         }
120         public void testSearch() throws Exception JavaDoc
121         {
122             ListedHashTree tree = new ListedHashTree();
123             SearchByClass searcher = new SearchByClass(Integer JavaDoc.class);
124             String JavaDoc one = "one";
125             String JavaDoc two = "two";
126             Integer JavaDoc o = new Integer JavaDoc(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