T - Type of the payload (or resource)N - Type of the next level payloadC - Type of the next level node (another TreeNode)public class TreeNode<T,N,V,C extends TreeNode<N,?,V,?>> extends Object
class MyTree extends TreeNode<RootPayload, L1Payload, TreeNode<L1Payload, L2Payload, TreeNode<L2Payload, Void, NilTreeNode>>> {
}
However since java doesn't have type inferencing we'll have to write often the full type of intermediate nodes,
especially during tree construction. Thus it's recommended that you split the chain into a number of dummy classes,
serving only to the purpuse of declaring the type chain:
class MyTree extends TreeNode<RootPayload, L1Payload, L1Tree> {
}
class L1Tree extends TreeNode<L1Payload, L2Payload, L2Tree> {
}
class L2Tree extends TreeNode<L2Payload, Void, NilTreeNode> {
}
NOTE: you could keep the whole definition inside a single file using inner classes.| Modifier and Type | Field and Description |
|---|---|
static String |
CHILDR_UNDER_LEAF |
| Modifier | Constructor and Description |
|---|---|
protected |
TreeNode()
Default constructor doesn't set the payload.
|
protected |
TreeNode(T resource)
However the constructor is protected because users should only create objects through
addChild or in
alternative they should provide they own constructor ovverrides. |
| Modifier and Type | Method and Description |
|---|---|
void |
accept(V dummy) |
C |
addChild(N resource)
Call this method to add a child node.
|
TreeNode<T,N,V,C> |
appendChild(N resource)
This method is especially useful for leaf nodes when you want to append several children to the same parent node,
without having to declare a temporary variable to hold the parent node (which could have long and ugly type)
|
protected Class<? extends C> |
autoChildNodeType(Class<? extends TreeNode> clazz)
For the curious, this method takes care of retrieving the runtime type information for the tree node element
declared for children nodes.
|
void |
baseDepthFirst(V visitor) |
void |
breadthFirst(V visitor)
This method applies a visitor to all nodes in a breadth first fashon
Currently null payloads are skipped
|
void |
depthFirst(V visitor)
depth first.
|
List<C> |
getChildren() |
protected <X> X |
getRawType(Type type) |
T |
getResource() |
void |
setChildren(List<C> children) |
void |
setResource(T resource) |
public static final String CHILDR_UNDER_LEAF
protected TreeNode()
TreeNode(T resource) constructor only because it would force any implementor of "alias" subclasses
of TreeNode to provide a dummy implementation of the constructor calling super(resource)
However the constructor is protected because users should only create objects through addChild or in
alternative they should provide they own constructor ovverrides.protected TreeNode(T resource)
addChild or in
alternative they should provide they own constructor ovverrides.resource - payloadpublic C addChild(N resource)
L1Child c1 = root.addChild(new L1Resource()); c1.addChild(new L2Resource()); c1.addChild(new L2Resource()).addChild(new L3Resource());
resource - payloadpublic TreeNode<T,N,V,C> appendChild(N resource)
resource - public void breadthFirst(V visitor)
visitor - Visitorpublic void depthFirst(V visitor)
Visitor - Visitorpublic void baseDepthFirst(V visitor)
protected Class<? extends C> autoChildNodeType(Class<? extends TreeNode> clazz)
clazz - the class object of this classprotected <X> X getRawType(Type type)
public T getResource()
public void setResource(T resource)
public void accept(V dummy)
Copyright © 2019. All rights reserved.