blob: 981d09b1886ff7dc6a80870291918ef82ac3689e [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 */
tomcbff9392014-09-10 00:45:23 -070018public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>>
19 implements Graph<V, E> {
tome3489412014-08-29 02:30:38 -070020
21 private final Set<V> vertexes;
22 private final Set<E> edges;
23
24 private final ImmutableSetMultimap<V, E> sources;
25 private final ImmutableSetMultimap<V, E> destinations;
26
tome3489412014-08-29 02:30:38 -070027 /**
28 * Creates a graph comprising of the specified vertexes and edges.
29 *
30 * @param vertexes set of graph vertexes
31 * @param edges set of graph edges
32 */
33 public AdjacencyListsGraph(Set<V> vertexes, Set<E> edges) {
34 checkNotNull(vertexes, "Vertex set cannot be null");
35 checkNotNull(edges, "Edge set cannot be null");
36
37 // Record ingress/egress edges for each vertex.
38 ImmutableSetMultimap.Builder<V, E> srcMap = ImmutableSetMultimap.builder();
39 ImmutableSetMultimap.Builder<V, E> dstMap = ImmutableSetMultimap.builder();
40
41 // Also make sure that all edge end-points are added as vertexes
42 ImmutableSet.Builder<V> actualVertexes = ImmutableSet.builder();
43 actualVertexes.addAll(vertexes);
44
45 for (E edge : edges) {
46 srcMap.put(edge.src(), edge);
47 actualVertexes.add(edge.src());
48 dstMap.put(edge.dst(), edge);
49 actualVertexes.add(edge.dst());
50 }
51
52 // Make an immutable copy of the edge and vertex sets
53 this.edges = ImmutableSet.copyOf(edges);
54 this.vertexes = actualVertexes.build();
55
56 // Build immutable copies of sources and destinations edge maps
57 sources = srcMap.build();
58 destinations = dstMap.build();
59 }
60
61 @Override
62 public Set<V> getVertexes() {
63 return vertexes;
64 }
65
66 @Override
67 public Set<E> getEdges() {
68 return edges;
69 }
70
71 @Override
72 public Set<E> getEdgesFrom(V src) {
73 return sources.get(src);
74 }
75
76 @Override
77 public Set<E> getEdgesTo(V dst) {
78 return destinations.get(dst);
79 }
80
81 @Override
82 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070083 if (this == obj) {
84 return true;
85 }
tome3489412014-08-29 02:30:38 -070086 if (obj instanceof AdjacencyListsGraph) {
87 AdjacencyListsGraph that = (AdjacencyListsGraph) obj;
88 return this.getClass() == that.getClass() &&
89 Objects.equals(this.vertexes, that.vertexes) &&
90 Objects.equals(this.edges, that.edges);
91 }
92 return false;
93 }
94
95 @Override
96 public int hashCode() {
97 return Objects.hash(vertexes, edges);
98 }
99
100 @Override
101 public String toString() {
tomeadbb462014-09-07 16:10:19 -0700102 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -0700103 .add("vertexes", vertexes)
104 .add("edges", edges)
105 .toString();
106 }
107}