Class SearchByClass<T>

  • Type Parameters:
    T - Class that should be searched for
    All Implemented Interfaces:
    HashTreeTraverser

    public class SearchByClass<T>
    extends Object
    implements HashTreeTraverser
    Useful for finding all nodes in the tree that represent objects of a particular type. For instance, if your tree contains all strings, and a few StringBuilder objects, you can use the SearchByClass traverser to find all the StringBuilder objects in your tree.

    Usage is simple. Given a HashTree object "tree", and a SearchByClass object:

     HashTree tree = new HashTree();
     // ... tree gets filled with objects
     SearchByClass searcher = new SearchByClass(StringBuilder.class);
     tree.traverse(searcher);
     Iterator iter = searcher.getSearchResults().iterator();
     while (iter.hasNext()) {
         StringBuilder foundNode = (StringBuilder) iter.next();
         HashTree subTreeOfFoundNode = searcher.getSubTree(foundNode);
         // .... do something with node and subTree...
     }
     
    Version:
    $Revision: 1648229 $
    See Also:
    HashTree, HashTreeTraverser
    • Constructor Detail

      • SearchByClass

        public SearchByClass​(Class<T> searchClass)
        Creates an instance of SearchByClass, and sets the Class to be searched for.
        Parameters:
        searchClass - class to be searched for
    • Method Detail

      • getSearchResults

        public Collection<T> getSearchResults()
        After traversing the HashTree, call this method to get a collection of the nodes that were found.
        Returns:
        Collection All found nodes of the requested type
      • getSubTree

        public HashTree getSubTree​(Object root)
        Given a specific found node, this method will return the sub tree of that node.
        Parameters:
        root - the node for which the sub tree is requested
        Returns:
        HashTree
      • addNode

        public void addNode​(Object node,
                            HashTree subTree)
        The tree traverses itself depth-first, calling addNode for each object it encounters as it goes. This is a callback method, and should not be called except by a HashTree during traversal.
        Specified by:
        addNode in interface HashTreeTraverser
        Parameters:
        node - the node currently encountered
        subTree - the HashTree under the node encountered
      • subtractNode

        public void subtractNode()
        Indicates traversal has moved up a step, and the visitor should remove the top node from its stack structure. This is a callback method, and should not be called except by a HashTree during traversal.
        Specified by:
        subtractNode in interface HashTreeTraverser
      • processPath

        public void processPath()
        Process path is called when a leaf is reached. If a visitor wishes to generate Lists of path elements to each leaf, it should keep a Stack data structure of nodes passed to it with addNode, and removing top items for every HashTreeTraverser.subtractNode() call. This is a callback method, and should not be called except by a HashTree during traversal.
        Specified by:
        processPath in interface HashTreeTraverser