org.omnaest.utils.structure.hierarchy.tree
Class TreeNavigator<T extends Tree<?,TN>,TN extends TreeNode>

java.lang.Object
  extended by org.omnaest.utils.structure.hierarchy.tree.TreeNavigator<T,TN>
Type Parameters:
T - Tree
TN - TreeNode
Direct Known Subclasses:
ObjectTreeNavigator

public class TreeNavigator<T extends Tree<?,TN>,TN extends TreeNode>
extends Object

A TreeNavigator allows to navigate on a given Tree. It will store a path to the current TreeNode, so that it can navigate in a upward direction, too.

The TreeNavigator will use an internal cache to cache the children of the selected TreeNode path. This can be turned of using #se

Author:
Omnaest
See Also:
Tree, TreeNode

Nested Class Summary
protected static class TreeNavigator.LocalAndReducedTraversalControl
          Reduced TreeNavigator.TreeNodeVisitor.TraversalControl which affects the local behavior
protected  class TreeNavigator.TreeNodePathAndCache
          Internal representation of the path of TreeNodes.
protected  class TreeNavigator.TreeNodeTraversal<TNV extends TreeNavigator.TreeNodeVisitor<T,TN>>
          Includes the logic to traverse TreeNodes
static interface TreeNavigator.TreeNodeVisitor<T extends Tree<?,TN>,TN extends TreeNode>
           
 
Field Summary
protected  boolean cachingChildrenOfPathNodes
           
protected  boolean navigationSuccessful
           
protected  T tree
           
protected  TreeNavigator.TreeNodePathAndCache treeNodePathAndCache
          Stores the current path through the TreeNodes.
protected  TreeNavigator.TreeNodeTraversal<TreeNavigator.TreeNodeVisitor<T,TN>> treeNodeTraversal
           
 
Constructor Summary
  TreeNavigator(T tree)
          Creates a TreeNavigator on top of the given Tree which starts with the Tree.getTreeRootNode() as navigation origin.
  TreeNavigator(T tree, boolean cachingChildrenOfPathNodes)
           
protected TreeNavigator(T tree, TreeNavigator.TreeNodePathAndCache treeNodePath, boolean cachingChildrenOfPathNodes)
           
 
Method Summary
 TreeNavigator<T,TN> fork()
          Creates a fork instance of the current TreeNavigator.
 TN getCurrentTreeNode()
          Returns the current TreeNode
 T getTree()
           
 List<TN> getTreeNodePathList()
          Returns a new List instance containing the current path of TreeNodes
 boolean hasChildren()
          Returns true if the current TreeNode.getChildrenList() is not empty
 boolean hasNextSibling()
          Returns true, if the current TreeNode has a next sibling
 boolean hasParent()
          Returns true, if the current TreeNode has a parent
 boolean hasPreviousSibling()
          Returns true, if the current TreeNode has a previous sibling
 boolean isCachingChildrenOfPathNodes()
           
 boolean isNavigationSuccessful()
           
 TreeNavigator<T,TN> navigateToChildAt(int index)
          Tries to navigate to the child of the current TreeNode at the given index position
 TreeNavigator<T,TN> navigateToFirstChild()
          Navigates to the first child of the current TreeNode
 TreeNavigator<T,TN> navigateToLastChild()
          Navigates to the last child of the current TreeNode
 TreeNavigator<T,TN> navigateToNextSibling()
          Tries to navigate to the next TreeNode sibling.
 TreeNavigator<T,TN> navigateToNextSibling(int relativeIndexPosition)
          Tries to navigate to the next TreeNode sibling with the given relative index position.
 TreeNavigator<T,TN> navigateToParent()
          Navigates to the parent TreeNode
 TreeNavigator<T,TN> navigateToPreviousSibling()
          Tries to navigate to the direct previous sibling.
 TreeNavigator<T,TN> navigateToPreviousSibling(int relativeIndexPosition)
          Navigates to the previous sibling using the given relative index position decrement.
 void setCachingChildrenOfPathNodes(boolean cachingChildrenOfPathNodes)
           
 String toString()
           
 TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor.TraversalConfiguration defaultTraversalConfiguration, TreeNavigator.TreeNodeVisitor<T,TN>... treeNodeVisitors)
          Similar to traverse(TreeNodeVisitor...) but allows to specify a default TreeNavigator.TreeNodeVisitor.TraversalConfiguration.
 TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor.TraversalConfiguration defaultTraversalConfiguration, TreeNavigator.TreeNodeVisitor<T,TN> treeNodeVisitor)
          Similar to #traverse(TraversalConfiguration, TreeNodeVisitor)
 TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor<T,TN>... treeNodeVisitors)
          Traverses the current TreeNode and its children transitively invoking TreeNavigator.TreeNodeVisitors for each TreeNode.
 TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor<T,TN> treeNodeVisitor)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

