blob: a4fc386649089c46d79616b8a2e73bd1f95e77ec [file] [log] [blame]
tome3489412014-08-29 02:30:38 -07001package org.onlab.graph;
2
3import com.google.common.collect.ImmutableList;
4
5import java.util.Collections;
6import java.util.List;
7import java.util.Objects;
8
tomeadbb462014-09-07 16:10:19 -07009import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070010import static com.google.common.base.Preconditions.checkArgument;
11import static com.google.common.base.Preconditions.checkNotNull;
12
13/**
14 * Simple concrete implementation of a directed graph path.
15 */
16public 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() {
tomeadbb462014-09-07 16:10:19 -070060 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -070061 .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) {
tomfc9a4ff2014-09-22 18:22:47 -070075 if (this == obj) {
76 return true;
77 }
tome3489412014-08-29 02:30:38 -070078 if (obj instanceof DefaultPath) {
79 final DefaultPath other = (DefaultPath) obj;
tom144de692014-08-29 11:38:44 -070080 return Objects.equals(this.src, other.src) &&
tome3489412014-08-29 02:30:38 -070081 Objects.equals(this.dst, other.dst) &&
82 Objects.equals(this.cost, other.cost) &&
83 Objects.equals(this.edges, other.edges);
84 }
85 return false;
86 }
87
88}