blob: 044a60d2e79fbd2102fabb10ebfa3947286ec00d [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) {
114 // TODO Auto-generated method stub
115 if (vertexes != null && edges != null) {
116 if (vertexes.contains(vertex)) {
117 vertexes.remove(vertex);
118 Set<E> srcEdgesList = sources.get(vertex);
119 Set<E> dstEdgesList = destinations.get(vertex);
120 edges.removeAll(srcEdgesList);
121 edges.removeAll(dstEdgesList);
122 sources.remove(vertex, srcEdgesList);
123 sources.remove(vertex, dstEdgesList);
124 }
125 }
126 }
127
128 @Override
129 public void addEdge(E edge) {
130 if (edges != null) {
131 if (!edges.contains(edge)) {
132 edges.add(edge);
133 sources.put(edge.src(), edge);
134 destinations.put(edge.dst(), edge);
135 }
136 }
137 }
138
139 @Override
140 public void removeEdge(E edge) {
141 if (edges != null) {
142 if (edges.contains(edge)) {
143 edges.remove(edge);
144 sources.remove(edge.src(), edge);
145 destinations.remove(edge.dst(), edge);
146 }
147 }
148 }
149
150 @Override
151 public Graph<V, E> toImmutable() {
152 // TODO Auto-generated method stub
153 return null;
154 }
155
156 /**
157 * Clear the graph.
158 */
159 public void clear() {
160 edges.clear();
161 vertexes.clear();
162 sources.clear();
163 destinations.clear();
164 }
165}