Skip to content
Arc.java 1.58 KiB
Newer Older
Javier Costa's avatar
Javier Costa committed
package tfm.arcs;
Javier Costa's avatar
Javier Costa committed

Javier Costa's avatar
Javier Costa committed
import tfm.arcs.data.ArcData;
Javier Costa's avatar
Javier Costa committed
import tfm.nodes.Node;
Javier Costa's avatar
Javier Costa committed

import java.util.Objects;

Javier Costa's avatar
Javier Costa committed
public abstract class Arc<D extends ArcData> extends edg.graphlib.Arrow<String, D> {

    @SuppressWarnings("unchecked")
Javier Costa's avatar
Javier Costa committed
    public Arc(Node from, Node to) {
Javier Costa's avatar
Javier Costa committed
        super((edg.graphlib.Vertex<String, D>) from, (edg.graphlib.Vertex<String, D>) to);
    }

    public abstract boolean isControlFlowArrow();

    public abstract boolean isControlDependencyArrow();

    public abstract boolean isDataDependencyArrow();

    @Override
    public String toString() {
        return String.format("Arc{data: %s, %s -> %s}",
                getData(),
                getFrom(),
                getTo()
        );
    }

    public String toGraphvizRepresentation() {
Javier Costa's avatar
Javier Costa committed
        Node from = (Node) getFrom();
        Node to = (Node) getTo();
Javier Costa's avatar
Javier Costa committed
        return String.format("%s -> %s",
                from.getId(),
                to.getId()
        );
Javier Costa's avatar
Javier Costa committed
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getData()) + getFrom().hashCode() + getTo().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;

        if (!(o instanceof Arc))
            return false;

        Arc arc = (Arc) o;

Javier Costa's avatar
Javier Costa committed
        Node from = (Node) arc.getFrom();
        Node from2 = (Node) getFrom();
        Node to = (Node) getTo();
        Node to2 = (Node) arc.getTo();

        return Objects.equals(arc.getData(), getData()) &&
Javier Costa's avatar
Javier Costa committed
                Objects.equals(from.getId(), from2.getId()) &&
                Objects.equals(to.getId(), to2.getId());
Javier Costa's avatar
Javier Costa committed
}