blob: 575ddc63692561578f7d3b8eb92a4be2025a1102 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
tome3489412014-08-29 02:30:38 -070020import java.util.List;
21import java.util.Objects;
22
tomeadbb462014-09-07 16:10:19 -070023import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070024import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Simple concrete implementation of a directed graph path.
29 */
30public class DefaultPath<V extends Vertex, E extends Edge<V>> implements Path<V, E> {
31
32 private final V src;
33 private final V dst;
34 private final List<E> edges;
Andrey Komarov2398d962016-09-26 15:11:23 +030035 private Weight cost;
tome3489412014-08-29 02:30:38 -070036
37 /**
38 * Creates a new path from the specified list of edges and cost.
39 *
40 * @param edges list of path edges
Andrey Komarov2398d962016-09-26 15:11:23 +030041 * @param cost path cost as a weight object
tome3489412014-08-29 02:30:38 -070042 */
Andrey Komarov2398d962016-09-26 15:11:23 +030043 public DefaultPath(List<E> edges, Weight cost) {
tome3489412014-08-29 02:30:38 -070044 checkNotNull(edges, "Edges list must not be null");
45 checkArgument(!edges.isEmpty(), "There must be at least one edge");
46 this.edges = ImmutableList.copyOf(edges);
47 this.src = edges.get(0).src();
48 this.dst = edges.get(edges.size() - 1).dst();
49 this.cost = cost;
50 }
51
52 @Override
53 public V src() {
54 return src;
55 }
56
57 @Override
58 public V dst() {
59 return dst;
60 }
61
62 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +030063 public Weight cost() {
tome3489412014-08-29 02:30:38 -070064 return cost;
65 }
66
67 @Override
68 public List<E> edges() {
Yuta HIGUCHI945bed22017-04-15 10:57:18 -070069 return edges;
tome3489412014-08-29 02:30:38 -070070 }
71
72 @Override
73 public String toString() {
tomeadbb462014-09-07 16:10:19 -070074 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -070075 .add("src", src)
76 .add("dst", dst)
77 .add("cost", cost)
78 .add("edges", edges)
79 .toString();
80 }
81
82 @Override
83 public int hashCode() {
84 return Objects.hash(src, dst, edges, cost);
85 }
86
87 @Override
88 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070089 if (this == obj) {
90 return true;
91 }
tome3489412014-08-29 02:30:38 -070092 if (obj instanceof DefaultPath) {
93 final DefaultPath other = (DefaultPath) obj;
tom144de692014-08-29 11:38:44 -070094 return Objects.equals(this.src, other.src) &&
tome3489412014-08-29 02:30:38 -070095 Objects.equals(this.dst, other.dst) &&
96 Objects.equals(this.cost, other.cost) &&
97 Objects.equals(this.edges, other.edges);
98 }
99 return false;
100 }
101
102}