blob: 1d6768d892c7f117caac94e12864fb6b4f667067 [file] [log] [blame]
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -07003 *
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
18package org.onlab.graph;
19
20import java.util.List;
21import java.util.Objects;
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070022
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070023import static com.google.common.base.MoreObjects.toStringHelper;
24
Thomas Vachuska48e64e42015-09-22 15:32:55 -070025/**
26 * Pair of disjoint paths.
27 *
28 * @param <V> type of vertex
29 * @param <E> type of edge
30 */
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070031public class DisjointPathPair<V extends Vertex, E extends Edge<V>> implements Path<V, E> {
Thomas Vachuska48e64e42015-09-22 15:32:55 -070032
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070033 private final Path<V, E> primary, secondary;
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070034
35 /**
Thomas Vachuska48e64e42015-09-22 15:32:55 -070036 * Creates a disjoint path pair from two paths.
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070037 *
Thomas Vachuska48e64e42015-09-22 15:32:55 -070038 * @param primary primary path
39 * @param secondary secondary path
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070040 */
Thomas Vachuska48e64e42015-09-22 15:32:55 -070041 public DisjointPathPair(Path<V, E> primary, Path<V, E> secondary) {
42 this.primary = primary;
43 this.secondary = secondary;
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070044 }
45
46 @Override
47 public V src() {
Thomas Vachuska48e64e42015-09-22 15:32:55 -070048 return primary.src();
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070049 }
50
51 @Override
52 public V dst() {
Thomas Vachuska48e64e42015-09-22 15:32:55 -070053 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 HIGUCHIc3d69f52016-08-08 11:52:52 -070068 * @return secondary path, or null if there is no secondary path available.
Thomas Vachuska48e64e42015-09-22 15:32:55 -070069 */
70 public Path<V, E> secondary() {
71 return secondary;
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070072 }
73
74 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +030075 public Weight cost() {
76 return hasBackup() ? primary.cost().merge(secondary.cost()) : primary.cost();
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070077 }
78
79 @Override
80 public List<E> edges() {
Yuta HIGUCHI6deacbd2017-05-22 23:25:05 -070081 return primary.edges();
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070082 }
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 Yutaca9cc8e2015-10-29 23:26:51 -070090 return secondary != null;
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070091 }
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 Yutaca9cc8e2015-10-29 23:26:51 -0700105 // Note: DisjointPathPair with primary and secondary swapped
106 // must result in same hashCode
107 return hasBackup() ? primary.hashCode() + secondary.hashCode() :
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700108 Objects.hash(primary);
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700109 }
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 Vachuska48e64e42015-09-22 15:32:55 -0700120 (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 Cheerlaf7c2e1a2015-07-09 12:06:37 -0700124 }
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 Vachuska48e64e42015-09-22 15:32:55 -0700134 return hasBackup() ? 2 : 1;
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700135 }
136}