org.fabric3.util.graph.DirectedGraphImpl Maven / Gradle / Ivy
The newest version!
/*
* Fabric3
* Copyright (c) 2009-2013 Metaform Systems
*
* Fabric3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version, with the
* following exception:
*
* Linking this software statically or dynamically with other
* modules is making a combined work based on this software.
* Thus, the terms and conditions of the GNU General Public
* License cover the whole combination.
*
* As a special exception, the copyright holders of this software
* give you permission to link this software with independent
* modules to produce an executable, regardless of the license
* terms of these independent modules, and to copy and distribute
* the resulting executable under terms of your choice, provided
* that you also meet, for each linked independent module, the
* terms and conditions of the license of that module. An
* independent module is a module which is not derived from or
* based on this software. If you modify this software, you may
* extend this exception to your version of the software, but
* you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version.
*
* Fabric3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the
* GNU General Public License along with Fabric3.
* If not, see .
*/
package org.fabric3.util.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Default directed graph implementation.
*/
public class DirectedGraphImpl implements DirectedGraph {
private Map, VertexHolder> graphVertices;
private Set> graphEdges;
public DirectedGraphImpl() {
graphVertices = new HashMap, VertexHolder>();
graphEdges = new HashSet>();
}
public Set> getVertices() {
return graphVertices.keySet();
}
public void add(Vertex vertex) {
if (graphVertices.containsKey(vertex)) {
return;
}
graphVertices.put(vertex, new VertexHolder());
}
public void remove(Vertex vertex) {
List> edges = new ArrayList>(getOutgoingEdges(vertex));
for (Edge edge : edges) {
removeEdge(edge);
}
graphVertices.remove(vertex);
}
public Set> getAdjacentVertices(Vertex vertex) {
Set> adjacentVertices = new HashSet>();
Set> incidentEdges = getOutgoingEdges(vertex);
if (incidentEdges != null) {
for (Edge edge : incidentEdges) {
adjacentVertices.add(edge.getOppositeVertex(vertex));
}
}
return adjacentVertices;
}
public List> getOutgoingAdjacentVertices(Vertex vertex) {
return getAdjacentVertices(vertex, true);
}
public List> getIncomingAdjacentVertices(Vertex vertex) {
return getAdjacentVertices(vertex, false);
}
public Edge getEdge(Vertex source, Vertex sink) {
Set> edges = getOutgoingEdges(source);
for (Edge edge : edges) {
if (edge.getSink() == sink) {
return edge;
}
}
return null;
}
public Set> getOutgoingEdges(Vertex vertex) {
return graphVertices.get(vertex).getOutgoingEdges();
}
public Set> getIncomingEdges(Vertex vertex) {
return graphVertices.get(vertex).getIncomingEdges();
}
public Set> getEdges() {
return graphEdges;
}
public void add(Edge edge) {
if (graphEdges.contains(edge)) {
return;
}
Vertex source = edge.getSource();
Vertex sink = edge.getSink();
if (!graphVertices.containsKey(source)) {
add(source);
}
if ((sink != source) && !graphVertices.containsKey(sink)) {
add(sink);
}
Set> sourceEdges = getOutgoingEdges(source);
sourceEdges.add(edge);
if (source != sink) {
// avoid adding the edge a second time if the edge points back on itself
Set> sinkEdges = getIncomingEdges(sink);
sinkEdges.add(edge);
}
graphEdges.add(edge);
VertexHolder sourceHolder = graphVertices.get(edge.getSource());
VertexHolder sinkHolder = graphVertices.get(edge.getSink());
sourceHolder.getOutgoingEdges().add(edge);
sinkHolder.getIncomingEdges().add(edge);
}
public void remove(Edge edge) {
removeEdge(edge);
Vertex source = edge.getSource();
Vertex sink = edge.getSink();
VertexHolder sourceHolder = graphVertices.get(source);
VertexHolder sinkHolder = graphVertices.get(sink);
// remove the edge from the source's outgoing edges
sourceHolder.getOutgoingEdges().remove(edge);
// remove the edge from the sink's incoming edges
sinkHolder.getIncomingEdges().remove(edge);
}
private void removeEdge(Edge edge) {
// Remove the edge from the vertices incident edges.
Vertex source = edge.getSource();
Set> sourceEdges = getOutgoingEdges(source);
sourceEdges.remove(edge);
Vertex sink = edge.getSink();
Set> sinkEdges = getIncomingEdges(sink);
sinkEdges.remove(edge);
// Remove the edge from edgeSet
graphEdges.remove(edge);
}
/**
* Returns the outgoing or incoming adjacent vertices for a given vertex
*
* @param vertex the vertex.
* @param outGoing true for returning outgoing vertices.
* @return the adjacent vertices
*/
private List> getAdjacentVertices(Vertex vertex, boolean outGoing) {
List> adjacentVertices = new ArrayList>();
Set> edges;
if (outGoing) {
edges = getOutgoingEdges(vertex);
} else {
edges = getIncomingEdges(vertex);
}
for (Edge edge : edges) {
Vertex oppositeVertex = edge.getOppositeVertex(vertex);
adjacentVertices.add(oppositeVertex);
}
return adjacentVertices;
}
private class VertexHolder {
private Set> incoming = new HashSet>();
private Set> outgoingEdges = new HashSet>();
public Set> getIncomingEdges() {
return incoming;
}
public Set> getOutgoingEdges() {
return outgoingEdges;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy