Data
- The type of additional data which are referenced in this node. Use Object if data are not used.DerivedNode
- Type of the TreeNode which is build with this class as superclass.
If one will only use the underived TreeNodeBase, use TreeNodeBase.TreeNode
instead.public class TreeNodeBase<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data,IfcType extends SortedTree<IfcType>> extends java.lang.Object implements TreeNode_ifc<DerivedNode,Data>, SortedTree<IfcType>, java.lang.Iterable<DerivedNode>
TreeMap
for example is:
Members of a TreeMap are organized in a tree, but there are not nodes itself. The TreeMap
is one of more possibly access forms. The same data may be organized in another TreeMap with
another key at the same time, it may be organized in a List in the same time or in an array.
The node of a TreeMap is not visible for usage.
Docu_UML_simpleNotation
:
TreeNodeBase |---data---> T (may be null) TreeNodeBase <---parent----------| (maybe null) | TreeNodeBase | | |------childNodes------*>| | |---data------> T |<------------parent-----| (a leaf if it hasn't | childNodes) |----leafData-----*> T (leafs)
class MyData{ final TreeNodeBase node; MyData(MyData parent){ this.node = new TreeNodeBase(parent.node, this); } }
TreeNode -key |------data-------> T (data, may be null) TreeNode <---parent--------------| (maybe null) |------idxChildren-----------key*>| | TreeNode (children) unique |------unsortedChildren---------*>| | |-------data-------> T |<------------parent--------------| (the leaf if another node is not referenced)
!Meta-Node for all nodes !parent !with same key TreeNodeBase TreeNodeBase TreeNodeBase | -key !one of the children | | -key |---data-->T |--data->null | <---parent---| |--idxChildren-->null | |--idxChildren----key*>| |---data-->T the data | |--childNodes----------*>| | | |--idxChildren--->maybe deeper nodes or null | | |--childNodes---->maybe deeper nodes or null |<-----------parent----|<---------parent--------| | | |--childNodes---------------------------------*>|Sometimes a container should refer more as one data with the same key. That is not possible if the java.util.TreeMap is used, it supports only one key per data. For example for XML data an element can contain more as one sub element with the same tag name
childNodes()
reference. That is because this list of children represents
the creation order. For example for XML nodes it is the order in the XML file.
nd_idxChildren
.
This meta node refers all children with the same key with its childNodes()
list.
The order is the creation order.
nd_data
of the meta node is left empty, because the data are located in
childNodes()
.nd_data
. The field nd_idxChildren
is left empty too because
there are no children with other keys than the only one.
Modifier and Type | Class and Description |
---|---|
protected class |
TreeNodeBase.IteratorDerivedImpl
It is similar like
TreeNodeBase.IteratorImpl but with DerivedNode as return. |
protected class |
TreeNodeBase.IteratorDerivedNode |
protected class |
TreeNodeBase.IteratorImpl |
protected class |
TreeNodeBase.IteratorMetaNode |
protected class |
TreeNodeBase.IteratorOneNode |
protected static class |
TreeNodeBase.MetaNode<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data,IfcType extends SortedTree<IfcType>> |
static class |
TreeNodeBase.TreeNode<Data>
This class provides a ready to use TreeNode without additional functionality like TreeNodeBase.
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
metaNodeKey
This instance is used if the node is a meta node to refer more as one child with the same key.
|
Data |
nd_data
Any additional data associated with this node.
|
protected DerivedNode |
nd_firstChild
The List of child nodes in order of adding.
|
protected java.util.Map<java.lang.String,TreeNodeBase<DerivedNode,Data,IfcType>> |
nd_idxChildren
The child nodes sorted to the key.
|
protected java.lang.String |
nd_key
The key which is used in the
nd_idxChildren of the parent. |
protected DerivedNode |
nd_lastChild
The List of child nodes in order of adding.
|
java.util.List<Data> |
nd_leafData
Instances which are a leaf of this node.
|
protected DerivedNode |
nd_next
The List of child nodes in order of adding.
|
protected int |
nd_nrofChildren |
protected TreeNodeBase<DerivedNode,Data,IfcType> |
nd_parent
The parent is a metaNodeKey if there is more as one child with the same key.
|
protected DerivedNode |
nd_prev
The List of child nodes in order of adding.
|
static int |
version
Version, history and license.
|
Modifier | Constructor and Description |
---|---|
|
TreeNodeBase(java.lang.String key,
Data data)
Creates a new unbounded node.
|
protected |
TreeNodeBase(java.lang.String key,
Data data,
TreeNodeBase<DerivedNode,Data,IfcType> parent)
Creates a new unbounded node.
|
Modifier and Type | Method and Description |
---|---|
void |
addLeaf(Data leaf)
Deprecated.
|
void |
addNode(DerivedNode childNode)
Adds a given child node.
|
DerivedNode |
addNode(java.lang.String itsKey,
Data leaf)
create a child node and adds it.
|
void |
addNodeFirst(DerivedNode childNode) |
private void |
addNodeKeyed(DerivedNode childNode,
TreeNodeBase<DerivedNode,Data,IfcType> prev)
Adds the child node in the index of nodes with key of this parent and set the parent of child.
|
void |
addSiblingNext(DerivedNode childNode)
Adds a new node behind the given node as sibling of this respectively child of this parent.
|
void |
addSiblingPrev(DerivedNode childNode)
Adds a new node before the given node as sibling of this respectively child of this parent.
|
java.util.List<DerivedNode> |
childNodes()
The List of child nodes in order of adding.
|
protected java.util.List<DerivedNode> |
childNodes(TreeNodeBase<DerivedNode,Data,IfcType> node) |
void |
detach()
Detaches the node from its tree.
|
DerivedNode |
firstChild()
Returns the first of all children.
|
IfcType |
getChild(java.lang.String sKey)
Returns the node with the given key.
|
java.util.List<DerivedNode> |
getContainerChildren(java.lang.String sKey)
Returns the container with children with the same key.
|
java.lang.String |
getKey()
Returns the key of this node.
|
DerivedNode |
getNode(java.lang.String path,
java.lang.String separator)
Returns any selected node in the tree.
|
DerivedNode |
getOrCreateNode(java.lang.String path,
java.lang.String separator)
Searches a node with the path, creates it if it is not found.
|
IfcType |
getParent()
Gets the parent of this node or null if it is the root node or it is
detach() . |
Data |
getParentData() |
boolean |
hasChildren() |
IterableIterator<DerivedNode> |
iterator()
It returns an instance which can used as both as
Iterator
and as Iterable . |
IterableIterator<DerivedNode> |
iteratorChildren()
Returns an iterator through the list of all children of the node.
|
IterableIterator<DerivedNode> |
iteratorChildren(java.lang.String keyP) |
java.util.Iterator<IfcType> |
iterChildren()
Returns an iterator through the list of all children of the node.
|
java.util.Iterator<IfcType> |
iterChildren(java.lang.String sKey)
Returns an iterator through the list of all children with the given key.
|
DerivedNode |
lastChild()
Returns the last of all children.
|
java.util.List<IfcType> |
listChildren()
Returns a List of all children of the node.
|
java.util.List<IfcType> |
listChildren(java.lang.String sKey)
Searches the child node with the given key.
|
void |
movetoFirstChild(DerivedNode src)
Remove the dst node from its bough and add it as the first child of this.
|
void |
movetoSiblingNext(DerivedNode src)
Remove the dst node from its bough and add it as next sibling after this.
|
void |
movetoSiblingPrev(DerivedNode src)
Remove the dst node from its bough and add it as previous sibling before this.
|
protected DerivedNode |
newNode(java.lang.String keyP,
Data dataP)
This method has to be overridden in the derived class.
|
DerivedNode |
nextSibling() |
int |
nrofChildren()
Returns the number of children.
|
DerivedNode |
parent() |
boolean |
parentEquals(TreeNode_ifc<DerivedNode,Data> cmp)
Checks whether the parent is a known node.
|
DerivedNode |
prevSibling() |
void |
removeChildren()
Remove all children of this node.
|
void |
swap(DerivedNode other)
Swaps two nodes, this and dst.
|
java.lang.String |
toString() |
private TreeNodeCallback.Result |
walkSubTree(DerivedNode node,
int depth,
TreeNodeCallback<DerivedNode> callback) |
void |
walkTree(DerivedNode root,
int depth,
TreeNodeCallback<DerivedNode> callback)
Walk through the tree.
|
public static final int version
iteratorChildren()
but with TreeNodeBase.IteratorDerivedImpl
which returns the next() as user type node
TODO: iteratorChildren(String)
gardening with documentation and test.
movetoSiblingNext(TreeNodeBase)
, swap(TreeNodeBase)
removeChildren()
if an exception was before.
iterChildren()
have to be return null, see its interface-definition.
It had returned an Iterator, which first Iterator.hasNext()
returns false. But with them
it is not determined simply whether a node has children or not. The SimpleXmlOutputter
has not worked correctly. There the decision whether the text() of a node should be output depends
on the existence of children. If it has children, the children contains the text() already.
TreeNodeBase.IteratorDerivedNode
etc.
firstChild()
, lastChild()
. Some methods returns
the DerivedNode yet instead a reference of Type TreeNodeBase or IfcType. Only methods which are declared
in SortedTree
returns the IfcType. It is better for usage. A node is always type of DerivedNode
because it is declared and known as forward reference.
#addNode(TreeNodeBase, int)
not on end.
nd_next
, nd_prev
and nd_lastChild
for sibling navigation.
SortedTree
.
XmlNodeSimple
and has reduced the complexity
of that class.
TreeNodeUniqueKey
is sorted than.
private static java.lang.String metaNodeKey
protected final java.lang.String nd_key
nd_idxChildren
of the parent.
Note that a key need not be unique. A parent can have children with the same key.protected TreeNodeBase<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data,IfcType extends SortedTree<IfcType>> nd_parent
parent()
.protected DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType> nd_prev
protected DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType> nd_next
protected DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType> nd_firstChild
protected DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType> nd_lastChild
protected java.util.Map<java.lang.String,TreeNodeBase<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data,IfcType extends SortedTree<IfcType>>> nd_idxChildren
nd_key
.
This index can contain a so named 'meta node' which holds more as one child with the same key.
Note that the meta node is not registered in the childNodes()
.
The sorting with key may not be necessary in some kinds, then this element is null. Don't use it
to get a list of children. Use nd_firstChild
etc. instead.public java.util.List<Data> nd_leafData
protected int nd_nrofChildren
public final Data nd_data
public TreeNodeBase(java.lang.String key, Data data)
key
- The key will be used if the tree is sorted. It can be null,
then the tree is not sorted. The key is used to search this nodes by its key
in the parent node or from any grandparent node with the path.data
- User data of this node.protected TreeNodeBase(java.lang.String key, Data data, TreeNodeBase<DerivedNode,Data,IfcType> parent)
key
- The key will be used if the tree is sorted. It can be null,
then the tree is not sorted. The key is used to search this nodes by its key
in the parent node or from any grandparent node with the path.data
- User data of this node.protected DerivedNode newNode(java.lang.String keyP, Data dataP)
nd_key
- see TreeNodeBase(String, Object)
nd_data
- see TreeNodeBase(String, Object)
@Deprecated public void addLeaf(Data leaf)
leaf
- The data to add.public DerivedNode addNode(java.lang.String itsKey, Data leaf)
addNode(TreeNodeBase)
addNode
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
itsKey
- The key may be free defined outside, independent of the content of the child.
This key is used to find out children.leaf
- The data of the node.public void addNode(DerivedNode childNode)
addNode
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
itsKey
- The key may be free defined outside, independent of the content of the child.
This key is used to find out children.leaf
- The data of the node.public void addNodeFirst(DerivedNode childNode)
addNodeFirst
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public void addSiblingNext(DerivedNode childNode)
addSiblingNext
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
childNode
- public void addSiblingPrev(DerivedNode childNode)
childNode
- The yet not added new instance of node.private void addNodeKeyed(DerivedNode childNode, TreeNodeBase<DerivedNode,Data,IfcType> prev)
childNode
- public void swap(DerivedNode other)
dst
- The tree node which is swapped with this.public void movetoSiblingNext(DerivedNode src)
src
- the tree node which should be removed and add as next sibling of this.public void movetoSiblingPrev(DerivedNode src)
src
- the tree node which should be removed and add as next sibling of this.public void movetoFirstChild(DerivedNode src)
firstChild().movetoSiblingPrev(src)
. But if this has not children, it is the first one.
It is not necessary that both TreeNode have the same root. This and dst may be siblings.src
- the tree node which should be removed and add as next sibling of this.public void detach()
getParent()
.
Nevertheless you can refer any node within another structure of data.
detach
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public void removeChildren()
detach()
for any child node if it and their sub tree should be used furthermore.removeChildren
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public java.lang.String getKey()
getKey
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public Data getParentData()
getParentData
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public DerivedNode getNode(java.lang.String path, java.lang.String separator)
getNode
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
path
- The path may have more as one key to traverse into the boughs of the tree.separator
- Separator between key elements in path. If null then only one level, since 2019-04.public DerivedNode getOrCreateNode(java.lang.String path, java.lang.String separator)
getOrCreateNode
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
path
- path of keys, see getNode(String, String)
separator
- The separator in path.public IfcType getParent()
detach()
.getParent
in interface SortedTree<IfcType extends SortedTree<IfcType>>
SortedTree.getParent()
public DerivedNode parent()
parent
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public boolean parentEquals(TreeNode_ifc<DerivedNode,Data> cmp)
TreeNode_ifc
parentEquals
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
cmp
- The node to compare withpublic DerivedNode nextSibling()
nextSibling
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public DerivedNode prevSibling()
prevSibling
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public java.util.List<DerivedNode> childNodes()
public int nrofChildren()
nrofChildren
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public DerivedNode firstChild()
firstChild
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public boolean hasChildren()
hasChildren
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public DerivedNode lastChild()
lastChild
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public IfcType getChild(java.lang.String sKey)
getChild
in interface SortedTree<IfcType extends SortedTree<IfcType>>
sKey
- public java.lang.String toString()
toString
in class java.lang.Object
public IterableIterator<DerivedNode> iterator()
Iterator
and as Iterable
. It means the routines Iterator.hasNext()
etc. are able to call
and Iterable.iterator()
is able to call, which returns this.
In this kind the method can be used in a for-container-loop.iterator
in interface java.lang.Iterable<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>>
public IterableIterator<DerivedNode> iteratorChildren(java.lang.String keyP)
iteratorChildren
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public java.util.Iterator<IfcType> iterChildren()
SortedTree
iterChildren
in interface SortedTree<IfcType extends SortedTree<IfcType>>
public IterableIterator<DerivedNode> iteratorChildren()
TreeNode_ifc
iteratorChildren
in interface TreeNode_ifc<DerivedNode extends TreeNodeBase<DerivedNode,Data,IfcType> & SortedTree<IfcType>,Data>
public java.util.Iterator<IfcType> iterChildren(java.lang.String sKey)
SortedTree
iterChildren
in interface SortedTree<IfcType extends SortedTree<IfcType>>
sKey
- The key of the demanded child. The implementation may have extra possibilities, see getChild(String).public java.util.List<IfcType> listChildren()
SortedTree
listChildren
in interface SortedTree<IfcType extends SortedTree<IfcType>>
public java.util.List<IfcType> listChildren(java.lang.String sKey)
childNodes(TreeNodeBase)
list is the proper return value. But if the child node refers only one child,
a list with this one element is built temporary because this method should return a list anyway.listChildren
in interface SortedTree<IfcType extends SortedTree<IfcType>>
sKey
- The key of the demanded child. The implementation may have extra possibilities, see getChild(String).SortedTree.listChildren(java.lang.String)
public void walkTree(DerivedNode root, int depth, TreeNodeCallback<DerivedNode> callback)
root
- The start nodedepth
- Maximum number of child levels, maybe Integer.MAX_VALUE
callback
- The callback should be implemented by the user to evaluate and control the walking.private TreeNodeCallback.Result walkSubTree(DerivedNode node, int depth, TreeNodeCallback<DerivedNode> callback)
public java.util.List<DerivedNode> getContainerChildren(java.lang.String sKey)
sKey
- protected java.util.List<DerivedNode> childNodes(TreeNodeBase<DerivedNode,Data,IfcType> node)