blob: 1c97d04b2cb9c29654cbcd5dec0a60dc51af3a3a [file] [log] [blame]
weibit9a3631b2014-11-03 14:39:25 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
weibit9a3631b2014-11-03 14:39:25 -08003 *
alshabibab984662014-12-04 18:56:18 -08004 * 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
weibit9a3631b2014-11-03 14:39:25 -08007 *
alshabibab984662014-12-04 18:56:18 -08008 * 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.
weibit9a3631b2014-11-03 14:39:25 -080015 */
16package org.onlab.graph;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19
20import java.util.HashSet;
21import java.util.Objects;
22import java.util.Set;
23
24import com.google.common.collect.HashMultimap;
25import com.google.common.collect.SetMultimap;
26
27public class MutableAdjacencyListsGraph<V extends Vertex, E extends Edge<V>>
Thomas Vachuska90b3a402017-06-20 13:23:00 -070028 implements MutableGraph<V, E> {
29 private Set<V> vertexes = new HashSet<>();
30 private Set<E> edges = new HashSet<>();
weibit9a3631b2014-11-03 14:39:25 -080031
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 Vachuska90b3a402017-06-20 13:23:00 -070038 * @param vertex set of graph vertexes
39 * @param edge set of graph edges
weibit9a3631b2014-11-03 14:39:25 -080040 */
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 Vachuska90b3a402017-06-20 13:23:00 -0700102 vertexes.add(vertex);
weibit9a3631b2014-11-03 14:39:25 -0800103 }
104
105 @Override
106 public void removeVertex(V vertex) {
Thomas Vachuska90b3a402017-06-20 13:23:00 -0700107 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);
weibit9a3631b2014-11-03 14:39:25 -0800114 }
115 }
116
117 @Override
118 public void addEdge(E edge) {
Thomas Vachuska90b3a402017-06-20 13:23:00 -0700119 if (edges.add(edge)) {
120 sources.put(edge.src(), edge);
121 destinations.put(edge.dst(), edge);
weibit9a3631b2014-11-03 14:39:25 -0800122 }
123 }
124
125 @Override
126 public void removeEdge(E edge) {
Thomas Vachuska90b3a402017-06-20 13:23:00 -0700127 if (edges.remove(edge)) {
128 sources.remove(edge.src(), edge);
129 destinations.remove(edge.dst(), edge);
weibit9a3631b2014-11-03 14:39:25 -0800130 }
131 }
132
133 @Override
134 public Graph<V, E> toImmutable() {
Thomas Vachuska90b3a402017-06-20 13:23:00 -0700135 return new AdjacencyListsGraph<>(vertexes, edges);
weibit9a3631b2014-11-03 14:39:25 -0800136 }
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}