today
[ephemerata.git] / KezvhLib / src-lib / net / kezvh / collections / graphs / directed / HashDirectedGraphSet.java
blob7ccc552a999e3a81c49ef3e9c946deef5cdaaafb
1 package net.kezvh.collections.graphs.directed;
3 import java.util.AbstractSet;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.Set;
8 import net.kezvh.collections.views.MapAsView;
9 import net.kezvh.collections.wrappers.CollectionAsSet;
10 import net.kezvh.functional.lambda.L1;
12 /**
13 * @author mjacob
15 * @param <Node> COMMENT
16 * @param <EdgeValue> COMMENT
18 public class HashDirectedGraphSet<Node, EdgeValue> extends AbstractSet<Node> implements DirectedGraphWithEdgeValues<Node, EdgeValue> {
19 @Override
20 public int hashCode() {
21 final int prime = 31;
22 int result = super.hashCode();
23 result = prime * result + ((this.innerGraph == null) ? 0 : this.innerGraph.hashCode());
24 return result;
27 @Override
28 public boolean equals(final Object obj) {
29 if (this == obj)
30 return true;
31 if (!super.equals(obj))
32 return false;
33 if (this.getClass() != obj.getClass())
34 return false;
35 final HashDirectedGraphSet<?, ?> other = (HashDirectedGraphSet<?, ?>) obj;
36 if (this.innerGraph == null) {
37 if (other.innerGraph != null)
38 return false;
39 } else if (!this.innerGraph.equals(other.innerGraph))
40 return false;
41 return true;
44 private static final Object PRESENT = new Object();
45 private final HashDirectedGraph<Node, Object, EdgeValue> innerGraph = new HashDirectedGraph<Node, Object, EdgeValue>();
47 private final L1<Object, Node> presentMapper = new L1<Object, Node>() {
48 public Object op(final Node param) {
49 return HashDirectedGraphSet.PRESENT;
53 @Override
54 public boolean add(final Node e) {
55 return this.innerGraph.put(e, HashDirectedGraphSet.PRESENT) != null;
58 @SuppressWarnings("unchecked")
59 @Override
60 public boolean addAll(final Collection<? extends Node> c) {
61 this.innerGraph.putAll(new MapAsView<Node, Object>(new CollectionAsSet<Node, Collection<Node>>((Collection<Node>) c), this.presentMapper));
62 return !c.isEmpty();
65 @Override
66 public void clear() {
67 this.innerGraph.clear();
70 @Override
71 public boolean contains(final Object o) {
72 return this.innerGraph.containsKey(o);
75 @Override
76 public boolean isEmpty() {
77 return this.innerGraph.isEmpty();
80 @Override
81 public Iterator<Node> iterator() {
82 return this.innerGraph.keySet().iterator();
85 @Override
86 public boolean remove(final Object o) {
87 return this.innerGraph.remove(o) != null;
90 @Override
91 public int size() {
92 return this.innerGraph.size();
95 @Override
96 public Object[] toArray() {
97 return this.innerGraph.keySet().toArray();
100 @Override
101 public <T> T[] toArray(final T[] a) {
102 return this.innerGraph.keySet().toArray(a);
105 @Override
106 public Set<Node> adjacentFrom(final Node source) {
107 return this.innerGraph.adjacentFrom(source);
110 @Override
111 public Set<Node> adjacentTo(final Node destination) {
112 return this.innerGraph.adjacentTo(destination);
115 @Override
116 public boolean containsEdge(final Node source, final Node destination) {
117 return this.innerGraph.containsEdge(source, destination);
120 @Override
121 public boolean containsEdge(final EdgeWithEdgeValue<Node, EdgeValue> edge) {
122 return this.innerGraph.keySet().contains(edge);
125 @Override
126 public long edgeCount() {
127 return this.innerGraph.edgeCount();
130 @Override
131 public Set<EdgeWithEdgeValue<Node, EdgeValue>> edges() {
132 return this.innerGraph.keySet().edges();
135 @Override
136 public int inDegree(final Node destination) {
137 return this.innerGraph.inDegree(destination);
140 @Override
141 public Set<EdgeWithEdgeValue<Node, EdgeValue>> inEdges(final Node destination) {
142 return this.innerGraph.keySet().inEdges(destination);
145 @Override
146 public int outDegree(final Node source) {
147 return this.innerGraph.outDegree(source);
150 @Override
151 public Set<EdgeWithEdgeValue<Node, EdgeValue>> outEdges(final Node source) {
152 return this.innerGraph.keySet().outEdges(source);
155 @Override
156 public EdgeValue addEdge(final Node source, final Node destination, final EdgeValue edgeValue) {
157 return this.innerGraph.addEdge(source, destination, edgeValue);
160 @Override
161 public EdgeValue getEdgeValue(final Node source, final Node destination) {
162 return this.innerGraph.getEdgeValue(source, destination);
165 @Override
166 public EdgeValue removeEdge(final Node source, final Node destination) {
167 return this.innerGraph.removeEdge(source, destination);
170 @Override
171 public EdgeValue removeEdge(final EdgeWithEdgeValue<Node, EdgeValue> edge) {
172 return this.innerGraph.keySet().removeEdge(edge);