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