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