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