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

org.jruby.ir.util.Vertex Maven / Gradle / Ivy

There is a newer version: 0.8.14
Show newest version
package org.jruby.ir.util;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 *
 */
public class Vertex implements Comparable> {
    private DirectedGraph graph;
    private T data;
    private Set> incoming = null;
    private Set> outgoing = null;
    int id;
    
    public Vertex(DirectedGraph graph, T data, int id) {
        this.graph = graph;
        this.data = data;
        this.id = id;
    }
    
    public void addEdgeTo(Vertex destination) {
        addEdgeTo(destination, null);
    }

    public void addEdgeTo(Vertex destination, Object type) {
        Edge edge = new Edge(this, destination, type);
        getOutgoingEdges().add(edge);
        destination.getIncomingEdges().add(edge);
        graph.edges().add(edge);
    }
    
    public void addEdgeTo(T destination) {
        addEdgeTo(destination, null);
    }    
    
    public void addEdgeTo(T destination, Object type) {
        Vertex destinationVertex = graph.vertexFor(destination);
        
        addEdgeTo(destinationVertex, type);
    }
    
    public boolean removeEdgeTo(Vertex destination) {
        for (Edge edge: getOutgoingEdges()) {
            if (edge.getDestination() == destination) {
                getOutgoingEdges().remove(edge);
                edge.getDestination().getIncomingEdges().remove(edge);
                graph.edges().remove(edge);
                return true;
            }
        }
        
        return false;
    }
    
    public void removeAllIncomingEdges() {
        for (Edge edge: getIncomingEdges()) {
            edge.getSource().getOutgoingEdges().remove(edge);
            graph.edges().remove(edge);
        }
        incoming = null;
    }
    
    public void removeAllOutgoingEdges() {
        for (Edge edge: getOutgoingEdges()) {
            edge.getDestination().getIncomingEdges().remove(edge);
            graph.edges().remove(edge);
        }
        outgoing = null;
    }

    public void removeAllEdges() {
        removeAllIncomingEdges();
        removeAllOutgoingEdges();
    }

    public int inDegree() {
        return (incoming == null) ? 0 : incoming.size();
    }

    public int outDegree() {
        return (outgoing == null) ? 0 : outgoing.size();
    }
    
    public Iterable> getIncomingEdgesOfType(Object type) {
        return new EdgeTypeIterable(getIncomingEdges(), type);
    }

    public Iterable> getIncomingEdgesNotOfType(Object type) {
        return new EdgeTypeIterable(getIncomingEdges(), type, true);
    }
    
    public Iterable> getOutgoingEdgesOfType(Object type) {
        return new EdgeTypeIterable(getOutgoingEdges(), type);
    }

    public T getIncomingSourceData() {
        Edge edge = getSingleEdge(getIncomingEdges().iterator(), "");
        
        return edge == null ? null : edge.getSource().getData();  
    }    
    
    public T getIncomingSourceDataOfType(Object type) {
        Edge edge = getSingleEdge(getIncomingEdgesOfType(type).iterator(), type);
        
        return edge == null ? null : edge.getSource().getData();
    }    
    
    public Iterable getIncomingSourcesData() {
        return new DataIterable(getIncomingEdges(), null, true, true);
    }
    
    public Iterable getIncomingSourcesDataOfType(Object type) {
        return new DataIterable(getIncomingEdges(), type, true, false);
    }      
    
    public Iterable getIncomingSourcesDataNotOfType(Object type) {
        return new DataIterable(getIncomingEdges(), type, true, true);
    }      
        
    public Iterable> getOutgoingEdgesNotOfType(Object type) {
        return new EdgeTypeIterable(getOutgoingEdges(), type, true);
    }
    
    public Iterable getOutgoingDestinationsData() {
        return new DataIterable(getOutgoingEdges(), null, false, true);
    }

    public Iterable getOutgoingDestinationsDataOfType(Object type) {
        return new DataIterable(getOutgoingEdges(), type, false, false);
    }
    
    public Iterable getOutgoingDestinationsDataNotOfType(Object type) {
        return new DataIterable(getOutgoingEdges(), type, false, true);
    }

    public T getOutgoingDestinationData() {
        Edge edge = getSingleEdge(getOutgoingEdges().iterator(), "");
        
        return edge == null ? null : edge.getDestination().getData();  
    }    
    
    public T getOutgoingDestinationDataOfType(Object type) {
        Edge edge = getSingleEdge(getOutgoingEdgesOfType(type).iterator(), type);
        
        return edge == null ? null : edge.getDestination().getData();
    }
    
    private Edge getSingleEdge(Iterator> iterator, Object type) {
        if (iterator.hasNext()) {
            Edge edge = iterator.next();
            
            assert !iterator.hasNext() : "Should only be one edge of type " + type;
            
            return edge;
        }
        
        return null;
    }
    
    public Edge getIncomingEdgeOfType(Object type) {
        return getSingleEdge(getIncomingEdgesOfType(type).iterator(), type);
    }

    public Edge getOutgoingEdgeOfType(Object type) {
        return getSingleEdge(getOutgoingEdgesOfType(type).iterator(), type);
    }
    
    /**
     * Get single incoming edge of any type and assert if there is more than
     * one.
     */
    public Edge getIncomingEdge() {
        return getSingleEdge(getIncomingEdgesNotOfType(null).iterator(), null);
    }
    
    /**
     * Get single outgoing edge of any type and assert if there is more than
     * one.
     */
    public Edge getOutgoingEdge() {
        return getSingleEdge(getOutgoingEdgesNotOfType(null).iterator(), null);
    }

    public Set> getIncomingEdges() {
        if (incoming == null) incoming = new HashSet>();
        
        return incoming;
    }
    
    public Set> getOutgoingEdges() {
        if (outgoing == null) outgoing = new HashSet>();
        
        return outgoing;
    }    
    
    public T getData() {
        return data;
    }
    
    public int getID() {
        return data instanceof ExplicitVertexID ? ((ExplicitVertexID) data).getID() : id;
    }
    
    @Override
    public String toString() {
        boolean found = false;
        StringBuilder buf = new StringBuilder(data.toString());

        buf.append(":");
        
        Set> edges = getOutgoingEdges();
        int size = edges.size();
        
        if (size > 0) {
            found = true;
            buf.append(">[");
            Iterator> iterator = edges.iterator();
            
            for (int i = 0; i < size - 1; i++) {
                buf.append(iterator.next().getDestination().getID()).append(",");
            }
            buf.append(iterator.next().getDestination().getID()).append("]");
        }

        edges = getIncomingEdges();
        size = edges.size();
        
        if (size > 0) {
            if (found) buf.append(", ");
            buf.append("<[");            
            Iterator> iterator = edges.iterator();

            for (int i = 0; i < size - 1; i++) {
                buf.append(iterator.next().getSource().getID()).append(",");
            }
            buf.append(iterator.next().getSource().getID()).append("]");
        }
        buf.append("\n");
        
        return buf.toString();
    }

    public int compareTo(Vertex that) {
        if (getID() == that.getID()) return 0;
        if (getID() < that.getID()) return -1;
        return 1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy