I have a generic tree, the generic parameter is the data type stored by the nodes:
class TreeNode<D>{
public D data;
.....
}
Then a visitor interface to use along with a tree transversal:
interface Visitor<D> {
void visit(TreeNode<D> node);
}
Some visitors can take advantage of generics:
class DataListCreator<D> implements Visitor<D> {
List<D> dataList = new ArrayList<D>();
public void visit(TreeNode<D> node) {
dataList.add(node.data);
}
public List<D> getDataList() {
return dataList;
}
But others don't, they would fit better in a raw class
class NodeCounter implements Visitor {
private int nodeCount = 0;
public void visit(TreeNode node) {
nodeCount++;
}
public int count() {
return nodeCount;
}
But I don't know how implement this last case, the code above don't compile as I have to implement the generic interface not the raw one. I tried implementing
Visitor<?>
with the same result. So my question is, I'm forced to use a generic type
NodeCounter<D>
to implement the Visitor interface?.
Thanks.