0
# Abstract Base Classes
1
2
Foundation classes that provide common graph functionality and establish consistent interfaces across all JUNG graph implementations. These abstract classes handle shared operations like edge validation, vertex relationships, and common graph queries.
3
4
## Capabilities
5
6
### AbstractGraph
7
8
Base implementation of the Graph interface providing common functionality for all graph types.
9
10
```java { .api }
11
/**
12
* Abstract implementation of the Graph interface designed to simplify
13
* implementation of new graph classes.
14
*/
15
public abstract class AbstractGraph<V,E> implements Graph<V,E>, Serializable {
16
17
// Edge addition methods
18
public boolean addEdge(E edge, Collection<? extends V> vertices);
19
public boolean addEdge(E edge, Collection<? extends V> vertices, EdgeType edgeType);
20
public boolean addEdge(E e, V v1, V v2);
21
public boolean addEdge(E e, V v1, V v2, EdgeType edge_type);
22
public boolean addEdge(E edge, Pair<? extends V> endpoints);
23
public abstract boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType);
24
25
// Vertex degree and relationship queries
26
public int inDegree(V vertex);
27
public int outDegree(V vertex);
28
public boolean isPredecessor(V v1, V v2);
29
public boolean isSuccessor(V v1, V v2);
30
public int getPredecessorCount(V vertex);
31
public int getSuccessorCount(V vertex);
32
public boolean isNeighbor(V v1, V v2);
33
public boolean isIncident(V vertex, E edge);
34
public int getNeighborCount(V vertex);
35
public int degree(V vertex);
36
37
// Edge queries and navigation
38
public int getIncidentCount(E edge);
39
public V getOpposite(V vertex, E edge);
40
public E findEdge(V v1, V v2);
41
public Collection<E> findEdgeSet(V v1, V v2);
42
public Collection<V> getIncidentVertices(E edge);
43
44
// Utility methods
45
public String toString();
46
47
// Protected helper methods
48
protected Pair<V> getValidatedEndpoints(E edge, Pair<? extends V> endpoints);
49
}
50
```
51
52
**Usage Examples:**
53
54
```java
55
// AbstractGraph is extended by concrete implementations
56
public class CustomGraph<V,E> extends AbstractGraph<V,E> {
57
@Override
58
public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) {
59
// Implementation specific logic
60
return true;
61
}
62
63
// Implement other required abstract methods
64
}
65
66
// Common operations available on all graph implementations
67
Graph<String, String> graph = new DirectedSparseGraph<>();
68
graph.addVertex("A");
69
graph.addVertex("B");
70
graph.addEdge("edge1", "A", "B");
71
72
// These methods work on any AbstractGraph subclass
73
int degree = graph.degree("A"); // Get vertex degree
74
boolean connected = graph.isNeighbor("A", "B"); // Check if vertices are neighbors
75
String opposite = graph.getOpposite("A", "edge1"); // Get opposite vertex of edge
76
```
77
78
### AbstractTypedGraph
79
80
Abstract class for graphs where all edges have the same EdgeType, simplifying implementation of homogeneous graph types.
81
82
```java { .api }
83
/**
84
* An abstract class for graphs whose edges all have the same EdgeType.
85
* Intended to simplify the implementation of such graph classes.
86
*/
87
public abstract class AbstractTypedGraph<V,E> extends AbstractGraph<V,E> {
88
89
/**
90
* Creates an instance with the specified edge type
91
* @param edge_type The edge type for all edges in this graph
92
*/
93
public AbstractTypedGraph(EdgeType edge_type);
94
95
/**
96
* Returns this graph's edge type
97
* @return The default edge type for this graph
98
*/
99
public EdgeType getDefaultEdgeType();
100
101
/**
102
* Returns this graph's edge type, or null if e is not in this graph
103
* @param e The edge to check
104
* @return Edge type or null if edge not found
105
*/
106
public EdgeType getEdgeType(E e);
107
108
/**
109
* Returns the edge set for this graph if edgeType matches default type
110
* @param edge_type The edge type to filter by
111
* @return Collection of edges of specified type
112
*/
113
public Collection<E> getEdges(EdgeType edge_type);
114
115
/**
116
* Returns the edge count for this graph if edge_type matches default type
117
* @param edge_type The edge type to count
118
* @return Number of edges of specified type
119
*/
120
public int getEdgeCount(EdgeType edge_type);
121
122
// Protected fields and methods
123
protected final EdgeType edge_type;
124
protected boolean hasEqualEdgeType(EdgeType edge_type);
125
protected void validateEdgeType(EdgeType edge_type);
126
}
127
```
128
129
**Usage Examples:**
130
131
```java
132
// AbstractTypedGraph ensures all edges have the same type
133
DirectedSparseGraph<String, String> directedGraph = new DirectedSparseGraph<>();
134
// All edges in this graph will be DIRECTED
135
136
UndirectedSparseGraph<String, String> undirectedGraph = new UndirectedSparseGraph<>();
137
// All edges in this graph will be UNDIRECTED
138
139
// Query edge type information
140
EdgeType type = directedGraph.getDefaultEdgeType(); // Returns EdgeType.DIRECTED
141
Collection<String> directedEdges = directedGraph.getEdges(EdgeType.DIRECTED);
142
int directedCount = directedGraph.getEdgeCount(EdgeType.DIRECTED);
143
144
// This would return empty/0 since graph only has directed edges
145
Collection<String> undirectedEdges = directedGraph.getEdges(EdgeType.UNDIRECTED);
146
```
147
148
## Design Patterns
149
150
### Template Method Pattern
151
152
`AbstractGraph` uses the template method pattern where concrete subclasses implement the abstract `addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType)` method while inheriting common behavior for other edge addition variants.
153
154
### Type Safety
155
156
`AbstractTypedGraph` enforces type safety by validating that all operations maintain the graph's declared edge type, preventing mixed directed/undirected edges in homogeneous graph implementations.
157
158
### Delegation
159
160
Both abstract classes delegate complex operations to simpler primitive operations, allowing subclasses to implement minimal functionality while inheriting comprehensive graph behavior.