
org.fabric3.util.graph.DirectedGraphImpl Maven / Gradle / Ivy
The newest version!
/*
* Fabric3
* Copyright (c) 2009-2015 Metaform Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Portions originally based on Apache Tuscany 2007
* licensed under the Apache 2.0 license.
*/
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<>();
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