- java.lang.Object
-
- com.github.moaxcp.graphs.AbstractGraph<ID>
-
- Type Parameters:
ID
- type of all identifiers in graph
- All Implemented Interfaces:
Graph<ID>
- Direct Known Subclasses:
DirectedGraph
,UndirectedGraph
public abstract class AbstractGraph<ID> extends Object implements Graph<ID>
This class provides a partial implementation of theGraph
interface.Vertices and edges are stored in insertion order.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
AbstractGraph.SimpleEdge
class
AbstractGraph.SimpleVertex
-
Nested classes/interfaces inherited from interface com.github.moaxcp.graphs.Graph
Graph.Edge<ID>, Graph.Vertex<ID>
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractGraph()
protected
AbstractGraph(ID id)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected Graph.Edge<ID>
addEdge(ID from, ID to)
protected Graph.Vertex<ID>
addVertex(ID id)
Graph.Edge<ID>
edge(ID from, ID to)
Ensures an edge exists between 'from' and 'to' vertices.Graph<ID>
edgeProperty(String name, Object value)
Sets edge property returning this graphboolean
equals(Object o)
Optional<Graph.Edge<ID>>
findEdge(ID id)
finds edge with given id.Optional<Graph.Edge<ID>>
findEdge(ID from, ID to)
Finds edge connecting 'from' and 'to' vertex.Optional<Graph.Vertex<ID>>
findVertex(ID id)
Finds vertex with given id.Map<ID,Graph.Edge<ID>>
getEdgeIds()
Returns a map of edge ids to edges.Map<String,Object>
getEdgeProperties()
Returns an unmodifiable map of edge properties.Optional<Object>
getEdgeProperty(String name)
ReturnsOptional
of an edge property.Collection<Graph.Edge<ID>>
getEdges()
Returns an ordered collection of all edges in this graph.Optional<ID>
getId()
Returns the id of this graph.Map<String,Object>
getProperties()
Returns an unmodifiable map of properties for this graph.Optional<Object>
getProperty(String name)
ReturnsOptional
of a property.Map<String,Object>
getVertexProperties()
Retruns an unmodifiable map of vertex properties.Optional<Object>
getVertexProperty(String name)
ReturnsOptional
of a vertex property.Map<ID,Graph.Vertex<ID>>
getVertices()
Returns an ordered map of all vertices in this graph.int
hashCode()
Graph<ID>
id(ID id)
Sets the id of this graphprotected Graph.Edge<ID>
newEdge(ID from, ID to, Map<String,Object> inherited)
protected abstract EdgeKey<ID>
newEdgeKey(ID from, ID to)
protected Graph.Vertex<ID>
newVertex(ID id, Map<String,Object> inherited)
Graph<ID>
property(String name, Object value)
Sets property on this graph returning this graphvoid
removeEdge(ID id)
Removes edge with given idvoid
removeEdge(ID from, ID to)
Removes edge with given from and to.Graph<ID>
removeEdgeProperty(String name)
Removes edge property returning this graphGraph<ID>
removeProperty(String name)
Removes property on this graph returning this graphvoid
removeVertex(ID id)
Removes vertex with given id.Graph<ID>
removeVertexProperty(String name)
Removes vertex property returning this graphvoid
setEdgeProperty(String name, Object value)
Sets edge property.void
setId(ID id)
Sets the id of this graph.void
setProperty(String name, Object value)
Sets property on this graphvoid
setVertexProperty(String name, Object value)
Sets vertex property.Graph.Vertex<ID>
vertex(ID id)
Ensure a vertex exists with id and returns the vertex.Graph<ID>
vertexProperty(String name, Object value)
Sets vertex property returning this graph-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.github.moaxcp.graphs.Graph
isDirected
-
-
-
-
Constructor Detail
-
AbstractGraph
protected AbstractGraph()
-
AbstractGraph
protected AbstractGraph(ID id)
-
-
Method Detail
-
getVertices
public Map<ID,Graph.Vertex<ID>> getVertices()
Description copied from interface:Graph
Returns an ordered map of all vertices in this graph. The key is the vertex id. Ordering is determined by implementing class.- Specified by:
getVertices
in interfaceGraph<ID>
- Returns:
- ordered map of all vertices in this graph
-
getEdges
public Collection<Graph.Edge<ID>> getEdges()
Description copied from interface:Graph
Returns an ordered collection of all edges in this graph.
-
getEdgeIds
public Map<ID,Graph.Edge<ID>> getEdgeIds()
Description copied from interface:Graph
Returns a map of edge ids to edges.- Specified by:
getEdgeIds
in interfaceGraph<ID>
- Returns:
- map of edge ids to edges
-
findVertex
public Optional<Graph.Vertex<ID>> findVertex(ID id)
Description copied from interface:Graph
Finds vertex with given id. If vertex is not in this graph the optional is empty.- Specified by:
findVertex
in interfaceGraph<ID>
- Parameters:
id
- of vertex- Returns:
- optional of vertex
-
vertex
public Graph.Vertex<ID> vertex(ID id)
Description copied from interface:Graph
Ensure a vertex exists with id and returns the vertex. If the vertex does not exist it is created.
-
addVertex
protected Graph.Vertex<ID> addVertex(ID id)
-
removeVertex
public void removeVertex(ID id)
Description copied from interface:Graph
Removes vertex with given id. If edges connect to the vertex they are removed first.- Specified by:
removeVertex
in interfaceGraph<ID>
- Parameters:
id
- of vertex
-
findEdge
public Optional<Graph.Edge<ID>> findEdge(ID from, ID to)
Description copied from interface:Graph
Finds edge connecting 'from' and 'to' vertex. If edge is not in this graph the optional is empty. The order of 'from' and 'to' does not matter in undirected implementations. However, if the graph is directed 'from' and 'to' must match 'from' and 'to' of the edge exactly.
-
findEdge
public Optional<Graph.Edge<ID>> findEdge(ID id)
Description copied from interface:Graph
finds edge with given id. If edge is not in this graph the optional is empty.
-
edge
public Graph.Edge<ID> edge(ID from, ID to)
Description copied from interface:Graph
Ensures an edge exists between 'from' and 'to' vertices. If an edge does not exist it is created. If any of the vertices do not exist they are created.If this graph is undirected and contains an edge connecting 'to' and 'from' the edge will not be created. Undirected graphs ignore 'from'/'to' order in edges.
-
addEdge
protected Graph.Edge<ID> addEdge(ID from, ID to)
-
removeEdge
public void removeEdge(ID from, ID to)
Description copied from interface:Graph
Removes edge with given from and to.- Specified by:
removeEdge
in interfaceGraph<ID>
- Parameters:
from
- of edgeto
- of edge
-
removeEdge
public void removeEdge(ID id)
Description copied from interface:Graph
Removes edge with given id- Specified by:
removeEdge
in interfaceGraph<ID>
- Parameters:
id
- of edge
-
getId
public Optional<ID> getId()
Description copied from interface:Graph
Returns the id of this graph. If the id is not set and empty optional is returned.
-
getProperties
public Map<String,Object> getProperties()
Description copied from interface:Graph
Returns an unmodifiable map of properties for this graph.- Specified by:
getProperties
in interfaceGraph<ID>
- Returns:
- an unmodifiable map of properties for this graph
-
getProperty
public Optional<Object> getProperty(String name)
Description copied from interface:Graph
- Specified by:
getProperty
in interfaceGraph<ID>
- Parameters:
name
- of property- Returns:
- optional of named proeprty
-
setProperty
public void setProperty(String name, Object value)
Description copied from interface:Graph
Sets property on this graph- Specified by:
setProperty
in interfaceGraph<ID>
- Parameters:
name
- of propertyvalue
- of property
-
property
public Graph<ID> property(String name, Object value)
Description copied from interface:Graph
Sets property on this graph returning this graph
-
removeProperty
public Graph<ID> removeProperty(String name)
Description copied from interface:Graph
Removes property on this graph returning this graph- Specified by:
removeProperty
in interfaceGraph<ID>
- Parameters:
name
- of property- Returns:
- this graph
-
getEdgeProperties
public Map<String,Object> getEdgeProperties()
Description copied from interface:Graph
Returns an unmodifiable map of edge properties.- Specified by:
getEdgeProperties
in interfaceGraph<ID>
- Returns:
- an unmodifiable map of edge properties
-
getEdgeProperty
public Optional<Object> getEdgeProperty(String name)
Description copied from interface:Graph
ReturnsOptional
of an edge property. If the property does not exist the returnedOptional
is empty.- Specified by:
getEdgeProperty
in interfaceGraph<ID>
- Parameters:
name
- of property- Returns:
- optional of property
-
setEdgeProperty
public void setEdgeProperty(String name, Object value)
Description copied from interface:Graph
Sets edge property.- Specified by:
setEdgeProperty
in interfaceGraph<ID>
- Parameters:
name
- of propertyvalue
- of property
-
edgeProperty
public Graph<ID> edgeProperty(String name, Object value)
Description copied from interface:Graph
Sets edge property returning this graph- Specified by:
edgeProperty
in interfaceGraph<ID>
- Parameters:
name
- of edge propertyvalue
- of edge property- Returns:
- this graph
-
removeEdgeProperty
public Graph<ID> removeEdgeProperty(String name)
Description copied from interface:Graph
Removes edge property returning this graph- Specified by:
removeEdgeProperty
in interfaceGraph<ID>
- Parameters:
name
- of property- Returns:
- this graph
-
getVertexProperties
public Map<String,Object> getVertexProperties()
Description copied from interface:Graph
Retruns an unmodifiable map of vertex properties.- Specified by:
getVertexProperties
in interfaceGraph<ID>
- Returns:
- an unmodifiable map of vertex properties
-
getVertexProperty
public Optional<Object> getVertexProperty(String name)
Description copied from interface:Graph
ReturnsOptional
of a vertex property. If the property does not exist the returnedOptional
is empty.- Specified by:
getVertexProperty
in interfaceGraph<ID>
- Parameters:
name
- of property- Returns:
- optional of property
-
setVertexProperty
public void setVertexProperty(String name, Object value)
Description copied from interface:Graph
Sets vertex property.- Specified by:
setVertexProperty
in interfaceGraph<ID>
- Parameters:
name
- of propertyvalue
- of property
-
vertexProperty
public Graph<ID> vertexProperty(String name, Object value)
Description copied from interface:Graph
Sets vertex property returning this graph- Specified by:
vertexProperty
in interfaceGraph<ID>
- Parameters:
name
- of propertyvalue
- of property- Returns:
- created or existing edge
-
removeVertexProperty
public Graph<ID> removeVertexProperty(String name)
Description copied from interface:Graph
Removes vertex property returning this graph- Specified by:
removeVertexProperty
in interfaceGraph<ID>
- Parameters:
name
- of property- Returns:
- this graph
-
-