weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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>> |
Thomas Vachuska | 90b3a40 | 2017-06-20 13:23:00 -0700 | [diff] [blame] | 28 | implements MutableGraph<V, E> { |
| 29 | private Set<V> vertexes = new HashSet<>(); |
| 30 | private Set<E> edges = new HashSet<>(); |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 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 | * |
Thomas Vachuska | 90b3a40 | 2017-06-20 13:23:00 -0700 | [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) { |
Thomas Vachuska | 90b3a40 | 2017-06-20 13:23:00 -0700 | [diff] [blame] | 102 | vertexes.add(vertex); |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void removeVertex(V vertex) { |
Thomas Vachuska | 90b3a40 | 2017-06-20 13:23:00 -0700 | [diff] [blame] | 107 | if (vertexes.remove(vertex)) { |
| 108 | Set<E> srcEdgesList = sources.get(vertex); |
| 109 | Set<E> dstEdgesList = destinations.get(vertex); |
| 110 | edges.removeAll(srcEdgesList); |
| 111 | edges.removeAll(dstEdgesList); |
| 112 | sources.remove(vertex, srcEdgesList); |
| 113 | sources.remove(vertex, dstEdgesList); |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void addEdge(E edge) { |
Thomas Vachuska | 90b3a40 | 2017-06-20 13:23:00 -0700 | [diff] [blame] | 119 | if (edges.add(edge)) { |
| 120 | sources.put(edge.src(), edge); |
| 121 | destinations.put(edge.dst(), edge); |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void removeEdge(E edge) { |
Thomas Vachuska | 90b3a40 | 2017-06-20 13:23:00 -0700 | [diff] [blame] | 127 | if (edges.remove(edge)) { |
| 128 | sources.remove(edge.src(), edge); |
| 129 | destinations.remove(edge.dst(), edge); |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Graph<V, E> toImmutable() { |
Thomas Vachuska | 90b3a40 | 2017-06-20 13:23:00 -0700 | [diff] [blame] | 135 | return new AdjacencyListsGraph<>(vertexes, edges); |
weibit | 9a3631b | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 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 | } |