blob: 816fb1619b7fad89e2e87bc05d2c1e1d22ab155b [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
18import com.google.common.collect.ImmutableList;
19
20import java.util.Collections;
21import java.util.List;
22import java.util.Objects;
23
tomeadbb462014-09-07 16:10:19 -070024import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070025import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Simple concrete implementation of a directed graph path.
30 */
31public 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() {
tomeadbb462014-09-07 16:10:19 -070075 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -070076 .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) {
tomfc9a4ff2014-09-22 18:22:47 -070090 if (this == obj) {
91 return true;
92 }
tome3489412014-08-29 02:30:38 -070093 if (obj instanceof DefaultPath) {
94 final DefaultPath other = (DefaultPath) obj;
tom144de692014-08-29 11:38:44 -070095 return Objects.equals(this.src, other.src) &&
tome3489412014-08-29 02:30:38 -070096 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}