4 package net
.kezvh
.collections
.graphs
.directed
;
6 import java
.util
.AbstractSet
;
7 import java
.util
.Iterator
;
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
;
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
;
27 * @param wrappedGraph wrapped graph
29 public WrappedKeySet(final DirectedGraphWithValues
<Node
, NodeValue
, EdgeValue
> wrappedGraph
) {
30 this.wrappedGraph
= wrappedGraph
;
34 public boolean containsEdge(final Node source
, final Node destination
) {
35 return this.wrappedGraph
.containsEdge(source
, destination
);
39 public long edgeCount() {
40 return this.wrappedGraph
.edgeCount();
44 public int inDegree(final Node destination
) {
45 return this.wrappedGraph
.inDegree(destination
);
49 public int outDegree(final Node source
) {
50 return this.wrappedGraph
.outDegree(source
);
54 public Set
<Node
> adjacentFrom(final Node source
) {
55 return this.wrappedGraph
.adjacentFrom(source
);
59 public Set
<Node
> adjacentTo(final Node destination
) {
60 return this.wrappedGraph
.adjacentTo(destination
);
64 public EdgeValue
addEdge(final Node source
, final Node destination
, final EdgeValue edgeValue
) {
65 return this.wrappedGraph
.addEdge(source
, destination
, edgeValue
);
69 public EdgeValue
getEdgeValue(final Node source
, final Node destination
) {
70 return this.wrappedGraph
.getEdgeValue(source
, destination
);
74 public EdgeValue
removeEdge(final Node source
, final Node destination
) {
75 return this.wrappedGraph
.removeEdge(source
, destination
);
79 public EdgeValue
removeEdge(final EdgeWithEdgeValue
<Node
, EdgeValue
> edge
) {
80 return this.wrappedGraph
.removeEdge(edge
.getSourceNode(), edge
.getDestinationNode());
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
>>() {
90 public EdgeWithEdgeValue
<Node
, EdgeValue
> op(final EdgeWithValues
<Node
, NodeValue
, EdgeValue
> param
) {
91 return new EdgeWithEdgeValue
<Node
, EdgeValue
>() {
93 public Node
getDestinationNode() {
94 return param
.getDestinationNode();
98 public Node
getSourceNode() {
99 return param
.getSourceNode();
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
>>() {
116 public EdgeWithValues
<Node
, NodeValue
, EdgeValue
> op(final EdgeWithEdgeValue
<Node
, EdgeValue
> param
) {
117 return new EdgeWithValues
<Node
, NodeValue
, EdgeValue
>() {
119 public java
.util
.Map
.Entry
<Node
, NodeValue
> getDestination() {
120 return new AbstractMapEntry
<Node
, NodeValue
>() {
122 public Node
getKey() {
123 return getDestinationNode();
127 public NodeValue
getValue() {
128 return WrappedKeySet
.this.wrappedGraph
.get(this.getKey());
134 public Node
getDestinationNode() {
135 return param
.getDestinationNode();
139 public java
.util
.Map
.Entry
<Node
, NodeValue
> getSource() {
140 return new AbstractMapEntry
<Node
, NodeValue
>() {
142 public Node
getKey() {
143 return getSourceNode();
147 public NodeValue
getValue() {
148 return WrappedKeySet
.this.wrappedGraph
.get(this.getKey());
154 public Node
getSourceNode() {
155 return param
.getSourceNode();
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
);
173 public Set
<EdgeWithEdgeValue
<Node
, EdgeValue
>> edges() {
174 return this.edgesView
;
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
);
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()
191 public void clear() {
192 this.wrappedGraph
.clear();
196 public boolean contains(final Object o
) {
197 return this.wrappedGraph
.containsKey(o
);
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
);
209 public Iterator
<Node
> iterator() {
210 return this.entrySet
.iterator();
214 public boolean remove(final Object o
) {
215 return this.wrappedGraph
.remove(o
) != null;
220 return this.wrappedGraph
.size();
224 public Object
[] toArray() {
225 return this.entrySet
.toArray();
229 public <T
> T
[] toArray(final T
[] a
) {
230 return this.entrySet
.toArray(a
);