Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | import java.util.Objects; |
| 23 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Simple concrete implementation of a directed graph path. |
| 30 | */ |
| 31 | public class DefaultMutablePath<V extends Vertex, E extends Edge<V>> implements MutablePath<V, E> { |
| 32 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 33 | private final List<E> edges = new ArrayList<>(); |
| 34 | private double cost = 0.0; |
| 35 | |
| 36 | /** |
| 37 | * Creates a new empty path. |
| 38 | */ |
| 39 | public DefaultMutablePath() { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Creates a new path as a copy of another path. |
| 44 | * |
| 45 | * @param path path to be copied |
| 46 | */ |
| 47 | public DefaultMutablePath(Path<V, E> path) { |
| 48 | checkNotNull(path, "Path cannot be null"); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 49 | this.cost = path.cost(); |
| 50 | edges.addAll(path.edges()); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public V src() { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 55 | return edges.isEmpty() ? null : edges.get(0).src(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public V dst() { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 60 | return edges.isEmpty() ? null : edges.get(edges.size() - 1).dst(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public double cost() { |
| 65 | return cost; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public List<E> edges() { |
| 70 | return ImmutableList.copyOf(edges); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void setCost(double cost) { |
| 75 | this.cost = cost; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Path<V, E> toImmutable() { |
| 80 | return new DefaultPath<>(edges, cost); |
| 81 | } |
| 82 | |
| 83 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 84 | public void insertEdge(E edge) { |
| 85 | checkNotNull(edge, "Edge cannot be null"); |
| 86 | checkArgument(edges.isEmpty() || src().equals(edge.dst()), |
| 87 | "Edge destination must be the same as the current path source"); |
| 88 | edges.add(0, edge); |
| 89 | } |
| 90 | |
| 91 | @Override |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 92 | public void appendEdge(E edge) { |
| 93 | checkNotNull(edge, "Edge cannot be null"); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 94 | checkArgument(edges.isEmpty() || dst().equals(edge.src()), |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 95 | "Edge source must be the same as the current path destination"); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 96 | edges.add(edge); |
| 97 | } |
| 98 | |
| 99 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 100 | public void removeEdge(E edge) { |
| 101 | checkArgument(edge.src().equals(edge.dst()) || |
| 102 | edges.indexOf(edge) == 0 || |
| 103 | edges.lastIndexOf(edge) == edges.size() - 1, |
| 104 | "Edge must be at start or end of path, or it must be a cyclic edge"); |
| 105 | edges.remove(edge); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 110 | return toStringHelper(this) |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 111 | .add("src", src()) |
| 112 | .add("dst", dst()) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 113 | .add("cost", cost) |
| 114 | .add("edges", edges) |
| 115 | .toString(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public int hashCode() { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 120 | return Objects.hash(edges, cost); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 125 | if (this == obj) { |
| 126 | return true; |
| 127 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 128 | if (obj instanceof DefaultMutablePath) { |
| 129 | final DefaultMutablePath other = (DefaultMutablePath) obj; |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 130 | return Objects.equals(this.cost, other.cost) && |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 131 | Objects.equals(this.edges, other.edges); |
| 132 | } |
| 133 | return false; |
| 134 | } |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 135 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 136 | } |