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