org.grinvin.graphs

Class DefaultGraph

Implemented Interfaces:
Graph, GraphView, UndoableGraph
Known Direct Subclasses:
DefaultGraphModel

public class DefaultGraph
extends java.lang.Object
implements UndoableGraph

Default implementation of UndoableGraph.

Nested Class Summary

protected class
DefaultGraph.EdgeIterator
Iterator over all edges of this graph.
protected class
DefaultGraph.VertexIterator
Iterator over all vertices of this graph.

Field Summary

protected Collection
edges
Collection of edges for this graph.

Constructor Summary

DefaultGraph()
Creates a new empty graph.

Method Summary

Edge
addNewEdge(Vertex firstEndpoint, Vertex secondEndpoint)
Add a new edge to the graph which joins the given (existing) vertices.
Vertex
addNewVertex()
Create a new vertex and add it to the graph.
boolean
areAdjacent(Vertex v1, Vertex v2)
void
clear()
Remove all vertices and all edges from this graph.
boolean
contains(Edge edge)
boolean
contains(Vertex vertex)
void
copy(GraphView original)
Make this graph a copy of the given view.
Iterator
edgeIterator()
Iterable
edges()
protected void
finalizeRemoveEdge(Edge edge)
This method is called after an edge has been removed by the method remove(Edge) or by DefaultGraph.EdgeIterator.remove().
protected void
finalizeRemoveVertex(Vertex v)
This method is called after a vertex has been removed by the method remove(Vertex) or by DefaultGraph.VertexIterator.remove().
Edge
getEdge(Vertex first, Vertex second)
int
getModCount()
Returns the modification count of this graph.
int
getNumberOfEdges()
int
getNumberOfVertices()
Vertex
getVertex(int index)
protected void
remove(int index)
Remove vertex at given index position.
void
remove(Edge e)
Remove the given edge from the graph.
void
remove(Vertex v)
Remove the given vertex from the graph.
void
restore(Edge e)
void
restore(Vertex v)
Iterator
vertexIterator()
Iterable
vertices()

Field Details

edges

protected Collection edges
Collection of edges for this graph.

Constructor Details

DefaultGraph

public DefaultGraph()
Creates a new empty graph.

Method Details

addNewEdge

public Edge addNewEdge(Vertex firstEndpoint,
                       Vertex secondEndpoint)
Add a new edge to the graph which joins the given (existing) vertices.
Specified by:
addNewEdge in interface Graph
Returns:
the newly created edge.

addNewVertex

public Vertex addNewVertex()
Create a new vertex and add it to the graph. The index of this newly created vertex should be equal to the number of vertices in the graph before that vertex was added.
Specified by:
addNewVertex in interface Graph
Returns:
the newly created vertex.

areAdjacent

public boolean areAdjacent(Vertex v1,
                           Vertex v2)
Specified by:
areAdjacent in interface GraphView

clear

public void clear()
Remove all vertices and all edges from this graph.
Specified by:
clear in interface Graph

contains

public boolean contains(Edge edge)
Specified by:
contains in interface GraphView

contains

public boolean contains(Vertex vertex)
Specified by:
contains in interface GraphView

copy

public void copy(GraphView original)
Make this graph a copy of the given view. Corresponding vertices will have the same index in the copy as in the original. This graph will be cleared prior to the copy operation.
Specified by:
copy in interface Graph

edgeIterator

public Iterator edgeIterator()
Specified by:
edgeIterator in interface GraphView

edges

public Iterable edges()
Specified by:
edges in interface GraphView

finalizeRemoveEdge

protected void finalizeRemoveEdge(Edge edge)
This method is called after an edge has been removed by the method remove(Edge) or by DefaultGraph.EdgeIterator.remove(). This implementation is empty.

finalizeRemoveVertex

protected void finalizeRemoveVertex(Vertex v)
This method is called after a vertex has been removed by the method remove(Vertex) or by DefaultGraph.VertexIterator.remove(). This implementation removes all edges having the given vertex as an endpoint.

getEdge

public Edge getEdge(Vertex first,
                    Vertex second)
Specified by:
getEdge in interface GraphView

getModCount

public int getModCount()
Returns the modification count of this graph. This count is incremented every time the graph is changed and serves as a kind of version number for this object.

getNumberOfEdges

public int getNumberOfEdges()
Specified by:
getNumberOfEdges in interface GraphView

getNumberOfVertices

public int getNumberOfVertices()
Specified by:
getNumberOfVertices in interface GraphView

getVertex

public Vertex getVertex(int index)
Specified by:
getVertex in interface GraphView

remove

protected void remove(int index)

remove

public void remove(Edge e)
Remove the given edge from the graph.
Specified by:
remove in interface Graph

remove

public void remove(Vertex v)
Remove the given vertex from the graph. Also removes all edges incident with this vertex. Removing a vertex makes its index negative and may change the indices of other vertices in the same graph.
Specified by:
remove in interface Graph

restore

public void restore(Edge e)
Specified by:
restore in interface UndoableGraph

restore

public void restore(Vertex v)
Specified by:
restore in interface UndoableGraph

vertexIterator

public Iterator vertexIterator()
Specified by:
vertexIterator in interface GraphView

vertices

public Iterable vertices()
Specified by:
vertices in interface GraphView