All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yworks.util.graph.Edge Maven / Gradle / Ivy

Go to download

The open-source Java obfuscation tool working with Ant and Gradle by yWorks - the diagramming experts

There is a newer version: 4.1.1
Show newest version
package com.yworks.util.graph;

import com.google.common.graph.EndpointPair;
import com.google.common.graph.Network;

import java.util.Set;

public class Edge {
    private Network network;

    public Edge(Network network) {
        this.network = network;
    }

    /**
     * Returns the target node of the edge.
     * @return {Node}
     */
    public Node target() {
        return this.network.incidentNodes(this).target();
    }

    /**
     * Returns the source node of the edge
     * @return {Node}
     */
    public Node source() {
        return this.network.incidentNodes(this).source();
    }

    /**
     * Returns the next edge (in insertion order) connecting source and target node, if any.
     * @return {Edge|null}
     */
    public Edge nextInEdge() {
        Set inEdges = this.network.inEdges(this.target());
        boolean found = false;
        for (final Edge edge: inEdges) {
            if (found) {
                return edge;
            } else if (edge.equals(this)) {
                found = true;
            }
        }
        return null;
    }

    /**
     * Returns the next edge (in insertion order) that is outgoing of target node, if any.
     * @return {Edge|null}
     */
    public Edge nextOutEdge() {
        Set outEdges = this.network.outEdges(this.network.incidentNodes(this).source());
        boolean found = false;
        for (final Edge edge: outEdges) {
            if (found) {
                return edge;
            } else if (edge.equals(this)) {
                found = true;
            }
        }
        return null;
    }

    /**
     * Returns the edge going in opposite direction of this edge in insertion order, respectively to the given node, if any.
     * @param node
     * @return {Node}
     */
    public Node opposite(Node node) {
        EndpointPair endpointPair = this.network.incidentNodes(this);
        if (endpointPair.source().equals(node)) {
            return endpointPair.target();
        } else {
            return endpointPair.source();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy