Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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.ImmutableList; |
| 19 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 20 | import java.util.List; |
| 21 | import java.util.Objects; |
| 22 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Simple concrete implementation of a directed graph path. |
| 29 | */ |
| 30 | public class DefaultPath<V extends Vertex, E extends Edge<V>> implements Path<V, E> { |
| 31 | |
| 32 | private final V src; |
| 33 | private final V dst; |
| 34 | private final List<E> edges; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 35 | private Weight cost; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Creates a new path from the specified list of edges and cost. |
| 39 | * |
| 40 | * @param edges list of path edges |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 41 | * @param cost path cost as a weight object |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 42 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 43 | public DefaultPath(List<E> edges, Weight cost) { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 44 | checkNotNull(edges, "Edges list must not be null"); |
| 45 | checkArgument(!edges.isEmpty(), "There must be at least one edge"); |
| 46 | this.edges = ImmutableList.copyOf(edges); |
| 47 | this.src = edges.get(0).src(); |
| 48 | this.dst = edges.get(edges.size() - 1).dst(); |
| 49 | this.cost = cost; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public V src() { |
| 54 | return src; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public V dst() { |
| 59 | return dst; |
| 60 | } |
| 61 | |
| 62 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 63 | public Weight cost() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 64 | return cost; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public List<E> edges() { |
Yuta HIGUCHI | 945bed2 | 2017-04-15 10:57:18 -0700 | [diff] [blame] | 69 | return edges; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String toString() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 74 | return toStringHelper(this) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 75 | .add("src", src) |
| 76 | .add("dst", dst) |
| 77 | .add("cost", cost) |
| 78 | .add("edges", edges) |
| 79 | .toString(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int hashCode() { |
| 84 | return Objects.hash(src, dst, edges, cost); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 89 | if (this == obj) { |
| 90 | return true; |
| 91 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 92 | if (obj instanceof DefaultPath) { |
| 93 | final DefaultPath other = (DefaultPath) obj; |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 94 | return Objects.equals(this.src, other.src) && |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 95 | Objects.equals(this.dst, other.dst) && |
| 96 | Objects.equals(this.cost, other.cost) && |
| 97 | Objects.equals(this.edges, other.edges); |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | } |