Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.ImmutableSetMultimap; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | import java.util.Set; |
| 23 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 25 | import 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 | */ |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 33 | public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>> |
| 34 | implements Graph<V, E> { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 35 | |
| 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 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 42 | /** |
| 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) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 98 | if (this == obj) { |
| 99 | return true; |
| 100 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 101 | 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() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 117 | return toStringHelper(this) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 118 | .add("vertexes", vertexes) |
| 119 | .add("edges", edges) |
| 120 | .toString(); |
| 121 | } |
| 122 | } |