blob: 7279109e1f9f9b9e7368ffa1d090c7784b888737 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tome3489412014-08-29 02:30:38 -070019package org.onlab.graph;
20
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.ImmutableSetMultimap;
23
24import java.util.Objects;
25import java.util.Set;
26
tomeadbb462014-09-07 16:10:19 -070027import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070028import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Immutable graph implemented using adjacency lists.
32 *
33 * @param <V> vertex type
34 * @param <E> edge type
35 */
tomcbff9392014-09-10 00:45:23 -070036public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>>
37 implements Graph<V, E> {
tome3489412014-08-29 02:30:38 -070038
39 private final Set<V> vertexes;
40 private final Set<E> edges;
41
42 private final ImmutableSetMultimap<V, E> sources;
43 private final ImmutableSetMultimap<V, E> destinations;
44
tome3489412014-08-29 02:30:38 -070045 /**
46 * Creates a graph comprising of the specified vertexes and edges.
47 *
48 * @param vertexes set of graph vertexes
49 * @param edges set of graph edges
50 */
51 public AdjacencyListsGraph(Set<V> vertexes, Set<E> edges) {
52 checkNotNull(vertexes, "Vertex set cannot be null");
53 checkNotNull(edges, "Edge set cannot be null");
54
55 // Record ingress/egress edges for each vertex.
56 ImmutableSetMultimap.Builder<V, E> srcMap = ImmutableSetMultimap.builder();
57 ImmutableSetMultimap.Builder<V, E> dstMap = ImmutableSetMultimap.builder();
58
59 // Also make sure that all edge end-points are added as vertexes
60 ImmutableSet.Builder<V> actualVertexes = ImmutableSet.builder();
61 actualVertexes.addAll(vertexes);
62
63 for (E edge : edges) {
64 srcMap.put(edge.src(), edge);
65 actualVertexes.add(edge.src());
66 dstMap.put(edge.dst(), edge);
67 actualVertexes.add(edge.dst());
68 }
69
70 // Make an immutable copy of the edge and vertex sets
71 this.edges = ImmutableSet.copyOf(edges);
72 this.vertexes = actualVertexes.build();
73
74 // Build immutable copies of sources and destinations edge maps
75 sources = srcMap.build();
76 destinations = dstMap.build();
77 }
78
79 @Override
80 public Set<V> getVertexes() {
81 return vertexes;
82 }
83
84 @Override
85 public Set<E> getEdges() {
86 return edges;
87 }
88
89 @Override
90 public Set<E> getEdgesFrom(V src) {
91 return sources.get(src);
92 }
93
94 @Override
95 public Set<E> getEdgesTo(V dst) {
96 return destinations.get(dst);
97 }
98
99 @Override
100 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700101 if (this == obj) {
102 return true;
103 }
tome3489412014-08-29 02:30:38 -0700104 if (obj instanceof AdjacencyListsGraph) {
105 AdjacencyListsGraph that = (AdjacencyListsGraph) obj;
106 return this.getClass() == that.getClass() &&
107 Objects.equals(this.vertexes, that.vertexes) &&
108 Objects.equals(this.edges, that.edges);
109 }
110 return false;
111 }
112
113 @Override
114 public int hashCode() {
115 return Objects.hash(vertexes, edges);
116 }
117
118 @Override
119 public String toString() {
tomeadbb462014-09-07 16:10:19 -0700120 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -0700121 .add("vertexes", vertexes)
122 .add("edges", edges)
123 .toString();
124 }
125}