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