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