forked from tinkerpop/blueprints
/
ReadOnlyGraph.java
100 lines (83 loc) · 3.17 KB
/
ReadOnlyGraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.tinkerpop.blueprints.pgm.util.wrappers.readonly;
import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.WrappableGraph;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
import com.tinkerpop.blueprints.pgm.util.wrappers.readonly.util.ReadOnlyEdgeSequence;
import com.tinkerpop.blueprints.pgm.util.wrappers.readonly.util.ReadOnlyVertexSequence;
/**
* A ReadOnlyGraph wraps a Graph and overrides the underlying graph's mutating methods.
* In this way, a ReadOnlyGraph can only be read from, not written to.
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class ReadOnlyGraph<T extends Graph> implements Graph, WrappableGraph<T> {
protected final T rawGraph;
public ReadOnlyGraph(final T rawGraph) {
this.rawGraph = rawGraph;
}
/**
* @throws UnsupportedOperationException
*/
public void removeVertex(final Vertex vertex) throws UnsupportedOperationException {
throw new UnsupportedOperationException(ReadOnlyTokens.MUTATE_ERROR_MESSAGE);
}
/**
* @throws UnsupportedOperationException
*/
public Edge addEdge(final Object id, final Vertex outVertex, final Vertex inVertex, final String label) throws UnsupportedOperationException {
throw new UnsupportedOperationException(ReadOnlyTokens.MUTATE_ERROR_MESSAGE);
}
public Vertex getVertex(final Object id) {
final Vertex vertex = this.rawGraph.getVertex(id);
if (null == vertex)
return null;
else
return new ReadOnlyVertex(vertex);
}
/**
* @throws UnsupportedOperationException
*/
public void removeEdge(final Edge edge) throws UnsupportedOperationException {
throw new UnsupportedOperationException(ReadOnlyTokens.MUTATE_ERROR_MESSAGE);
}
public Iterable<Edge> getEdges() {
return new ReadOnlyEdgeSequence(this.rawGraph.getEdges().iterator());
}
public Edge getEdge(final Object id) {
final Edge edge = this.rawGraph.getEdge(id);
if (null == edge)
return null;
else
return new ReadOnlyEdge(edge);
}
public Iterable<Vertex> getVertices() {
return new ReadOnlyVertexSequence(this.rawGraph.getVertices().iterator());
}
/**
* @throws UnsupportedOperationException
*/
public Vertex addVertex(final Object id) throws UnsupportedOperationException {
throw new UnsupportedOperationException(ReadOnlyTokens.MUTATE_ERROR_MESSAGE);
}
/**
* @throws UnsupportedOperationException
*/
public void clear() throws UnsupportedOperationException {
throw new UnsupportedOperationException(ReadOnlyTokens.MUTATE_ERROR_MESSAGE);
}
/**
* @throws UnsupportedOperationException
*/
public void shutdown() throws UnsupportedOperationException {
throw new UnsupportedOperationException(ReadOnlyTokens.MUTATE_ERROR_MESSAGE);
}
public String toString() {
return StringFactory.graphString(this, this.rawGraph.toString());
}
@Override
public T getRawGraph() {
return this.rawGraph;
}
}