Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | package org.onlab.graph; |
| 19 | |
| 20 | import java.util.List; |
| 21 | import java.util.Objects; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 22 | |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 24 | |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Pair of disjoint paths. |
| 27 | * |
| 28 | * @param <V> type of vertex |
| 29 | * @param <E> type of edge |
| 30 | */ |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 31 | public class DisjointPathPair<V extends Vertex, E extends Edge<V>> implements Path<V, E> { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 32 | |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 33 | private final Path<V, E> primary, secondary; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 36 | * Creates a disjoint path pair from two paths. |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 37 | * |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 38 | * @param primary primary path |
| 39 | * @param secondary secondary path |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 40 | */ |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 41 | public DisjointPathPair(Path<V, E> primary, Path<V, E> secondary) { |
| 42 | this.primary = primary; |
| 43 | this.secondary = secondary; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public V src() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 48 | return primary.src(); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public V dst() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 53 | return primary.dst(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the primary path. |
| 58 | * |
| 59 | * @return primary path |
| 60 | */ |
| 61 | public Path<V, E> primary() { |
| 62 | return primary; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the secondary path. |
| 67 | * |
Yuta HIGUCHI | c3d69f5 | 2016-08-08 11:52:52 -0700 | [diff] [blame] | 68 | * @return secondary path, or null if there is no secondary path available. |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 69 | */ |
| 70 | public Path<V, E> secondary() { |
| 71 | return secondary; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 75 | public Weight cost() { |
| 76 | return hasBackup() ? primary.cost().merge(secondary.cost()) : primary.cost(); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public List<E> edges() { |
Yuta HIGUCHI | 6deacbd | 2017-05-22 23:25:05 -0700 | [diff] [blame] | 81 | return primary.edges(); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Checks if this path pair contains a backup/secondary path. |
| 86 | * |
| 87 | * @return boolean representing whether it has backup |
| 88 | */ |
| 89 | public boolean hasBackup() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 90 | return secondary != null; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String toString() { |
| 95 | return toStringHelper(this) |
| 96 | .add("src", src()) |
| 97 | .add("dst", dst()) |
| 98 | .add("cost", cost()) |
| 99 | .add("edges", edges()) |
| 100 | .toString(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 105 | // Note: DisjointPathPair with primary and secondary swapped |
| 106 | // must result in same hashCode |
| 107 | return hasBackup() ? primary.hashCode() + secondary.hashCode() : |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 108 | Objects.hash(primary); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean equals(Object obj) { |
| 113 | if (this == obj) { |
| 114 | return true; |
| 115 | } |
| 116 | if (obj instanceof DisjointPathPair) { |
| 117 | final DisjointPathPair other = (DisjointPathPair) obj; |
| 118 | return Objects.equals(this.src(), other.src()) && |
| 119 | Objects.equals(this.dst(), other.dst()) && |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 120 | (Objects.equals(this.primary, other.primary) && |
| 121 | Objects.equals(this.secondary, other.secondary)) || |
| 122 | (Objects.equals(this.primary, other.secondary) && |
| 123 | Objects.equals(this.secondary, other.primary)); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns number of paths inside this path pair object. |
| 130 | * |
| 131 | * @return number of paths |
| 132 | */ |
| 133 | public int size() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 134 | return hasBackup() ? 2 : 1; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 135 | } |
| 136 | } |