tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 1 | package org.onlab.graph; |
| 2 | |
| 3 | import com.google.common.collect.ImmutableSet; |
| 4 | import com.google.common.collect.ImmutableSetMultimap; |
| 5 | |
| 6 | import java.util.Objects; |
| 7 | import java.util.Set; |
| 8 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 9 | import static com.google.common.base.MoreObjects.toStringHelper; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 10 | import 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 | */ |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 18 | public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>> |
| 19 | implements Graph<V, E> { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 20 | |
| 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 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 27 | /** |
| 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) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 83 | if (this == obj) { |
| 84 | return true; |
| 85 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 86 | 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() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 102 | return toStringHelper(this) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 103 | .add("vertexes", vertexes) |
| 104 | .add("edges", edges) |
| 105 | .toString(); |
| 106 | } |
| 107 | } |