blob: 6a9362c382825e6f5f516510ee1a4574f71fc1b9 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tome3489412014-08-29 02:30:38 -070019package org.onlab.graph;
20
21import com.google.common.collect.ImmutableList;
22
23import java.util.Collections;
24import java.util.List;
25import java.util.Objects;
26
tomeadbb462014-09-07 16:10:19 -070027import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070028import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Simple concrete implementation of a directed graph path.
33 */
34public class DefaultPath<V extends Vertex, E extends Edge<V>> implements Path<V, E> {
35
36 private final V src;
37 private final V dst;
38 private final List<E> edges;
39 private double cost = 0.0;
40
41 /**
42 * Creates a new path from the specified list of edges and cost.
43 *
44 * @param edges list of path edges
45 * @param cost path cost as a unit-less number
46 */
47 public DefaultPath(List<E> edges, double cost) {
48 checkNotNull(edges, "Edges list must not be null");
49 checkArgument(!edges.isEmpty(), "There must be at least one edge");
50 this.edges = ImmutableList.copyOf(edges);
51 this.src = edges.get(0).src();
52 this.dst = edges.get(edges.size() - 1).dst();
53 this.cost = cost;
54 }
55
56 @Override
57 public V src() {
58 return src;
59 }
60
61 @Override
62 public V dst() {
63 return dst;
64 }
65
66 @Override
67 public double cost() {
68 return cost;
69 }
70
71 @Override
72 public List<E> edges() {
73 return Collections.unmodifiableList(edges);
74 }
75
76 @Override
77 public String toString() {
tomeadbb462014-09-07 16:10:19 -070078 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -070079 .add("src", src)
80 .add("dst", dst)
81 .add("cost", cost)
82 .add("edges", edges)
83 .toString();
84 }
85
86 @Override
87 public int hashCode() {
88 return Objects.hash(src, dst, edges, cost);
89 }
90
91 @Override
92 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070093 if (this == obj) {
94 return true;
95 }
tome3489412014-08-29 02:30:38 -070096 if (obj instanceof DefaultPath) {
97 final DefaultPath other = (DefaultPath) obj;
tom144de692014-08-29 11:38:44 -070098 return Objects.equals(this.src, other.src) &&
tome3489412014-08-29 02:30:38 -070099 Objects.equals(this.dst, other.dst) &&
100 Objects.equals(this.cost, other.cost) &&
101 Objects.equals(this.edges, other.edges);
102 }
103 return false;
104 }
105
106}