B
bengali
Hi,
i would like to model a Tree in Java but with the requirements that
every node
of the same level is of the same Node subtype.
I thought about using Generics and therefore, i designed the following
abstract class:
/**
* <T> child node type
* <K> parent node type
*/
class abstract Node<T extends Node,K extends Node> {
private K parentNode;
private List<T> childrenNodes = new ArrayList<T>();
public void addChildNode(T node) {
childrenNodes.add(node);
}
public K getParent() {
return parentNode;
}
public abstract void retrieveChildrenNodes();
}
with for instance the following implementation:
public FirstLevel extends Node<SecondLevel,Node> {
private Object firstLevelAttribute;
public void retrieveChildrenNodes() {
...
}
}
public SecondLevel extends Node<ThirdLevel,FirstLevel) {
....
public void retrieveChildrenNodes() {
...
}
}
But of course, i am unable to express the following:
- That the type of the parent node can be of Void type (for the
root node for instance).
- I get warnings for the FirstLevel class since one of its
parameterized types is
of type Node without parameterized types.
(public FirstLevel extends Node<SecondLevel,Node> )
Having parameterized type would allow me if given a node of a level
to navigate in the tree between children and parent without casting
and work with the specific attributes
of each level type.
So if you have any idea how i could model this data structure...
Thanks,
bengalister.
i would like to model a Tree in Java but with the requirements that
every node
of the same level is of the same Node subtype.
I thought about using Generics and therefore, i designed the following
abstract class:
/**
* <T> child node type
* <K> parent node type
*/
class abstract Node<T extends Node,K extends Node> {
private K parentNode;
private List<T> childrenNodes = new ArrayList<T>();
public void addChildNode(T node) {
childrenNodes.add(node);
}
public K getParent() {
return parentNode;
}
public abstract void retrieveChildrenNodes();
}
with for instance the following implementation:
public FirstLevel extends Node<SecondLevel,Node> {
private Object firstLevelAttribute;
public void retrieveChildrenNodes() {
...
}
}
public SecondLevel extends Node<ThirdLevel,FirstLevel) {
....
public void retrieveChildrenNodes() {
...
}
}
But of course, i am unable to express the following:
- That the type of the parent node can be of Void type (for the
root node for instance).
- I get warnings for the FirstLevel class since one of its
parameterized types is
of type Node without parameterized types.
(public FirstLevel extends Node<SecondLevel,Node> )
Having parameterized type would allow me if given a node of a level
to navigate in the tree between children and parent without casting
and work with the specific attributes
of each level type.
So if you have any idea how i could model this data structure...
Thanks,
bengalister.