tree

protected final T extends Tree<?,TN> tree

treeNodePathAndCache

protected TreeNavigator.TreeNodePathAndCache treeNodePathAndCache
Stores the current path through the TreeNodes. The last element is the current.


navigationSuccessful

protected boolean navigationSuccessful

cachingChildrenOfPathNodes

protected boolean cachingChildrenOfPathNodes

treeNodeTraversal

protected TreeNavigator.TreeNodeTraversal<TreeNavigator.TreeNodeVisitor<T extends Tree<?,TN>,TN extends TreeNode>> treeNodeTraversal
Constructor Detail

TreeNavigator

public TreeNavigator(T tree)
Creates a TreeNavigator on top of the given Tree which starts with the Tree.getTreeRootNode() as navigation origin.

Parameters:
tree -
Throws:
IllegalArgumentException - if the Tree reference is null
See Also:
TreeNavigator, Tree, TreeNode

TreeNavigator

protected TreeNavigator(T tree,
                        TreeNavigator.TreeNodePathAndCache treeNodePath,
                        boolean cachingChildrenOfPathNodes)
Parameters:
tree -
treeNodePath -
cachingChildrenOfPathNodes -
See Also:
TreeNavigator

TreeNavigator

public TreeNavigator(T tree,
                     boolean cachingChildrenOfPathNodes)
Parameters:
tree -
cachingChildrenOfPathNodes -
See Also:
TreeNavigator
Method Detail

fork

public TreeNavigator<T,TN> fork()
Creates a fork instance of the current TreeNavigator. Any navigation actions to the fork will not affect the current TreeNavigator instance and vice versa.

Returns:

navigateToFirstChild

public TreeNavigator<T,TN> navigateToFirstChild()
Navigates to the first child of the current TreeNode

Returns:
this

navigateToLastChild

public TreeNavigator<T,TN> navigateToLastChild()
Navigates to the last child of the current TreeNode

Returns:
this

navigateToChildAt

public TreeNavigator<T,TN> navigateToChildAt(int index)
Tries to navigate to the child of the current TreeNode at the given index position

Parameters:
index -
Returns:
this

navigateToNextSibling

public TreeNavigator<T,TN> navigateToNextSibling(int relativeIndexPosition)
Tries to navigate to the next TreeNode sibling with the given relative index position. A relative index position of 0 means the current TreeNode, a relative index position of 1 means the direct next one,...

Parameters:
relativeIndexPosition -
Returns:
this
See Also:
isNavigationSuccessful(), navigateToNextSibling(), navigateToPreviousSibling(int), navigateToPreviousSibling()

navigateToNextSibling

public TreeNavigator<T,TN> navigateToNextSibling()
Tries to navigate to the next TreeNode sibling.

Returns:
this
See Also:
isNavigationSuccessful(), navigateToPreviousSibling(), navigateToNextSibling(int)

navigateToPreviousSibling

public TreeNavigator<T,TN> navigateToPreviousSibling()
Tries to navigate to the direct previous sibling.

Returns:
this
See Also:
isNavigationSuccessful(), navigateToNextSibling(int), navigateToPreviousSibling(int)

navigateToPreviousSibling

public TreeNavigator<T,TN> navigateToPreviousSibling(int relativeIndexPosition)
Navigates to the previous sibling using the given relative index position decrement. A relative index position of 0 means the current TreeNode, a relative index position of 1 means the direct previous TreeNode

Parameters:
relativeIndexPosition -
Returns:
this
See Also:
isNavigationSuccessful(), navigateToPreviousSibling(), navigateToNextSibling(), navigateToNextSibling(int)

hasNextSibling

