weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 1 | /* |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 3 | * |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 7 | * |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 15 | */ |
| 16 | package org.onlab.graph; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Objects; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import com.google.common.collect.HashMultimap; |
| 25 | import com.google.common.collect.SetMultimap; |
| 26 | |
| 27 | public class MutableAdjacencyListsGraph<V extends Vertex, E extends Edge<V>> |
| 28 | implements MutableGraph<V, E> { |
| 29 | private Set<V> vertexes = new HashSet<V>(); |
| 30 | private Set<E> edges = new HashSet<E>(); |
| 31 | |
| 32 | private SetMultimap<V, E> sources = HashMultimap.create(); |
| 33 | private SetMultimap<V, E> destinations = HashMultimap.create(); |
| 34 | |
| 35 | /** |
| 36 | * Creates a graph comprising of the specified vertexes and edges. |
| 37 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 38 | * @param vertex set of graph vertexes |
| 39 | * @param edge set of graph edges |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 40 | */ |
| 41 | public MutableAdjacencyListsGraph(Set<V> vertex, Set<E> edge) { |
| 42 | vertexes.addAll(vertex); |
| 43 | edges.addAll(edge); |
| 44 | for (E e : edge) { |
| 45 | sources.put(e.src(), e); |
| 46 | vertexes.add(e.src()); |
| 47 | destinations.put(e.dst(), e); |
| 48 | vertexes.add(e.dst()); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Set<V> getVertexes() { |
| 54 | return vertexes; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public Set<E> getEdges() { |
| 59 | return edges; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Set<E> getEdgesFrom(V src) { |
| 64 | return sources.get(src); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Set<E> getEdgesTo(V dst) { |
| 69 | return destinations.get(dst); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean equals(Object obj) { |
| 74 | if (this == obj) { |
| 75 | return true; |
| 76 | } |
| 77 | if (obj instanceof MutableAdjacencyListsGraph) { |
| 78 | MutableAdjacencyListsGraph that = (MutableAdjacencyListsGraph) obj; |
| 79 | return this.getClass() == that.getClass() && |
| 80 | Objects.equals(this.vertexes, that.vertexes) && |
| 81 | Objects.equals(this.edges, that.edges); |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int hashCode() { |
| 88 | return Objects.hash(vertexes, edges); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String toString() { |
| 93 | return toStringHelper(this) |
| 94 | .add("vertexes", vertexes) |
| 95 | .add("edges", edges) |
| 96 | .toString(); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | @Override |
| 101 | public void addVertex(V vertex) { |
| 102 | if (vertexes != null) { |
| 103 | if (!vertexes.contains(vertex)) { |
| 104 | vertexes.add(vertex); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void removeVertex(V vertex) { |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 111 | if (vertexes != null && edges != null) { |
| 112 | if (vertexes.contains(vertex)) { |
| 113 | vertexes.remove(vertex); |
| 114 | Set<E> srcEdgesList = sources.get(vertex); |
| 115 | Set<E> dstEdgesList = destinations.get(vertex); |
| 116 | edges.removeAll(srcEdgesList); |
| 117 | edges.removeAll(dstEdgesList); |
| 118 | sources.remove(vertex, srcEdgesList); |
| 119 | sources.remove(vertex, dstEdgesList); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void addEdge(E edge) { |
| 126 | if (edges != null) { |
| 127 | if (!edges.contains(edge)) { |
| 128 | edges.add(edge); |
| 129 | sources.put(edge.src(), edge); |
| 130 | destinations.put(edge.dst(), edge); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void removeEdge(E edge) { |
| 137 | if (edges != null) { |
| 138 | if (edges.contains(edge)) { |
| 139 | edges.remove(edge); |
| 140 | sources.remove(edge.src(), edge); |
| 141 | destinations.remove(edge.dst(), edge); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Graph<V, E> toImmutable() { |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 148 | return null; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Clear the graph. |
| 153 | */ |
| 154 | public void clear() { |
| 155 | edges.clear(); |
| 156 | vertexes.clear(); |
| 157 | sources.clear(); |
| 158 | destinations.clear(); |
| 159 | } |
| 160 | } |