meaningless comment
[ephemerata.git] / KezvhLib / src-lib / net / kezvh / collections / graphs / directed / WrappedKeySet.java
blobfc4eaa8e7774ebb795a72ed1e023fbf3094b2773
1 /**
3 */
4 package net.kezvh.collections.graphs.directed;
6 import java.util.AbstractSet;
7 import java.util.Iterator;
8 import java.util.Set;
9 import java.util.Map.Entry;
11 import net.kezvh.collections.views.SetView;
12 import net.kezvh.functional.Operations;
13 import net.kezvh.functional.lambda.L1;
15 import com.google.common.collect.AbstractMapEntry;
17 /**
18 * @author afflux
19 * @param <Node> node type
20 * @param <NodeValue> node value type
21 * @param <EdgeValue> edge value type
23 class WrappedKeySet<Node, NodeValue, EdgeValue> extends AbstractSet<Node> implements DirectedGraphWithEdgeValues<Node, EdgeValue> {
24 private final DirectedGraphWithValues<Node, NodeValue, EdgeValue> wrappedGraph;
26 /**
27 * @param wrappedGraph wrapped graph
29 public WrappedKeySet(final DirectedGraphWithValues<Node, NodeValue, EdgeValue> wrappedGraph) {
30 this.wrappedGraph = wrappedGraph;
33 @Override
34 public boolean containsEdge(final Node source, final Node destination) {
35 return this.wrappedGraph.containsEdge(source, destination);
38 @Override
39 public long edgeCount() {
40 return this.wrappedGraph.edgeCount();
43 @Override
44 public int inDegree(final Node destination) {
45 return this.wrappedGraph.inDegree(destination);
48 @Override
49 public int outDegree(final Node source) {
50 return this.wrappedGraph.outDegree(source);
53 @Override
54 public Set<Node> adjacentFrom(final Node source) {
55 return this.wrappedGraph.adjacentFrom(source);
58 @Override
59 public Set<Node> adjacentTo(final Node destination) {
60 return this.wrappedGraph.adjacentTo(destination);
63 @Override
64 public EdgeValue addEdge(final Node source, final Node destination, final EdgeValue edgeValue) {
65 return this.wrappedGraph.addEdge(source, destination, edgeValue);
68 @Override
69 public EdgeValue getEdgeValue(final Node source, final Node destination) {
70 return this.wrappedGraph.getEdgeValue(source, destination);
73 @Override
74 public EdgeValue removeEdge(final Node source, final Node destination) {
75 return this.wrappedGraph.removeEdge(source, destination);
78 @Override
79 public EdgeValue removeEdge(final EdgeWithEdgeValue<Node, EdgeValue> edge) {
80 return this.wrappedGraph.removeEdge(edge.getSourceNode(), edge.getDestinationNode());
83 @Override
84 public boolean containsEdge(final EdgeWithEdgeValue<Node, EdgeValue> edge) {
85 return this.wrappedGraph.containsEdge(edge.getSourceNode(), edge.getDestinationNode());
88 private final L1<EdgeWithEdgeValue<Node, EdgeValue>, EdgeWithValues<Node, NodeValue, EdgeValue>> getSetEdges = new L1<EdgeWithEdgeValue<Node, EdgeValue>, EdgeWithValues<Node, NodeValue, EdgeValue>>() {
89 @Override
90 public EdgeWithEdgeValue<Node, EdgeValue> op(final EdgeWithValues<Node, NodeValue, EdgeValue> param) {
91 return new EdgeWithEdgeValue<Node, EdgeValue>() {
92 @Override
93 public Node getDestinationNode() {
94 return param.getDestinationNode();
97 @Override
98 public Node getSourceNode() {
99 return param.getSourceNode();
102 @Override
103 public EdgeValue getValue() {
104 return param.getValue();
107 public EdgeValue setValue(final EdgeValue newValue) {
108 return param.setValue(newValue);
114 private final L1<EdgeWithValues<Node, NodeValue, EdgeValue>, EdgeWithEdgeValue<Node, EdgeValue>> inverseSetEdges = new L1<EdgeWithValues<Node, NodeValue, EdgeValue>, EdgeWithEdgeValue<Node, EdgeValue>>() {
115 @Override
116 public EdgeWithValues<Node, NodeValue, EdgeValue> op(final EdgeWithEdgeValue<Node, EdgeValue> param) {
117 return new EdgeWithValues<Node, NodeValue, EdgeValue>() {
118 @Override
119 public java.util.Map.Entry<Node, NodeValue> getDestination() {
120 return new AbstractMapEntry<Node, NodeValue>() {
121 @Override
122 public Node getKey() {
123 return getDestinationNode();
126 @Override
127 public NodeValue getValue() {
128 return WrappedKeySet.this.wrappedGraph.get(this.getKey());
133 @Override
134 public Node getDestinationNode() {
135 return param.getDestinationNode();
138 @Override
139 public java.util.Map.Entry<Node, NodeValue> getSource() {
140 return new AbstractMapEntry<Node, NodeValue>() {
141 @Override
142 public Node getKey() {
143 return getSourceNode();
146 @Override
147 public NodeValue getValue() {
148 return WrappedKeySet.this.wrappedGraph.get(this.getKey());
153 @Override
154 public Node getSourceNode() {
155 return param.getSourceNode();
158 @Override
159 public EdgeValue getValue() {
160 return param.getValue();
163 public EdgeValue setValue(final EdgeValue newValue) {
164 return param.setValue(newValue);
170 private final Set<EdgeWithEdgeValue<Node, EdgeValue>> edgesView = new SetView<EdgeWithValues<Node, NodeValue, EdgeValue>, EdgeWithEdgeValue<Node, EdgeValue>>(this.wrappedGraph.edges(), this.getSetEdges, this.inverseSetEdges);
172 @Override
173 public Set<EdgeWithEdgeValue<Node, EdgeValue>> edges() {
174 return this.edgesView;
177 @Override
178 public Set<EdgeWithEdgeValue<Node, EdgeValue>> inEdges(final Node destination) {
179 return new SetView<EdgeWithValues<Node, NodeValue, EdgeValue>, EdgeWithEdgeValue<Node, EdgeValue>>(this.wrappedGraph.inEdges(destination), this.getSetEdges, this.inverseSetEdges);
182 @Override
183 public Set<EdgeWithEdgeValue<Node, EdgeValue>> outEdges(final Node source) {
184 return new SetView<EdgeWithValues<Node, NodeValue, EdgeValue>, EdgeWithEdgeValue<Node, EdgeValue>>(this.wrappedGraph.outEdges(source), this.getSetEdges, this.inverseSetEdges);
188 * @see java.util.Set#clear()
190 @Override
191 public void clear() {
192 this.wrappedGraph.clear();
195 @Override
196 public boolean contains(final Object o) {
197 return this.wrappedGraph.containsKey(o);
200 @Override
201 public boolean isEmpty() {
202 return this.wrappedGraph.isEmpty();
205 private final L1<Node, Entry<Node, NodeValue>> getKey = Operations.getKey();
206 private final Set<Node> entrySet = new SetView<Entry<Node, NodeValue>, Node>(this.wrappedGraph.entrySet(), this.getKey);
208 @Override
209 public Iterator<Node> iterator() {
210 return this.entrySet.iterator();
213 @Override
214 public boolean remove(final Object o) {
215 return this.wrappedGraph.remove(o) != null;
218 @Override
219 public int size() {
220 return this.wrappedGraph.size();
223 @Override
224 public Object[] toArray() {
225 return this.entrySet.toArray();
228 @Override
229 public <T> T[] toArray(final T[] a) {
230 return this.entrySet.toArray(a);