在java

时间:2017-02-28 07:49:55

标签: java generics composite

我定义了包含GraphNode的类如下,我声明N的意图是使用泛型比较2个GraphNode对象。

问题是如何实例化递归绑定的图。

声明如下,

错误。

Graph<Integer,Comparable<GraphNode>> graph = new Graph<>();

绑定不匹配:类型Comparable<GraphNode>不能替代<N extends Comparable<GraphNode<T,N>>>类型的有界参数Graph<T,N>

public class Graph<T, N extends Comparable<GraphNode<T, N>>> {

    private N root;

    private Class<N> clazz;

    Graph(Class<N> clazz) {
        this.clazz = clazz;
    }

    public N getInstance() {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(T d, N n) {
        if (root == null && n == null)
            root = getInstance();
        if (root == null)
            root = n;
        N node = root;
        while (node != null) {
            if (node.equals(n)) {
                N newNode = getInstance();
                ((GraphNode<T, N>) newNode).setAdjNode(newNode);
            }
        }
    }

}

public class GraphNode<T, N extends Comparable<GraphNode<T, N>>> implements Comparable<N> {

    private T data;

    private LinkedHashSet<N> adjNodes = new LinkedHashSet<>();

    GraphNode() {
        data = null;
    }

    GraphNode(T d) {
        setData(d);
    }

    public void setAdjNode(N n) {
        adjNodes.add(n);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public int hashCode() {
        return data.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof GraphNode<?, ?>) {
            return ((GraphNode<?, ?>) obj).getData() == this.getData();
        }
        return false;
    }

    @Override
    public String toString() {
        return data + "";
    }

    @Override
    public int compareTo(N o) {
        return this.compareTo(o);
    }

}

1 个答案:

答案 0 :(得分:0)

这解决了我上面的问题

 public class Graph<T extends Comparable<T>> {

    private GraphNode<T> root;

    public void insert(T d, GraphNode<T> n) {
        if (root == null && n == null)
            root = new GraphNode<T>(d);
        if (root == null)
            root = n;
        GraphNode<T> node = root;
        Queue<GraphNode<T>> queue = new ConcurrentLinkedQueue<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            node = queue.poll();
            node.setNodeColor(color.BLACK);
            if (node.equals(n)) {
                GraphNode<T> newNode = new GraphNode<T>(d);
                ((GraphNode<T>) newNode).setAdjNode(newNode);
            } else {
                queue.addAll(node.getUnexploredAdjNodes());
            }
        }
    }

}
public class GraphNode<T extends Comparable<T>> implements Comparable<GraphNode<T>> {

    enum color {
        WHITE, GREY, BLACK
    };

    private T data;

    private color nodeColor = color.WHITE;

    private LinkedHashSet<GraphNode<T>> adjNodes = new LinkedHashSet<>();

    GraphNode() {
        data = null;
    }

    GraphNode(T d) {
        setData(d);
    }

    public void setAdjNode(GraphNode<T> n) {
        adjNodes.add(n);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public LinkedHashSet<GraphNode<T>> getAdjNodes() {
        return adjNodes;
    }

    public LinkedHashSet<GraphNode<T>> getUnexploredAdjNodes() {
        LinkedHashSet<GraphNode<T>> n = new LinkedHashSet<>();
        for (GraphNode<T> node : adjNodes) {
            if (node.getNodeColor() == color.WHITE)
                n.add(node);
        }
        return n;
    }

    public color getNodeColor() {
        return nodeColor;
    }

    public void setNodeColor(color nodeColor) {
        this.nodeColor = nodeColor;
    }

    @Override
    public int hashCode() {
        return data.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof GraphNode<?>) {
            return ((GraphNode<?>) obj).getData() == this.getData();
        }
        return false;
    }

    @Override
    public int compareTo(GraphNode<T> o) {
        return data.compareTo(o.data);
    }

    @Override
    public String toString() {
        return data + "";
    }
}
Graph<Integer> graph = new Graph<>();