Class AbstractHierarchy<T>
- java.lang.Object
-
- com.macrofocus.hierarchy.AbstractHierarchy<T>
-
- All Implemented Interfaces:
Hierarchy<T>,java.io.Serializable
- Direct Known Subclasses:
SimpleHierarchy
public abstract class AbstractHierarchy<T> extends java.lang.Object implements Hierarchy<T>
This class provides a skeletal implementation of the Hierarchy interface to minimize the effort required to implement this interface.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description AbstractHierarchy()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddHierarchyListener(HierarchyListener<T> listener)voidaddWeakHierarchyListener(HierarchyListener<T> listener)java.lang.Iterable<T>breadthFirstIterator()java.lang.Iterable<T>breadthFirstIterator(T parent)Creates and returns an iterable that traverses the subhierarchy rooted at the give node in breadth-first order.java.lang.Iterable<T>depthFirstIterator()java.lang.Iterable<T>depthFirstIterator(T parent)Creates and returns an iterable that traverses the subhierarchy rooted at the give node in depth-first order.TgetChildAfter(T parent, T child)Returns the child in this node's child array that immediately followsaChild, which must be a child of this node.TgetChildBefore(T parent, T child)Returns the child in this node's child array that immediately precedesaChild, which must be a child of this node.intgetDepth()Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf.TgetFirstChild(T node)Returns this node's first child.TgetFirstLeaf(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.TgetLastChild(T node)Returns this node's last child.TgetLastLeaf(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.intgetLeafCount(T node)Returns the total number of leaves that are descendants of this node.intgetLevel(T node)Returns the number of levels above this node -- the distance from the root to this node.java.lang.Iterable<HierarchyListener<T>>getListeners()TgetNextLeaf(T node)Returns the leaf after this node or null if this node is the last leaf in the tree.TgetNextSibling(T parent, T child)Returns the next sibling of this node in the parent's children array.java.util.List<T>getPath(T node)Returns the path from the root, to get to this node.java.lang.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 java.lang.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.TgetPreviousLeaf(T node)Returns the leaf before this node or null if this node is the first leaf in the tree.TgetPreviousSibling(T parent, T node)Returns the previous sibling of this node in the parent's children array.booleanisAncestor(T ancestor, T descendant)Indicates whether an element is ancestor of another one.booleanisLeaf(T node)Returns true if this node has no children.booleanisNodeChild(T parent, T aNode)Returns true ifaNodeis a child of this node.booleanisNodeSibling(T aNode, T anotherNode)Returns true ifanotherNodeis a sibling of (has the same parent as) this node.booleanisRoot(T node)java.lang.Iterable<T>leavesIterator()java.lang.Iterable<T>leavesIterator(T parent)voidnotifyHierarchyNodeChanged(T child, T parent, int index, boolean isAdjusting)protected voidnotifyHierarchyNodeInserted(T child, T parent, int index, boolean isAdjusting)protected voidnotifyHierarchyNodeRemoved(T child, T parent, int index, boolean isAdjusting)protected voidnotifyHierarchyStructureChanged()java.lang.Iterable<T>preorderIterator()java.lang.Iterable<T>preorderIterator(T parent)Creates and returns an iterable that traverses the subhierarchy rooted at the give node in preorder.voidremoveHierarchyListener(HierarchyListener<T> hierarchyListener)voidremoveHierarchyListeners()voidsetNotifyListeners(boolean enable)java.lang.StringtoString()-
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
-
-
-
-
Method Detail
-
addHierarchyListener
public void addHierarchyListener(HierarchyListener<T> listener)
- Specified by:
addHierarchyListenerin interfaceHierarchy<T>
-
addWeakHierarchyListener
public void addWeakHierarchyListener(HierarchyListener<T> listener)
- Specified by:
addWeakHierarchyListenerin interfaceHierarchy<T>
-
removeHierarchyListener
public void removeHierarchyListener(HierarchyListener<T> hierarchyListener)
- Specified by:
removeHierarchyListenerin interfaceHierarchy<T>
-
removeHierarchyListeners
public void removeHierarchyListeners()
- Specified by:
removeHierarchyListenersin interfaceHierarchy<T>
-
getListeners
public java.lang.Iterable<HierarchyListener<T>> getListeners()
- Specified by:
getListenersin interfaceHierarchy<T>
-
setNotifyListeners
public void setNotifyListeners(boolean enable)
- Specified by:
setNotifyListenersin interfaceHierarchy<T>
-
getNextSibling
public T getNextSibling(T parent, T child)
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
public boolean isNodeSibling(T aNode, T anotherNode)
Returns true ifanotherNodeis a sibling of (has the same parent as) this node. A node is its own sibling. IfanotherNodeis null, returns false.- Parameters:
anotherNode- node to test as sibling of this node- Returns:
- true if
anotherNodeis a sibling of this node
-
getPreviousSibling
public T getPreviousSibling(T parent, T node)
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
public T 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. IfaChildis the last child, returns null. This method performs a linear search of this node's children foraChildand 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:
java.lang.IllegalArgumentException- ifaChildis null or is not a child of this node
-
getChildBefore
public T 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. IfaChildis the first child, returns null. This method performs a linear search of this node's children foraChildand is O(n) where n is the number of children.- Returns:
- the child of this node that immediately precedes
aChild - Throws:
java.lang.IllegalArgumentException- ifaChildis 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.- Specified by:
getDepthin interfaceHierarchy<T>- Returns:
- the depth of the tree whose root is this node
- See Also:
getLevel(T)
-
getLevel
public int getLevel(T node)
Returns the number of levels above this node -- the distance from the root to this node. If this node is the root, returns 0.- Specified by:
getLevelin interfaceHierarchy<T>- Returns:
- the number of levels above this node
- See Also:
getDepth()
-
getPath
public java.util.List<T> getPath(T node)
Returns the path from the root, to get to this node. The last element in the path is this node.
-
isAncestor
public boolean isAncestor(T ancestor, T descendant)
Indicates whether an element is ancestor of another one.- Parameters:
ancestor- potential ancestordescendant- potential descendant- Returns:
trueifancestoris equal todescendantor ifancestoris the parent ofdescendantor ifancestoris the parent of an ancestor ofdescendant; returnsfalseotherwise
-
notifyHierarchyNodeInserted
protected void notifyHierarchyNodeInserted(T child, T parent, int index, boolean isAdjusting)
-
notifyHierarchyNodeChanged
public void notifyHierarchyNodeChanged(T child, T parent, int index, boolean isAdjusting)
- Specified by:
notifyHierarchyNodeChangedin interfaceHierarchy<T>
-
notifyHierarchyNodeRemoved
protected void notifyHierarchyNodeRemoved(T child, T parent, int index, boolean isAdjusting)
-
notifyHierarchyStructureChanged
protected void notifyHierarchyStructureChanged()
-
preorderIterator
public java.lang.Iterable<T> preorderIterator(T parent)
Description copied from interface:HierarchyCreates 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:
preorderIteratorin 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
public java.lang.Iterable<T> breadthFirstIterator(T parent)
Description copied from interface:HierarchyCreates 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:
breadthFirstIteratorin 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
public java.lang.Iterable<T> depthFirstIterator(T parent)
Description copied from interface:HierarchyCreates 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:
depthFirstIteratorin 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
public java.lang.Iterable<T> leavesIterator(T parent)
- Specified by:
leavesIteratorin interfaceHierarchy<T>
-
preorderIterator
public java.lang.Iterable<T> preorderIterator()
- Specified by:
preorderIteratorin interfaceHierarchy<T>
-
breadthFirstIterator
public java.lang.Iterable<T> breadthFirstIterator()
- Specified by:
breadthFirstIteratorin interfaceHierarchy<T>
-
depthFirstIterator
public java.lang.Iterable<T> depthFirstIterator()
- Specified by:
depthFirstIteratorin interfaceHierarchy<T>
-
leavesIterator
public java.lang.Iterable<T> leavesIterator()
- Specified by:
leavesIteratorin interfaceHierarchy<T>
-
getPathToRoot
public java.lang.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. The length of the returned array gives the node's depth in the tree.- Specified by:
getPathToRootin interfaceHierarchy<T>- Parameters:
aNode- the TreeNode to get the path for
-
isNodeChild
public boolean isNodeChild(T parent, T aNode)
Returns true ifaNodeis a child of this node. IfaNodeis null, this method returns false.- Returns:
- true if
aNodeis a child of this node; false ifaNodeis null
-
getFirstChild
public T getFirstChild(T node)
Returns this node's first child. If this node has no children, throws NoSuchElementException.- Returns:
- the first child of this node
- Throws:
java.util.NoSuchElementException- if this node has no children
-
getLastChild
public T getLastChild(T node)
Returns this node's last child. If this node has no children, throws NoSuchElementException.- Returns:
- the last child of this node
- Throws:
java.util.NoSuchElementException- if this node has no children
-
getPathToRoot
protected java.lang.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. 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
public boolean isLeaf(T node)
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
public T 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. Returns this node if it is a leaf.- Specified by:
getFirstLeafin interfaceHierarchy<T>- Returns:
- the first leaf in the subtree rooted at this node
- See Also:
isLeaf(T)
-
getLastLeaf
public T 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. Returns this node if it is a leaf.- Specified by:
getLastLeafin interfaceHierarchy<T>- Returns:
- the last leaf in the subtree rooted at this node
- See Also:
isLeaf(T)
-
getNextLeaf
public T getNextLeaf(T node)
Returns the leaf after this node or null if this node is the last leaf in the tree.In this implementation of the
MutableNodeinterface, 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
depthFirstEnumerationto enumerate the nodes in the tree and useisLeafon each node to determine which are leaves.- Specified by:
getNextLeafin interfaceHierarchy<T>- Returns:
- returns the next leaf past this node
- See Also:
isLeaf(T)
-
getPreviousLeaf
public T getPreviousLeaf(T node)
Returns the leaf before this node or null if this node is the first leaf in the tree.In this implementation of the
MutableNodeinterface, 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
depthFirstEnumerationto enumerate the nodes in the tree and useisLeafon each node to determine which are leaves.- Specified by:
getPreviousLeafin interfaceHierarchy<T>- Returns:
- returns the leaf before this node
- See Also:
isLeaf(T)
-
getLeafCount
public int getLeafCount(T node)
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:
getLeafCountin interfaceHierarchy<T>- Returns:
- the number of leaves beneath this node
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-