/**
* Allows any implementation of the HashTreeTraverser interface to easily
* traverse (depth-first) all the nodes of the HashTree. The Traverser
* implementation will be given notification of each node visited.
*
* @see HashTreeTraverser
* @param visitor
* the visitor that wants to traverse the tree
*/
public void traverse(HashTreeTraverser visitor) {
for (Object item : list()) {
visitor.addNode(item, getTree(item));
getTree(item).traverseInto(visitor);
}
}
/**
* The recursive method that accomplishes the tree-traversal and performs
* the callbacks to the HashTreeTraverser.
*
* 完成树遍历和执行的递归方法对HashTreeTraverser的回调。使用深度优先遍历hashTree
*
* @param visitor
* the {@link HashTreeTraverser} to be notified
*/
private void traverseInto(HashTreeTraverser visitor) {
if (list().isEmpty()) {
visitor.processPath();
} else {
for (Object item : list()) {
final HashTree treeItem = getTree(item);
visitor.addNode(item, treeItem);
treeItem.traverseInto(visitor);
}
}
visitor.subtractNode();
}
public class SearchByClass implements HashTreeTraverser {
private final List objectsOfClass = new LinkedList();
private final Map subTrees = new HashMap();
private final Class searchClass;
/**
* Creates an instance of SearchByClass, and sets the Class to be searched
* for.
*
* @param searchClass
* class to be searched for
*/
public SearchByClass(Class searchClass) {
this.searchClass = searchClass;
}
/**
* After traversing the Ha服务器托管网shTree, call this method to get a collection of
* the nodes that were found.
*
* @return Collection All found nodes of the requested type
*/
public Collection getSearchResults() { // TODO specify collection type without breaking callers
return objectsOfClass;
}
/**
* Given a specific found node, this method will return the sub tree of that
* node.
*
* @param root
* the node for which the sub tree is requested
* @return HashTree
*/
public HashTree getSubTree(Object root) {
return subTrees.get(root);
}
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public void addNode(Object node, HashTree subTree) { // 某一层HashTre 的数据访问
if (searchClass.isAssignableFrom(node.getClass())) {
objectsOfClass.add((T) node);
ListedHashTree tree = new ListedHashTree(node);
tree.set(node, subTree);
subTrees.put(node, tree);
}
}
/** {@inheritDoc} */
@Override
public void subtractNode() {
}
/** {@inheritDoc} */
@Override
public void processPath() {
}
}