Class AbstractHierarchy<T>
- All Implemented Interfaces:
Hierarchy<T>
,Serializable
- Direct Known Subclasses:
SimpleHierarchy
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addHierarchyListener
(HierarchyListener<T> listener) void
addWeakHierarchyListener
(HierarchyListener<T> listener) breadthFirstIterator
(T parent) Creates and returns an iterable that traverses the subhierarchy rooted at the give node in breadth-first order.depthFirstIterator
(T parent) Creates and returns an iterable that traverses the subhierarchy rooted at the give node in depth-first order.getChildAfter
(T parent, T child) Returns the child in this node's child array that immediately followsaChild
, which must be a child of this node.getChildBefore
(T parent, T child) Returns the child in this node's child array that immediately precedesaChild
, which must be a child of this node.int
getDepth()
Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf.getFirstChild
(T node) Returns this node's first child.getFirstLeaf
(T node) Finds and returns the first leaf that is a descendant of this node -- either this node or its first child's first leaf.getLastChild
(T node) Returns this node's last child.getLastLeaf
(T node) Finds and returns the last leaf that is a descendant of this node -- either this node or its last child's last leaf.int
getLeafCount
(T node) Returns the total number of leaves that are descendants of this node.int
Returns the number of levels above this node -- the distance from the root to this node.getNextLeaf
(T node) Returns the leaf after this node or null if this node is the last leaf in the tree.getNextSibling
(T parent, T child) Returns the next sibling of this node in the parent's children array.Returns the path from the root, to get to this node.Object[]
getPathToRoot
(T aNode) Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.protected Object[]
getPathToRoot
(T aNode, int depth) Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.getPreviousLeaf
(T node) Returns the leaf before this node or null if this node is the first leaf in the tree.getPreviousSibling
(T parent, T node) Returns the previous sibling of this node in the parent's children array.boolean
isAncestor
(T ancestor, T descendant) Indicates whether an element is ancestor of another one.boolean
Returns true if this node has no children.boolean
isNodeChild
(T parent, T aNode) Returns true ifaNode
is a child of this node.boolean
isNodeSibling
(T aNode, T anotherNode) Returns true ifanotherNode
is a sibling of (has the same parent as) this node.boolean
leavesIterator
(T parent) void
notifyHierarchyNodeChanged
(T child, T parent, int index, boolean isAdjusting) protected void
notifyHierarchyNodeInserted
(T child, T parent, int index, boolean isAdjusting) protected void
notifyHierarchyNodeRemoved
(T child, T parent, int index, boolean isAdjusting) protected void
preorderIterator
(T parent) Creates and returns an iterable that traverses the subhierarchy rooted at the give node in preorder.void
removeHierarchyListener
(HierarchyListener<T> hierarchyListener) void
void
setNotifyListeners
(boolean enable) toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.macrofocus.hierarchy.Hierarchy
containsChild, containsChild, getChild, getChildCount, getChildList, getChildren, getIndexOfChild, getParent, getRoot, hasChild
-
Constructor Details
-
AbstractHierarchy
public AbstractHierarchy()
-
-
Method Details
-
addHierarchyListener
- Specified by:
addHierarchyListener
in interfaceHierarchy<T>
-
addWeakHierarchyListener
- Specified by:
addWeakHierarchyListener
in interfaceHierarchy<T>
-
removeHierarchyListener
- Specified by:
removeHierarchyListener
in interfaceHierarchy<T>
-
removeHierarchyListeners
public void removeHierarchyListeners()- Specified by:
removeHierarchyListeners
in interfaceHierarchy<T>
-
getListeners
- Specified by:
getListeners
in interfaceHierarchy<T>
-
setNotifyListeners
public void setNotifyListeners(boolean enable) - Specified by:
setNotifyListeners
in interfaceHierarchy<T>
-
isRoot
-
getNextSibling
Returns the next sibling of this node in the parent's children array. Returns null if this node has no parent or is the parent's last child. This method performs a linear search that is O(n) where n is the number of children; to traverse the entire array, use the parent's child enumeration instead.- Returns:
- the sibling of this node that immediately follows this node
-
isNodeSibling
Returns true ifanotherNode
is a sibling of (has the same parent as) this node. A node is its own sibling. IfanotherNode
is null, returns false.- Parameters:
anotherNode
- node to test as sibling of this node- Returns:
- true if
anotherNode
is a sibling of this node
-
getPreviousSibling
Returns the previous sibling of this node in the parent's children array. Returns null if this node has no parent or is the parent's first child. This method performs a linear search that is O(n) where n is the number of children.- Returns:
- the sibling of this node that immediately precedes this node
-
getChildAfter
Returns the child in this node's child array that immediately followsaChild
, which must be a child of this node. IfaChild
is the last child, returns null. This method performs a linear search of this node's children foraChild
and is O(n) where n is the number of children; to traverse the entire array of children, use an enumeration instead.- Returns:
- the child of this node that immediately follows
aChild
- Throws:
IllegalArgumentException
- ifaChild
is null or is not a child of this node
-
getChildBefore
Returns the child in this node's child array that immediately precedesaChild
, which must be a child of this node. IfaChild
is the first child, returns null. This method performs a linear search of this node's children foraChild
and is O(n) where n is the number of children.- Returns:
- the child of this node that immediately precedes
aChild
- Throws:
IllegalArgumentException
- ifaChild
is null or is not a child of this node
-
getDepth
public int getDepth()Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf. If this node has no children, returns 0. This operation is much more expensive thangetLevel()
because it must effectively traverse the entire tree rooted at this node. -
getLevel
Returns the number of levels above this node -- the distance from the root to this node. If this node is the root, returns 0. -
getPath
Returns the path from the root, to get to this node. The last element in the path is this node. -
isAncestor
Indicates whether an element is ancestor of another one.- Parameters:
ancestor
- potential ancestordescendant
- potential descendant- Returns:
true
ifancestor
is equal todescendant
or ifancestor
is the parent ofdescendant
or ifancestor
is the parent of an ancestor ofdescendant
; returnsfalse
otherwise
-
notifyHierarchyNodeInserted
-
notifyHierarchyNodeChanged
- Specified by:
notifyHierarchyNodeChanged
in interfaceHierarchy<T>
-
notifyHierarchyNodeRemoved
-
notifyHierarchyStructureChanged
protected void notifyHierarchyStructureChanged() -
preorderIterator
Description copied from interface:Hierarchy
Creates and returns an iterable that traverses the subhierarchy rooted at the give node in preorder. The first node returned by the iterator's next() method is the given node.- Specified by:
preorderIterator
in interfaceHierarchy<T>
- Parameters:
parent
- the root of the hierarchy to traverse- Returns:
- an iterable that traverses the subtree rooted at this node in preorder.
-
breadthFirstIterator
Description copied from interface:Hierarchy
Creates and returns an iterable that traverses the subhierarchy rooted at the give node in breadth-first order. The first node returned by the iterator's next() method is the given node.- Specified by:
breadthFirstIterator
in interfaceHierarchy<T>
- Parameters:
parent
- the root of the hierarchy to traverse- Returns:
- an iterable that traverses the subtree rooted at this node in breadth-first order.
-
depthFirstIterator
Description copied from interface:Hierarchy
Creates and returns an iterable that traverses the subhierarchy rooted at the give node in depth-first order. The first node returned by the iterator's next() method is the leftmost leaf.- Specified by:
depthFirstIterator
in interfaceHierarchy<T>
- Parameters:
parent
- the root of the hierarchy to traverse- Returns:
- an iterable that traverses the subtree rooted at this node in depth-first order.
-
leavesIterator
- Specified by:
leavesIterator
in interfaceHierarchy<T>
-
preorderIterator
- Specified by:
preorderIterator
in interfaceHierarchy<T>
-
breadthFirstIterator
- Specified by:
breadthFirstIterator
in interfaceHierarchy<T>
-
depthFirstIterator
- Specified by:
depthFirstIterator
in interfaceHierarchy<T>
-
leavesIterator
- Specified by:
leavesIterator
in interfaceHierarchy<T>
-
getPathToRoot
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.- Specified by:
getPathToRoot
in interfaceHierarchy<T>
- Parameters:
aNode
- the TreeNode to get the path for
-
isNodeChild
Returns true ifaNode
is a child of this node. IfaNode
is null, this method returns false.- Returns:
- true if
aNode
is a child of this node; false ifaNode
is null
-
getFirstChild
Returns this node's first child. If this node has no children, throws NoSuchElementException.- Returns:
- the first child of this node
- Throws:
NoSuchElementException
- if this node has no children
-
getLastChild
Returns this node's last child. If this node has no children, throws NoSuchElementException.- Returns:
- the last child of this node
- Throws:
NoSuchElementException
- if this node has no children
-
getPathToRoot
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.- Parameters:
aNode
- the TreeNode to get the path fordepth
- an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned array- Returns:
- an array of TreeNodes giving the path from the root to the specified node
-
isLeaf
Returns true if this node has no children. To distinguish between nodes that have no children and nodes that cannot have children (e.g. to distinguish files from empty directories), use this method in conjunction withgetAllowsChildren
-
getFirstLeaf
Finds and returns the first leaf that is a descendant of this node -- either this node or its first child's first leaf. Returns this node if it is a leaf.- Specified by:
getFirstLeaf
in interfaceHierarchy<T>
- Returns:
- the first leaf in the subtree rooted at this node
- See Also:
-
getLastLeaf
Finds and returns the last leaf that is a descendant of this node -- either this node or its last child's last leaf. Returns this node if it is a leaf.- Specified by:
getLastLeaf
in interfaceHierarchy<T>
- Returns:
- the last leaf in the subtree rooted at this node
- See Also:
-
getNextLeaf
Returns the leaf after this node or null if this node is the last leaf in the tree.In this implementation of the
MutableNode
interface, this operation is very inefficient. In order to determine the next node, this method first performs a linear search in the parent's child-list in order to find the current node.That implementation makes the operation suitable for short traversals from a known position. But to traverse all of the leaves in the tree, you should use
depthFirstEnumeration
to enumerate the nodes in the tree and useisLeaf
on each node to determine which are leaves.- Specified by:
getNextLeaf
in interfaceHierarchy<T>
- Returns:
- returns the next leaf past this node
- See Also:
-
getPreviousLeaf
Returns the leaf before this node or null if this node is the first leaf in the tree.In this implementation of the
MutableNode
interface, this operation is very inefficient. In order to determine the previous node, this method first performs a linear search in the parent's child-list in order to find the current node.That implementation makes the operation suitable for short traversals from a known position. But to traverse all of the leaves in the tree, you should use
depthFirstEnumeration
to enumerate the nodes in the tree and useisLeaf
on each node to determine which are leaves.- Specified by:
getPreviousLeaf
in interfaceHierarchy<T>
- Returns:
- returns the leaf before this node
- See Also:
-
getLeafCount
Returns the total number of leaves that are descendants of this node. If this node is a leaf, returns1
. This method is O(n) where n is the number of descendants of this node.- Specified by:
getLeafCount
in interfaceHierarchy<T>
- Returns:
- the number of leaves beneath this node
-
toString
-