001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.collections;
020:
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: /**
028: * Useful for finding all nodes in the tree that represent objects of a
029: * particular type. For instance, if your tree contains all strings, and a few
030: * StringBuffer objects, you can use the SearchByClass traverser to find all the
031: * StringBuffer objects in your tree.
032: * <p>
033: * Usage is simple. Given a {@link HashTree} object "tree", and a SearchByClass
034: * object:
035: *
036: * <pre>
037: * HashTree tree = new HashTree();
038: * // ... tree gets filled with objects
039: * SearchByClass searcher = new SearchByClass(StringBuffer.class);
040: * tree.traverse(searcher);
041: * Iterator iter = searcher.getSearchResults().iterator();
042: * while (iter.hasNext()) {
043: * StringBuffer foundNode = (StringBuffer) iter.next();
044: * HashTree subTreeOfFoundNode = searcher.getSubTree(foundNode);
045: * // .... do something with node and subTree...
046: * }
047: * </pre>
048: *
049: * @see HashTree
050: * @see HashTreeTraverser
051: *
052: * @author Michael Stover (mstover1 at apache.org)
053: * @version $Revision: 493784 $
054: */
055: public class SearchByClass implements HashTreeTraverser {
056: List objectsOfClass = new LinkedList();
057:
058: Map subTrees = new HashMap();
059:
060: Class searchClass = null;
061:
062: /**
063: * Creates an instance of SearchByClass. However, without setting the Class
064: * to search for, it will be a useless object.
065: */
066: public SearchByClass() {
067: }
068:
069: /**
070: * Creates an instance of SearchByClass, and sets the Class to be searched
071: * for.
072: *
073: * @param searchClass
074: */
075: public SearchByClass(Class searchClass) {
076: this .searchClass = searchClass;
077: }
078:
079: /**
080: * After traversing the HashTree, call this method to get a collection of
081: * the nodes that were found.
082: *
083: * @return Collection All found nodes of the requested type
084: */
085: public Collection getSearchResults() {
086: return objectsOfClass;
087: }
088:
089: /**
090: * Given a specific found node, this method will return the sub tree of that
091: * node.
092: *
093: * @param root
094: * the node for which the sub tree is requested
095: * @return HashTree
096: */
097: public HashTree getSubTree(Object root) {
098: return (HashTree) subTrees.get(root);
099: }
100:
101: public void addNode(Object node, HashTree subTree) {
102: if (searchClass.isAssignableFrom(node.getClass())) {
103: objectsOfClass.add(node);
104: ListedHashTree tree = new ListedHashTree(node);
105: tree.set(node, subTree);
106: subTrees.put(node, tree);
107: }
108: }
109:
110: public void subtractNode() {
111: }
112:
113: public void processPath() {
114: }
115: }
|