blob: 4a29adf1c39d20ed6be04308ec0f8eff72042877 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Andrey Komarov2398d962016-09-26 15:11:23 +030036 private Weight cost;
tome3489412014-08-29 02:30:38 -070037
38 /**
39 * Creates a new path from the specified list of edges and cost.
40 *
41 * @param edges list of path edges
Andrey Komarov2398d962016-09-26 15:11:23 +030042 * @param cost path cost as a weight object
tome3489412014-08-29 02:30:38 -070043 */
Andrey Komarov2398d962016-09-26 15:11:23 +030044 public DefaultPath(List<E> edges, Weight cost) {
tome3489412014-08-29 02:30:38 -070045 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
Andrey Komarov2398d962016-09-26 15:11:23 +030064 public Weight cost() {
tome3489412014-08-29 02:30:38 -070065 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}