blob: 1cf22b6a7991f833d5d352ee0782cccb7872b3dd [file] [log] [blame]
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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
18package org.onlab.graph;
19
20import java.util.List;
21import java.util.Objects;
22import java.util.Set;
23
24import static com.google.common.collect.ImmutableSet.of;
25import static com.google.common.base.MoreObjects.toStringHelper;
26
27
28public class DisjointPathPair<V extends Vertex, E extends Edge<V>> implements Path<V, E> {
29 public Path<V, E> path1, path2;
30 boolean usingPath1 = true;
31
32<<<<<<< HEAD
33 /**
34 * Creates a Disjoint Path Pair from two paths.
35 *
36 * @param p1 first path
37 * @param p2 second path
38 */
39=======
40>>>>>>> Disjoint Path Pairs (Suurballe) utils
41 public DisjointPathPair(Path<V, E> p1, Path<V, E> p2) {
42 path1 = p1;
43 path2 = p2;
44 }
45<<<<<<< HEAD
46
47 @Override
48=======
49>>>>>>> Disjoint Path Pairs (Suurballe) utils
50 public V src() {
51 return path1.src();
52 }
53
54<<<<<<< HEAD
55 @Override
56 public V dst() {
57 return path1.dst();
58 }
59
60 @Override
61=======
62 public V dst() {
63 return path1.dst();
64 }
65>>>>>>> Disjoint Path Pairs (Suurballe) utils
66 public double cost() {
67 if (!hasBackup()) {
68 return path1.cost();
69 }
70 return path1.cost() + path2.cost();
71 }
72<<<<<<< HEAD
73
74 @Override
75=======
76>>>>>>> Disjoint Path Pairs (Suurballe) utils
77 public List<E> edges() {
78 if (usingPath1 || !hasBackup()) {
79 return path1.edges();
80 } else {
81 return path2.edges();
82 }
83 }
84<<<<<<< HEAD
85
86 /**
87 * Checks if this path pair contains a backup/secondary path.
88 *
89 * @return boolean representing whether it has backup
90 */
91 public boolean hasBackup() {
92 return path2 != null && path2.edges() != null;
93 }
94
95 /**
96 * Switches this disjoint path pair to using its backup path, instead of
97 * using its primary.
98 */
99 public void useBackup() {
100 usingPath1 = !usingPath1;
101 }
102
103 @Override
104=======
105 public boolean hasBackup() {
106 return path2 != null && path2.edges() != null;
107 }
108 public void useBackup() {
109 usingPath1 = !usingPath1;
110 }
111>>>>>>> Disjoint Path Pairs (Suurballe) utils
112 public String toString() {
113 return toStringHelper(this)
114 .add("src", src())
115 .add("dst", dst())
116 .add("cost", cost())
117 .add("edges", edges())
118 .toString();
119 }
120<<<<<<< HEAD
121
122 @Override
123=======
124>>>>>>> Disjoint Path Pairs (Suurballe) utils
125 public int hashCode() {
126 Set<Path<V, E>> paths;
127 if (!hasBackup()) {
128 paths = of(path1);
129 } else {
130 paths = of(path1, path2);
131 }
132 return Objects.hash(paths);
133 }
134<<<<<<< HEAD
135
136 @Override
137=======
138>>>>>>> Disjoint Path Pairs (Suurballe) utils
139 public boolean equals(Object obj) {
140 if (this == obj) {
141 return true;
142 }
143 if (obj instanceof DisjointPathPair) {
144 final DisjointPathPair other = (DisjointPathPair) obj;
145 return Objects.equals(this.src(), other.src()) &&
146 Objects.equals(this.dst(), other.dst()) &&
147 (Objects.equals(this.path1, other.path1) &&
148 Objects.equals(this.path2, other.path2)) ||
149 (Objects.equals(this.path1, other.path2) &&
150 Objects.equals(this.path2, other.path1));
151 }
152 return false;
153 }
154<<<<<<< HEAD
155
156 /**
157 * Returns number of paths inside this path pair object.
158 *
159 * @return number of paths
160 */
161=======
162>>>>>>> Disjoint Path Pairs (Suurballe) utils
163 public int size() {
164 if (hasBackup()) {
165 return 2;
166 }
167 return 1;
168 }
169}