org.grinvin

Class DefaultGraph

Implemented Interfaces:
Graph, GraphView
Known Direct Subclasses:
DefaultGraphModel

public class DefaultGraph
extends java.lang.Object
implements Graph

Default implementation of Graph.

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, Object annotation)
Add a new edge to the graph which joins the given (existing) vertices.
Vertex
addNewVertex(Object annotation)
Create a new vertex and add it to the graph.
boolean
areAdjacent(Vertex v1, Vertex v2)
Check whether the given vertices are adjacent in this graph.
void
change(Edge edge, Object annotation)
Change or remove the annotation of the given edge.
void
change(Vertex vertex, Object annotation)
Change or remove the annotation of the given vertex.
void
clear()
Remove all vertices and all edges from this graph.
boolean
contains(Edge edge)
Does the given edge belong to this graph?
boolean
contains(Vertex vertex)
Does the given vertex belong to this graph?
void
copy(GraphView original)
Make this graph a copy of the given view.
Iterator
edgeIterator()
Iterator over all edges of this graph.
Iterable
edges()
Set of edges of this graph.
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)
Return an edge joining the given vertices, or null when no such edge exists.
int
getModCount()
Returns the modification count of this graph.
int
getNumberOfEdges()
Returns the size, i.e.
int
getNumberOfVertices()
Returns the order, i.e.
Vertex
getVertex(int index)
Returns the vertex with the given index in the graph.
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)
Restore a edge which was previously removed from the graph.
void
restore(Vertex v)
Restore a vertex which was previously removed from the graph.
Iterator
vertexIterator()
Iterator over all vertices of this graph.
Iterable
vertices()
Set of vertices of this graph.

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,
                       Object annotation)
Add a new edge to the graph which joins the given (existing) vertices.
Specified by:
addNewEdge in interface Graph
Parameters:
annotation - optional annotation for this edge (or null).
Returns:
the newly created edge.

addNewVertex

public Vertex addNewVertex(Object annotation)
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
Parameters:
annotation - optional annotation for this vertex (or null).
Returns:
the newly created vertex.

areAdjacent

public boolean areAdjacent(Vertex v1,
                           Vertex v2)
Check whether the given vertices are adjacent in this graph. Should return the same result as
    getEdge (first,second) != null || getEgde (second,first) != null
 
Specified by:
areAdjacent in interface GraphView

change

public void change(Edge edge,
                   Object annotation)
Change or remove the annotation of the given edge.
Specified by:
change in interface Graph
Parameters:
edge - Edge whose annotation should be changed
annotation - new annotation for this element, or null to request removal

change

public void change(Vertex vertex,
                   Object annotation)
Change or remove the annotation of the given vertex.
Specified by:
change in interface Graph
Parameters:
vertex - Vertex whose annotation should be changed
annotation - new annotation for this element, or null to request removal

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)
Does the given edge belong to this graph?
Specified by:
contains in interface GraphView

contains

public boolean contains(Vertex vertex)
Does the given vertex belong to this graph?
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()
Iterator over all edges of this graph.
Specified by:
edgeIterator in interface GraphView

edges

public Iterable edges()
Set of edges of this graph. This method returns an Iterable and not a java.util.Set so its main purpose is to be used in a 'for each' loop:
    for (Edge e: graph.edges()) {
        // do something with e
    }
 
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)
Return an edge joining the given vertices, or null when no such edge exists.
Specified by:
getEdge in interface GraphView
Returns:
an edge having first as its first endpoint and second as its second endpoint.

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()
Returns the size, i.e. the number of edges, of the graph.
Specified by:
getNumberOfEdges in interface GraphView

getNumberOfVertices

public int getNumberOfVertices()
Returns the order, i.e. the number of vertices, of the graph.
Specified by:
getNumberOfVertices in interface GraphView

getVertex

public Vertex getVertex(int index)
Returns the vertex with the given index in the graph. Vertices in a graph are indexed in the range 0..n-1 where n is the number of vertices. Note that the index of a vertex is allowed to change whenever the graph is modified. Every graph should honour the following invariant whenever index is in the correct range:
    getVertex(index).getIndex() == 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)
Restore a edge which was previously removed from the graph.

This method is intended for use by the undo/redo framework and should be called with care. It is supposed to undo a prior removal of that same edge. Clients are urged to instead use Graph.addNewEdge(Vertex,Vertex,Object) whenever possible.

Specified by:
restore in interface Graph
Parameters:
e - Edge that should be restored. This must be a edge that was previously removed from the same graph.

restore

public void restore(Vertex v)
Restore a vertex which was previously removed from the graph. The vertex should end up at the same index position as it had before.

This method is intended for use by the undo/redo framework and should be called with care. It is supposed to undo a prior removal of that same vertex. Clients are urged to instead use Graph.addNewVertex(Object) whenever possible.

Specified by:
restore in interface Graph
Parameters:
v - Vertex that should be restored. This must be a vertex that was previously removed from the same graph.

vertexIterator

public Iterator vertexIterator()
Iterator over all vertices of this graph. If this iterator supports the remove operation, it should also remove all edges incident with the vertex that is removed.
Specified by:
vertexIterator in interface GraphView

vertices

public Iterable vertices()
Set of vertices of this graph. This method returns an Iterable and not a java.util.Set so its main purpose is to be used in a 'for each' loop:
    for (Vertex v: graph.vertices()) {
        // do something with v
    }
 
Specified by:
vertices in interface GraphView