Class NodeTreeWalker
java.lang.Object
org.apache.commons.configuration2.tree.NodeTreeWalker
A class providing different algorithms for traversing a hierarchy of configuration nodes.
The methods provided by this class accept a ConfigurationNodeVisitor
and visit all nodes in a hierarchy
starting from a given root node. Because a NodeHandler
has to be passed in, too, arbitrary types of nodes can
be processed. The walk()
methods differ in the order in which nodes are visited. Details can be found in the
method documentation.
An instance of this class does not define any state; therefore, it can be shared and used concurrently. The
INSTANCE
member field can be used for accessing a default instance. If desired (e.g. for testing purposes),
new instances can be created.
- Since:
- 2.0
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T> void
bfs
(T root, ConfigurationNodeVisitor<T> visitor, NodeHandler<T> handler) Helper method for performing a BFS traversal.private static <T> boolean
checkParameters
(T root, ConfigurationNodeVisitor<T> visitor, NodeHandler<T> handler) Helper method for checking the parameters for the walk() methods.private static <T> void
dfs
(T node, ConfigurationNodeVisitor<T> visitor, NodeHandler<T> handler) Recursive helper method for performing a DFS traversal.<T> void
walkBFS
(T root, ConfigurationNodeVisitor<T> visitor, NodeHandler<T> handler) Visits all nodes in the hierarchy represented by the given root node in breadth first search manner.<T> void
walkDFS
(T root, ConfigurationNodeVisitor<T> visitor, NodeHandler<T> handler) Visits all nodes in the hierarchy represented by the given root node in depth first search manner.
-
Field Details
-
INSTANCE
The default instance of this class.
-
-
Constructor Details
-
NodeTreeWalker
public NodeTreeWalker()
-
-
Method Details
-
bfs
Helper method for performing a BFS traversal. Implementation node: This method organizes the nodes to be visited in structures on the heap. Therefore, it can deal with larger structures than would be the case in a recursive approach (where the stack size limits the size of the structures which can be traversed).- Type Parameters:
T
- the type of the nodes involved- Parameters:
root
- the root node to be navigatedvisitor
- the visitorhandler
- the handler
-
checkParameters
private static <T> boolean checkParameters(T root, ConfigurationNodeVisitor<T> visitor, NodeHandler<T> handler) Helper method for checking the parameters for the walk() methods. If mandatory parameters are missing, an exception is thrown. The return value indicates whether an operation can be performed.- Type Parameters:
T
- the type of the nodes involved- Parameters:
root
- the root nodevisitor
- the visitorhandler
- the handler- Returns:
- true if a walk operation can be performed, false otherwise
- Throws:
IllegalArgumentException
- if a required parameter is missing
-
dfs
Recursive helper method for performing a DFS traversal.- Type Parameters:
T
- the type of the nodes involved- Parameters:
node
- the current nodevisitor
- the visitorhandler
- the handler
-
walkBFS
Visits all nodes in the hierarchy represented by the given root node in breadth first search manner. This means that the nodes are visited in an order corresponding to the distance from the root node: first the root node is visited, then all direct children of the root node, then all direct children of the first child of the root node, etc. In this mode of traversal, there is no direct connection between the encounter of a node and its children. Therefore, on the visitor object only thevisitBeforeChildren()
method gets called!.- Type Parameters:
T
- the type of the nodes involved- Parameters:
root
- the root node of the hierarchy to be processed (may be null, then this call has no effect)visitor
- theConfigurationNodeVisitor
(must not be null)handler
- theNodeHandler
(must not be null)- Throws:
IllegalArgumentException
- if a required parameter is null
-
walkDFS
Visits all nodes in the hierarchy represented by the given root node in depth first search manner. This means that firstConfigurationNodeVisitor.visitBeforeChildren(Object, NodeHandler)
is called on a node, then recursively all of its children are processed, and eventuallyConfigurationNodeVisitor.visitAfterChildren(Object, NodeHandler)
gets invoked.- Type Parameters:
T
- the type of the nodes involved- Parameters:
root
- the root node of the hierarchy to be processed (may be null, then this call has no effect)visitor
- theConfigurationNodeVisitor
(must not be null)handler
- theNodeHandler
(must not be null)- Throws:
IllegalArgumentException
- if a required parameter is null
-