public boolean hasNextSibling()
Returns true, if the current TreeNode has a next sibling

Returns:

hasPreviousSibling

public boolean hasPreviousSibling()
Returns true, if the current TreeNode has a previous sibling

Returns:

navigateToParent

public TreeNavigator<T,TN> navigateToParent()
Navigates to the parent TreeNode

Returns:
this

hasParent

public boolean hasParent()
Returns true, if the current TreeNode has a parent

Returns:

hasChildren

public boolean hasChildren()
Returns true if the current TreeNode.getChildrenList() is not empty

Returns:

traverse

public TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor<T,TN>... treeNodeVisitors)
Traverses the current TreeNode and its children transitively invoking TreeNavigator.TreeNodeVisitors for each TreeNode.

As default the TreeNavigator.TreeNodeVisitor.TraversalControl.GO_ON is used for the case the TreeNavigator.TreeNodeVisitor returns null.
If multiple TreeNavigator.TreeNodeVisitor instances are given and they return different TreeNavigator.TreeNodeVisitor.TraversalControl results they are grouped and a fork() is used for the different groups.

Nodes which are already visited will be excluded by default. This prevents indefinite loops related to cyclic references. To override this use TreeNavigator.TreeNodeVisitor.TraversalControl.GO_ON_INCLUDE_ALREADY_TRAVERSED_NODES in combination with #traverse(TraversalConfiguration, TreeNodeVisitor)

Parameters:
treeNodeVisitors - TreeNavigator.TreeNodeVisitor
Returns:
this
See Also:
#traverse(TraversalConfiguration, TreeNodeVisitor)

traverse

public TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor<T,TN> treeNodeVisitor)
Parameters:
treeNodeVisitor -
Returns:
See Also:
traverse(TreeNodeVisitor...)

traverse

public TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor.TraversalConfiguration defaultTraversalConfiguration,
                                    TreeNavigator.TreeNodeVisitor<T,TN> treeNodeVisitor)
Similar to #traverse(TraversalConfiguration, TreeNodeVisitor)

Parameters:
defaultTraversalConfiguration -
treeNodeVisitor -
Returns:

traverse

public TreeNavigator<T,TN> traverse(TreeNavigator.TreeNodeVisitor.TraversalConfiguration defaultTraversalConfiguration,
                                    TreeNavigator.TreeNodeVisitor<T,TN>... treeNodeVisitors)
Similar to traverse(TreeNodeVisitor...) but allows to specify a default TreeNavigator.TreeNodeVisitor.TraversalConfiguration.

The default TreeNavigator.TreeNodeVisitor.TraversalConfiguration is used initially for all TreeNavigator.TreeNodeVisitor instances. If any TreeNavigator.TreeNodeVisitor instance returns a TreeNavigator.TreeNodeVisitor.TraversalControl flag, the TreeNavigator.TreeNodeVisitor.TraversalConfiguration of the particular node is adapted while the other TreeNavigator.TreeNodeVisitor will keep their TreeNavigator.TreeNodeVisitor.TraversalConfiguration.

The TreeNavigator.TreeNodeVisitor.TraversalConfiguration defines which nodes are traversed.

Parameters:
defaultTraversalConfiguration - TreeNavigator.TreeNodeVisitor.TraversalConfiguration
treeNodeVisitors - TreeNavigator.TreeNodeVisitor
Returns:
this
See Also:
traverse(TreeNodeVisitor...)

isNavigationSuccessful

public boolean isNavigationSuccessful()
Returns:
the navigationSuccessful

getCurrentTreeNode

public TN getCurrentTreeNode()
Returns the current TreeNode

Returns:

toString

public String toString()
Overrides:
toString in class Object

isCachingChildrenOfPathNodes

public boolean isCachingChildrenOfPathNodes()
Returns:
the cachingChildrenOfPathNodes

setCachingChildrenOfPathNodes

public void setCachingChildrenOfPathNodes(boolean cachingChildrenOfPathNodes)
Parameters:
cachingChildrenOfPathNodes - the cachingChildrenOfPathNodes to set

getTree

public T getTree()
Returns:
the tree

getTreeNodePathList

public List<TN> getTreeNodePathList()
Returns a new List instance containing the current path of TreeNodes

Returns:


Copyright © 2013. All Rights Reserved.