4 package net
.kezvh
.collections
.graphs
.directed
;
6 import java
.util
.AbstractMap
;
7 import java
.util
.Collection
;
8 import java
.util
.HashMap
;
12 import net
.kezvh
.collections
.views
.MapAsView
;
13 import net
.kezvh
.functional
.lambda
.L1
;
14 import net
.kezvh
.patterns
.AbstractFactory
;
18 * @param <Node> COMMENT
19 * @param <NodeValue> COMMENT
20 * @param <EdgeValue> COMMENT
23 public class AdjacencyCollectionDirectedGraph
<Node
, NodeValue
, EdgeValue
, M
extends Map
<Node
, EdgeValue
>> extends AbstractMap
<Node
, NodeValue
> implements DirectedGraphWithValues
<Node
, NodeValue
, EdgeValue
> {
25 * @param collectionMaker something that makes a collection
27 public AdjacencyCollectionDirectedGraph(final AbstractFactory
<M
> collectionMaker
) {
29 this.collectionMaker
= collectionMaker
;
32 private static final class Entry
<NodeValue
, M
> {
34 private NodeValue value
;
35 private final M destinations
;
40 public NodeValue
getValue() {
45 * @param value the value to set
46 * @return the old value
48 public NodeValue
setValue(final NodeValue value
) {
59 public M
getDestinations() {
60 return this.destinations
;
63 public Entry(final NodeValue value
, final M m
) {
66 this.destinations
= m
;
70 private final AbstractFactory
<M
> collectionMaker
;
71 private final Map
<Node
, Entry
<NodeValue
, M
>> adjacencyListsByNode
= new HashMap
<Node
, Entry
<NodeValue
, M
>>();
73 DirectedGraphWithEdgeValues
<Node
, EdgeValue
> keySet
= new WrappedKeySet
<Node
, NodeValue
, EdgeValue
>(this);
76 * @see net.kezvh.collections.graphs.directed.DirectedGraphWithValues#keySet()
80 public DirectedGraphWithEdgeValues
<Node
, EdgeValue
> keySet() {
85 * @see net.kezvh.collections.graphs.directed.DirectedGraphNodesWithValues#inEdgeSources(java.lang.Object)
86 * @param destination COMMENT
90 public Map
<Node
, NodeValue
> inEdgeSources(final Node destination
) {
91 // TODO Auto-generated method stub
95 private final L1
<NodeValue
, Node
> getNodeValue
= new L1
<NodeValue
, Node
>() {
96 public NodeValue
op(final Node param
) {
97 return AdjacencyCollectionDirectedGraph
.this.adjacencyListsByNode
.get(param
).getValue();
102 * @see net.kezvh.collections.graphs.directed.DirectedGraphNodesWithValues#outEdgeDestinations(java.lang.Object)
103 * @param source COMMENT
107 public Map
<Node
, NodeValue
> outEdgeDestinations(final Node source
) {
108 final Entry
<NodeValue
, M
> entry
= this.adjacencyListsByNode
.get(source
);
109 return new MapAsView
<Node
, NodeValue
>(entry
.getDestinations().keySet(), this.getNodeValue
) {
111 public NodeValue
put(final Node key
, final NodeValue value
) {
112 return AdjacencyCollectionDirectedGraph
.this.adjacencyListsByNode
.get(key
).setValue(value
);
118 * @see java.util.Map#clear()
121 public void clear() {
122 this.adjacencyListsByNode
.clear();
126 * @see java.util.Map#containsKey(java.lang.Object)
131 public boolean containsKey(final Object key
) {
132 return this.adjacencyListsByNode
.containsKey(key
);
136 * @see java.util.Map#containsValue(java.lang.Object)
137 * @param value COMMENT
141 public boolean containsValue(final Object value
) {
142 // TODO Auto-generated method stub
147 * @see java.util.Map#entrySet()
151 public Set
<Map
.Entry
<Node
, NodeValue
>> entrySet() {
152 // TODO Auto-generated method stub
157 * @see java.util.Map#get(java.lang.Object)
162 public NodeValue
get(final Object key
) {
163 return this.adjacencyListsByNode
.get(key
).getValue();
167 * @see java.util.Map#isEmpty()
171 public boolean isEmpty() {
172 return this.adjacencyListsByNode
.isEmpty();
176 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
178 * @param value COMMENT
182 public NodeValue
put(final Node key
, final NodeValue value
) {
183 if (this.adjacencyListsByNode
.containsKey(key
))
184 return this.adjacencyListsByNode
.get(key
).setValue(value
);
186 this.adjacencyListsByNode
.put(key
, new Entry
<NodeValue
, M
>(value
, this.collectionMaker
.create()));
191 * @see java.util.Map#remove(java.lang.Object)
196 public NodeValue
remove(final Object key
) {
197 if (this.adjacencyListsByNode
.containsKey(key
))
198 return this.adjacencyListsByNode
.remove(key
).getValue();
203 * @see java.util.Map#size()
208 return this.adjacencyListsByNode
.size();
212 * @see java.util.Map#values()
216 public Collection
<NodeValue
> values() {
217 // TODO Auto-generated method stub
222 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdgesWithValues#addEdge(java.lang.Object,
223 * java.lang.Object, java.lang.Object)
224 * @param source COMMENT
225 * @param destination COMMENT
226 * @param edgeValue COMMENT
230 public EdgeValue
addEdge(final Node source
, final Node destination
, final EdgeValue edgeValue
) {
231 return this.adjacencyListsByNode
.get(source
).getDestinations().put(destination
, edgeValue
);
235 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdgesWithValues#getEdgeValue(java.lang.Object,
237 * @param source COMMENT
238 * @param destination COMMENT
242 public EdgeValue
getEdgeValue(final Node source
, final Node destination
) {
243 return this.adjacencyListsByNode
.get(source
).getDestinations().get(destination
);
247 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdgesWithValues#removeEdge(java.lang.Object,
249 * @param source COMMENT
250 * @param destination COMMENT
254 public EdgeValue
removeEdge(final Node source
, final Node destination
) {
255 return this.adjacencyListsByNode
.get(source
).getDestinations().remove(destination
);
259 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdgesWithValues#removeEdge(net.kezvh.collections.graphs.directed.EdgeWithEdgeValue)
260 * @param edge COMMENT
264 public EdgeValue
removeEdge(final EdgeWithValues
<Node
, NodeValue
, EdgeValue
> edge
) {
265 return this.removeEdge(edge
.getSourceNode(), edge
.getDestinationNode());
269 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#adjacentFrom(java.lang.Object)
270 * @param source COMMENT
274 public Set
<Node
> adjacentFrom(final Node source
) {
275 // TODO Auto-generated method stub
280 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#adjacentTo(java.lang.Object)
281 * @param destination COMMENT
285 public Set
<Node
> adjacentTo(final Node destination
) {
286 // TODO Auto-generated method stub
291 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#containsEdge(java.lang.Object,
293 * @param source COMMENT
294 * @param destination COMMENT
298 public boolean containsEdge(final Node source
, final Node destination
) {
299 // TODO Auto-generated method stub
304 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#containsEdge(net.kezvh.collections.graphs.directed.Edge)
305 * @param edge COMMENT
309 public boolean containsEdge(final EdgeWithValues
<Node
, NodeValue
, EdgeValue
> edge
) {
310 // TODO Auto-generated method stub
315 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#edgeCount()
319 public long edgeCount() {
320 // TODO Auto-generated method stub
325 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#edges()
329 public Set
<EdgeWithValues
<Node
, NodeValue
, EdgeValue
>> edges() {
330 // TODO Auto-generated method stub
335 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#inDegree(java.lang.Object)
336 * @param destination COMMENT
340 public int inDegree(final Node destination
) {
341 // TODO Auto-generated method stub
346 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#inEdges(java.lang.Object)
347 * @param destination COMMENT
351 public Set
<EdgeWithValues
<Node
, NodeValue
, EdgeValue
>> inEdges(final Node destination
) {
352 // TODO Auto-generated method stub
357 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#outDegree(java.lang.Object)
358 * @param source COMMENT
362 public int outDegree(final Node source
) {
363 // TODO Auto-generated method stub
368 * @see net.kezvh.collections.graphs.directed.DirectedGraphEdges#outEdges(java.lang.Object)
369 * @param source COMMENT
373 public Set
<EdgeWithValues
<Node
, NodeValue
, EdgeValue
>> outEdges(final Node source
) {
374 // TODO Auto-generated method stub