qflib 0.98.1

de.qfs.lib.tree
Class TreeUtil

java.lang.Object
  |
  +--de.qfs.lib.tree.TreeUtil

public class TreeUtil
extends java.lang.Object

This is a collection of static convenience methods that are useful for tree management.

All methods operate on trees in terms of a TreeAdapter.

Since:
0.98.1
Version:
$Revision: 1.2 $
Author:
Gregor Schmid

Constructor Summary
TreeUtil()
           
 
Method Summary
static java.lang.Object copyTree(MutableTreeAdapter adapter, java.lang.Object node)
          Copy a whole (sub)tree.
static java.lang.Object getFollowingNode(TreeAdapter adapter, java.lang.Object node)
          Get the following node in document order.
static java.lang.Object getFollowingNode(TreeAdapter adapter, java.lang.Object node, java.lang.Object limit)
          Get the following node in document order.
static java.lang.Object getLastDescendant(TreeAdapter adapter, java.lang.Object node)
          Get the last descendant of a node.
static java.lang.Object[] getPath(TreeAdapter adapter, java.lang.Object node)
          Get the path from a node's root to the node.
static java.lang.Object[] getPath(TreeAdapter adapter, java.lang.Object node, java.lang.Object root)
          Get the relative path from some root to a node.
static java.lang.Object getPrecedingNode(TreeAdapter adapter, java.lang.Object node)
          Get the preceding node in document order.
static java.lang.Object getPrecedingNode(TreeAdapter adapter, java.lang.Object node, java.lang.Object limit)
          Get the preceding node in document order.
static java.lang.Object getRoot(TreeAdapter adapter, java.lang.Object node)
          Get the root of a tree hierarchy, i.e. the first ancestor of node that has a null parent.
static java.lang.Object traverse(TreeAdapter adapter, java.lang.Object node, TraversalCallback callback)
          Traverse a tree and notify a Callback about every node entered and exited on the way.
static java.lang.Object traverseFrom(TreeAdapter adapter, java.lang.Object node, java.lang.Object start, TraversalCallback callback)
          Traverse a tree and notify a Callback about every node entered and exited on the way.
static java.lang.Object visitTree(TreeAdapter adapter, java.lang.Object visitor, java.lang.Object node)
          Visit every node in a tree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TreeUtil

public TreeUtil()
Method Detail

getRoot

public static java.lang.Object getRoot(TreeAdapter adapter,
                                       java.lang.Object node)
Get the root of a tree hierarchy, i.e. the first ancestor of node that has a null parent.
Parameters:
adapter - The tree hierarchy adapter.
node - The node to start the search from.
Returns:
The root of the hierarchy.

getPath

public static java.lang.Object[] getPath(TreeAdapter adapter,
                                         java.lang.Object node)
Get the path from a node's root to the node.
Parameters:
adapter - The tree hierarchy adapter.
node - The node whose path to get.
Returns:
An array of the node's ancestors with the root as its first and the node as its last element.

getPath

public static java.lang.Object[] getPath(TreeAdapter adapter,
                                         java.lang.Object node,
                                         java.lang.Object root)
Get the relative path from some root to a node.
Parameters:
adapter - The tree hierarchy adapter.
node - The node whose path to get.
root - The node to stop the search at
Returns:
An array of the node's ancestors with root as its first and the node as its last element. If root is not an ancestor of node, null is returned.

getFollowingNode

public static java.lang.Object getFollowingNode(TreeAdapter adapter,
                                                java.lang.Object node)
Get the following node in document order.
Parameters:
adapter - The tree hierarchy adapter.
node - The node to start from.
Returns:
The following node or null if it is the last child.

getPrecedingNode

public static java.lang.Object getPrecedingNode(TreeAdapter adapter,
                                                java.lang.Object node)
Get the preceding node in document order.
Parameters:
adapter - The tree hierarchy adapter.
node - The node to start from.
Returns:
The preceding node or null if it is the root node.

getFollowingNode

public static java.lang.Object getFollowingNode(TreeAdapter adapter,
                                                java.lang.Object node,
                                                java.lang.Object limit)
Get the following node in document order.
Parameters:
adapter - The tree hierarchy adapter.
node - The node to start from.
limit - Limit the search to this node and its descendants.
Returns:
The following node or null if it is the last child.

getPrecedingNode

public static java.lang.Object getPrecedingNode(TreeAdapter adapter,
                                                java.lang.Object node,
                                                java.lang.Object limit)
Get the preceding node in document order.
Parameters:
adapter - The tree hierarchy adapter.
node - The node to start from.
limit - Limit the search to this node and its descendants.
Returns:
The preceding node or null if it is the root node.

getLastDescendant

public static java.lang.Object getLastDescendant(TreeAdapter adapter,
                                                 java.lang.Object node)
Get the last descendant of a node.
Parameters:
adapter - The tree hierarchy adapter.
node - The node to start from.
Returns:
The last descendant or the node itself it it doesn't have children.

copyTree

public static java.lang.Object copyTree(MutableTreeAdapter adapter,
                                        java.lang.Object node)
Copy a whole (sub)tree.
Parameters:
adapter - The tree hierarchy adapter.
node - The root node of the (sub)tree to copy.
Returns:
The copy of the tree.

traverse

public static java.lang.Object traverse(TreeAdapter adapter,
                                        java.lang.Object node,
                                        TraversalCallback callback)
Traverse a tree and notify a Callback about every node entered and exited on the way.

External synchronization is required if multithreaded access to the tree is possible.

Parameters:
adapter - The tree hierarchy adapter.
node - The node to start the traversal at.
callback - The Callback to notify.
Returns:
This method may also be used to search for something in a tree. The callback method may abort the traversal early by throwing a ReturnFromTraversalException. In that case the return value will be taken from the exception. Otherwise null is returned.

traverseFrom

public static java.lang.Object traverseFrom(TreeAdapter adapter,
                                            java.lang.Object node,
                                            java.lang.Object start,
                                            TraversalCallback callback)
Traverse a tree and notify a Callback about every node entered and exited on the way.

External synchronization is required if multithreaded access to the tree is possible.

Parameters:
adapter - The tree hierarchy adapter.
node - The root node for the traversal.
start - The node to start the traversal at. All nodes befor this one will be skipped. Must be a descendant of node, or nothing is traversed.
callback - The Callback to notify.
Returns:
This method may also be used to search for something in a tree. The callback method may abort the traversal early by throwing a ReturnFromTraversalException. In that case the return value will be taken from the exception. Otherwise null is returned.

visitTree

public static java.lang.Object visitTree(TreeAdapter adapter,
                                         java.lang.Object visitor,
                                         java.lang.Object node)
Visit every node in a tree.
Parameters:
adapter - The tree hierarchy adapter.
visitor - The visitor that visits the nodes.
node - The node to start at.
Returns:
This method may also be used to search for something in a tree. The visitor may abort the traversal early by throwing a ReturnFromTraversalException. In that case the return value will be taken from the exception. Otherwise null is returned.

qflib 0.98.1