Question:
I'm implementing a recursive add() method on a tree type datastructure, implemented with a generic class representing tree nodes.
How can I create a sub-tree in that recursion, if the sub-tree is a generic type - thus has no constructor I can call?
Details of Situation:
I have a generic class set (which underneath is a map) that is designed to implement maps of maps of maps of ... of objects (basically, N level tree).
public interface NodeInterface<KeyType, ValueType> {
public void add(List<KeyType>, ValueType);
public ValueType getSubTree(KeyType); // Get subtree map
}
// Generic map class.
// Leaf level nodes are maps too, just not maps containing other maps
public abstract class NodeMap <KeyType, ValueType> implements NodeInterface<> {
protected Map<KeyType, ValueType> map; // actual map
...
}
// Leaf level objects - map NOT containing other maps
public class LeafNode extends NodeMap <String, MyDataType> {
public void add(List<KeyType>, ValueType) {} // implementation for leaf add
...
}
// Tree nodes (non-leaf)
public class ParentNodeMap <ValueType extends NodeMap <?,?>>
extends NodeMap <String, ValueType> {
// This class is needed because there are methods that must be
// implemented on a map that can't be implemented on leaf level nodes.
...
}
// Actual tree node classes (needed as syntactic sugar, to avoid end users
// having to deal with HashMap<String, Hashmap<String, HashMap<String, ...MyDataType>>>>>
public class Level2Node extends ParentNodeMap <LeafNode> {
}
public class Level1Node extends ParentNodeMap <Level2Node > {
}
What needs to be done:
I'm trying to implement a method which will recursively insert the leaf level object into this map of map of maps (described in the interface).
E.g., the desired behavior is:
tree = new Level1Node();
List<String> keys = Arrays.asList("key1", "key2"); // I think I need 3 here? irrelevant.
MyDataType data = new MyDataType();
tree.add(keys, data);
Problem:
I can't seem to implement creation and insertion of the subtree map in the add()
method for ParentNode
This is what I tried in ParentNodeMap class:
public void add(List<KeyType> keys, ValueType value);
KeyType key = getFirstKey(keys);
createMapIfNull(); // creates "map" as new HashMap<KeyType, ValueType>()
ValueType subTree = getSubTree(key); // Gets me child subtree from map
// PROBLEM! that subtree is now "null" since we just created an new Map
// which has no key "hey"
// Now here's where we should create a new subtree and add it:
if (subTree == null) {
subTree = new ValueType<>(); // THIS IS WHAT FAILS!!!!
map.put(key, subTree);
subTree.add(keys.removeFirstKey(), value); // recursively add to subtree
}
Now, what I'm specifically stuck on is: How can I create an subTree object (of NodeMap type); where its type is generic ValueType extends NodeMap <?,?>
and that's all I know of it?
Calling new ValueType()
gives "Cannot instantiate the type ValueType" error.