blob: 07dc70b09ca6bf982702c1e1bd9ead8f23fc41a6 [file] [log] [blame]
tome3489412014-08-29 02:30:38 -07001package org.onlab.graph;
2
3import com.google.common.collect.ImmutableSet;
4import com.google.common.collect.ImmutableSetMultimap;
5
6import java.util.Objects;
7import java.util.Set;
8
tomeadbb462014-09-07 16:10:19 -07009import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070010import static com.google.common.base.Preconditions.checkNotNull;
11
12/**
13 * Immutable graph implemented using adjacency lists.
14 *
15 * @param <V> vertex type
16 * @param <E> edge type
17 */
18public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>> implements Graph<V, E> {
19
20 private final Set<V> vertexes;
21 private final Set<E> edges;
22
23 private final ImmutableSetMultimap<V, E> sources;
24 private final ImmutableSetMultimap<V, E> destinations;
25
tome3489412014-08-29 02:30:38 -070026 /**
27 * Creates a graph comprising of the specified vertexes and edges.
28 *
29 * @param vertexes set of graph vertexes
30 * @param edges set of graph edges
31 */
32 public AdjacencyListsGraph(Set<V> vertexes, Set<E> edges) {
33 checkNotNull(vertexes, "Vertex set cannot be null");
34 checkNotNull(edges, "Edge set cannot be null");
35
36 // Record ingress/egress edges for each vertex.
37 ImmutableSetMultimap.Builder<V, E> srcMap = ImmutableSetMultimap.builder();
38 ImmutableSetMultimap.Builder<V, E> dstMap = ImmutableSetMultimap.builder();
39
40 // Also make sure that all edge end-points are added as vertexes
41 ImmutableSet.Builder<V> actualVertexes = ImmutableSet.builder();
42 actualVertexes.addAll(vertexes);
43
44 for (E edge : edges) {
45 srcMap.put(edge.src(), edge);
46 actualVertexes.add(edge.src());
47 dstMap.put(edge.dst(), edge);
48 actualVertexes.add(edge.dst());
49 }
50
51 // Make an immutable copy of the edge and vertex sets
52 this.edges = ImmutableSet.copyOf(edges);
53 this.vertexes = actualVertexes.build();
54
55 // Build immutable copies of sources and destinations edge maps
56 sources = srcMap.build();
57 destinations = dstMap.build();
58 }
59
60 @Override
61 public Set<V> getVertexes() {
62 return vertexes;
63 }
64
65 @Override
66 public Set<E> getEdges() {
67 return edges;
68 }
69
70 @Override
71 public Set<E> getEdgesFrom(V src) {
72 return sources.get(src);
73 }
74
75 @Override
76 public Set<E> getEdgesTo(V dst) {
77 return destinations.get(dst);
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (obj instanceof AdjacencyListsGraph) {
83 AdjacencyListsGraph that = (AdjacencyListsGraph) obj;
84 return this.getClass() == that.getClass() &&
85 Objects.equals(this.vertexes, that.vertexes) &&
86 Objects.equals(this.edges, that.edges);
87 }
88 return false;
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hash(vertexes, edges);
94 }
95
96 @Override
97 public String toString() {
tomeadbb462014-09-07 16:10:19 -070098 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -070099 .add("vertexes", vertexes)
100 .add("edges", edges)
101 .toString();
102 }
103}