weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 1 | package org.onlab.graph; |
| 2 | |
| 3 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 4 | |
| 5 | import java.util.HashSet; |
| 6 | import java.util.Objects; |
| 7 | import java.util.Set; |
| 8 | |
| 9 | import com.google.common.collect.HashMultimap; |
| 10 | import com.google.common.collect.SetMultimap; |
| 11 | |
| 12 | public class MutableAdjacencyListsGraph<V extends Vertex, E extends Edge<V>> |
| 13 | implements MutableGraph<V, E> { |
| 14 | private Set<V> vertexes = new HashSet<V>(); |
| 15 | private Set<E> edges = new HashSet<E>(); |
| 16 | |
| 17 | private SetMultimap<V, E> sources = HashMultimap.create(); |
| 18 | private SetMultimap<V, E> destinations = HashMultimap.create(); |
| 19 | |
| 20 | /** |
| 21 | * Creates a graph comprising of the specified vertexes and edges. |
| 22 | * |
| 23 | * @param vertexes set of graph vertexes |
| 24 | * @param edges set of graph edges |
| 25 | */ |
| 26 | public MutableAdjacencyListsGraph(Set<V> vertex, Set<E> edge) { |
| 27 | vertexes.addAll(vertex); |
| 28 | edges.addAll(edge); |
| 29 | for (E e : edge) { |
| 30 | sources.put(e.src(), e); |
| 31 | vertexes.add(e.src()); |
| 32 | destinations.put(e.dst(), e); |
| 33 | vertexes.add(e.dst()); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public Set<V> getVertexes() { |
| 39 | return vertexes; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Set<E> getEdges() { |
| 44 | return edges; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Set<E> getEdgesFrom(V src) { |
| 49 | return sources.get(src); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Set<E> getEdgesTo(V dst) { |
| 54 | return destinations.get(dst); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object obj) { |
| 59 | if (this == obj) { |
| 60 | return true; |
| 61 | } |
| 62 | if (obj instanceof MutableAdjacencyListsGraph) { |
| 63 | MutableAdjacencyListsGraph that = (MutableAdjacencyListsGraph) obj; |
| 64 | return this.getClass() == that.getClass() && |
| 65 | Objects.equals(this.vertexes, that.vertexes) && |
| 66 | Objects.equals(this.edges, that.edges); |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int hashCode() { |
| 73 | return Objects.hash(vertexes, edges); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return toStringHelper(this) |
| 79 | .add("vertexes", vertexes) |
| 80 | .add("edges", edges) |
| 81 | .toString(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | @Override |
| 86 | public void addVertex(V vertex) { |
| 87 | if (vertexes != null) { |
| 88 | if (!vertexes.contains(vertex)) { |
| 89 | vertexes.add(vertex); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void removeVertex(V vertex) { |
| 96 | // TODO Auto-generated method stub |
| 97 | if (vertexes != null && edges != null) { |
| 98 | if (vertexes.contains(vertex)) { |
| 99 | vertexes.remove(vertex); |
| 100 | Set<E> srcEdgesList = sources.get(vertex); |
| 101 | Set<E> dstEdgesList = destinations.get(vertex); |
| 102 | edges.removeAll(srcEdgesList); |
| 103 | edges.removeAll(dstEdgesList); |
| 104 | sources.remove(vertex, srcEdgesList); |
| 105 | sources.remove(vertex, dstEdgesList); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void addEdge(E edge) { |
| 112 | if (edges != null) { |
| 113 | if (!edges.contains(edge)) { |
| 114 | edges.add(edge); |
| 115 | sources.put(edge.src(), edge); |
| 116 | destinations.put(edge.dst(), edge); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void removeEdge(E edge) { |
| 123 | if (edges != null) { |
| 124 | if (edges.contains(edge)) { |
| 125 | edges.remove(edge); |
| 126 | sources.remove(edge.src(), edge); |
| 127 | destinations.remove(edge.dst(), edge); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Graph<V, E> toImmutable() { |
| 134 | // TODO Auto-generated method stub |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Clear the graph. |
| 140 | */ |
| 141 | public void clear() { |
| 142 | edges.clear(); |
| 143 | vertexes.clear(); |
| 144 | sources.clear(); |
| 145 | destinations.clear(); |
| 146 | } |
| 147 | } |