blob: 87571c4b501261507087c62af5c96d5f99c0dddf [file] [log] [blame]
weibit9a3631b2014-11-03 14:39:25 -08001/*
alshabibab984662014-12-04 18:56:18 -08002 * Copyright 2014 Open Networking Laboratory
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>>
28implements 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 HIGUCHI5c947272014-11-03 21:39:21 -080038 * @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) {
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) {
weibit9a3631b2014-11-03 14:39:25 -0800111 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() {
weibit9a3631b2014-11-03 14:39:25 -0800148 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}