blob: 3890dcf8550382fe4146ac48c2c402acbd517146 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.ImmutableSetMultimap;
20
21import java.util.Objects;
22import java.util.Set;
23
tomeadbb462014-09-07 16:10:19 -070024import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070025import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Immutable graph implemented using adjacency lists.
29 *
30 * @param <V> vertex type
31 * @param <E> edge type
32 */
tomcbff9392014-09-10 00:45:23 -070033public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>>
34 implements Graph<V, E> {
tome3489412014-08-29 02:30:38 -070035
36 private final Set<V> vertexes;
37 private final Set<E> edges;
38
39 private final ImmutableSetMultimap<V, E> sources;
40 private final ImmutableSetMultimap<V, E> destinations;
41
tome3489412014-08-29 02:30:38 -070042 /**
43 * Creates a graph comprising of the specified vertexes and edges.
44 *
45 * @param vertexes set of graph vertexes
46 * @param edges set of graph edges
47 */
48 public AdjacencyListsGraph(Set<V> vertexes, Set<E> edges) {
49 checkNotNull(vertexes, "Vertex set cannot be null");
50 checkNotNull(edges, "Edge set cannot be null");
51
52 // Record ingress/egress edges for each vertex.
53 ImmutableSetMultimap.Builder<V, E> srcMap = ImmutableSetMultimap.builder();
54 ImmutableSetMultimap.Builder<V, E> dstMap = ImmutableSetMultimap.builder();
55
56 // Also make sure that all edge end-points are added as vertexes
57 ImmutableSet.Builder<V> actualVertexes = ImmutableSet.builder();
58 actualVertexes.addAll(vertexes);
59
60 for (E edge : edges) {
61 srcMap.put(edge.src(), edge);
62 actualVertexes.add(edge.src());
63 dstMap.put(edge.dst(), edge);
64 actualVertexes.add(edge.dst());
65 }
66
67 // Make an immutable copy of the edge and vertex sets
68 this.edges = ImmutableSet.copyOf(edges);
69 this.vertexes = actualVertexes.build();
70
71 // Build immutable copies of sources and destinations edge maps
72 sources = srcMap.build();
73 destinations = dstMap.build();
74 }
75
76 @Override
77 public Set<V> getVertexes() {
78 return vertexes;
79 }
80
81 @Override
82 public Set<E> getEdges() {
83 return edges;
84 }
85
86 @Override
87 public Set<E> getEdgesFrom(V src) {
88 return sources.get(src);
89 }
90
91 @Override
92 public Set<E> getEdgesTo(V dst) {
93 return destinations.get(dst);
94 }
95
96 @Override
97 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070098 if (this == obj) {
99 return true;
100 }
tome3489412014-08-29 02:30:38 -0700101 if (obj instanceof AdjacencyListsGraph) {
102 AdjacencyListsGraph that = (AdjacencyListsGraph) obj;
103 return this.getClass() == that.getClass() &&
104 Objects.equals(this.vertexes, that.vertexes) &&
105 Objects.equals(this.edges, that.edges);
106 }
107 return false;
108 }
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(vertexes, edges);
113 }
114
115 @Override
116 public String toString() {
tomeadbb462014-09-07 16:10:19 -0700117 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -0700118 .add("vertexes", vertexes)
119 .add("edges", edges)
120 .toString();
121 }
122}