1 package net
.kezvh
.collections
.graphs
.directed
;
3 import java
.util
.AbstractSet
;
4 import java
.util
.Collection
;
5 import java
.util
.Iterator
;
8 import net
.kezvh
.collections
.views
.MapAsView
;
9 import net
.kezvh
.collections
.wrappers
.CollectionAsSet
;
10 import net
.kezvh
.functional
.lambda
.L1
;
14 * @param <Node> COMMENT
15 * @param <EdgeValue> COMMENT
17 public class GenericDirectedGraphSet
<Node
, EdgeValue
> extends AbstractSet
<Node
> implements DirectedGraphWithEdgeValues
<Node
, EdgeValue
> {
20 public int hashCode() {
22 int result
= super.hashCode();
23 result
= prime
* result
+ ((this.innerGraph
== null) ?
0 : this.innerGraph
.hashCode());
28 public boolean equals(final Object obj
) {
31 if (!super.equals(obj
))
33 if (this.getClass() != obj
.getClass())
35 final GenericDirectedGraphSet
<?
, ?
> other
= (GenericDirectedGraphSet
<?
, ?
>) obj
;
36 if (this.innerGraph
== null) {
37 if (other
.innerGraph
!= null)
39 } else if (!this.innerGraph
.equals(other
.innerGraph
))
44 private static final Object PRESENT
= new Object();
45 private final DirectedGraphWithValues
<Node
, Object
, EdgeValue
> innerGraph
;
48 * @param innerGraph a graph on which to build the set
50 public GenericDirectedGraphSet(final DirectedGraphWithValues
<Node
, Object
, EdgeValue
> innerGraph
) {
52 this.innerGraph
= innerGraph
;
55 private final L1
<Object
, Node
> presentMapper
= new L1
<Object
, Node
>() {
56 public Object
op(final Node param
) {
57 return GenericDirectedGraphSet
.PRESENT
;
62 public boolean add(final Node e
) {
63 return this.innerGraph
.put(e
, GenericDirectedGraphSet
.PRESENT
) != null;
66 @SuppressWarnings("unchecked")
68 public boolean addAll(final Collection
<?
extends Node
> c
) {
69 this.innerGraph
.putAll(new MapAsView
<Node
, Object
>(new CollectionAsSet
<Node
, Collection
<Node
>>((Collection
<Node
>) c
), this.presentMapper
));
75 this.innerGraph
.clear();
79 public boolean contains(final Object o
) {
80 return this.innerGraph
.containsKey(o
);
84 public boolean isEmpty() {
85 return this.innerGraph
.isEmpty();
89 public Iterator
<Node
> iterator() {
90 return this.innerGraph
.keySet().iterator();
94 public boolean remove(final Object o
) {
95 return this.innerGraph
.remove(o
) != null;
100 return this.innerGraph
.size();
104 public Object
[] toArray() {
105 return this.innerGraph
.keySet().toArray();
109 public <T
> T
[] toArray(final T
[] a
) {
110 return this.innerGraph
.keySet().toArray(a
);
114 public Set
<Node
> adjacentFrom(final Node source
) {
115 return this.innerGraph
.adjacentFrom(source
);
119 public Set
<Node
> adjacentTo(final Node destination
) {
120 return this.innerGraph
.adjacentTo(destination
);
124 public boolean containsEdge(final Node source
, final Node destination
) {
125 return this.innerGraph
.containsEdge(source
, destination
);
129 public boolean containsEdge(final EdgeWithEdgeValue
<Node
, EdgeValue
> edge
) {
130 return this.innerGraph
.keySet().contains(edge
);
134 public long edgeCount() {
135 return this.innerGraph
.edgeCount();
139 public Set
<EdgeWithEdgeValue
<Node
, EdgeValue
>> edges() {
140 return this.innerGraph
.keySet().edges();
144 public int inDegree(final Node destination
) {
145 return this.innerGraph
.inDegree(destination
);
149 public Set
<EdgeWithEdgeValue
<Node
, EdgeValue
>> inEdges(final Node destination
) {
150 return this.innerGraph
.keySet().inEdges(destination
);
154 public int outDegree(final Node source
) {
155 return this.innerGraph
.outDegree(source
);
159 public Set
<EdgeWithEdgeValue
<Node
, EdgeValue
>> outEdges(final Node source
) {
160 return this.innerGraph
.keySet().outEdges(source
);
164 public EdgeValue
addEdge(final Node source
, final Node destination
, final EdgeValue edgeValue
) {
165 return this.innerGraph
.addEdge(source
, destination
, edgeValue
);
169 public EdgeValue
getEdgeValue(final Node source
, final Node destination
) {
170 return this.innerGraph
.getEdgeValue(source
, destination
);
174 public EdgeValue
removeEdge(final Node source
, final Node destination
) {
175 return this.innerGraph
.removeEdge(source
, destination
);
179 public EdgeValue
removeEdge(final EdgeWithEdgeValue
<Node
, EdgeValue
> edge
) {
180 return this.innerGraph
.keySet().removeEdge(edge
);