blob: b62d3b24b1e8976d940b2c98abde5cf8706489c7 [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 /**
33 * Creates a Disjoint Path Pair from two paths.
34 *
35 * @param p1 first path
36 * @param p2 second path
37 */
38 public DisjointPathPair(Path<V, E> p1, Path<V, E> p2) {
39 path1 = p1;
40 path2 = p2;
41 }
42
43 @Override
44 public V src() {
45 return path1.src();
46 }
47
48 @Override
49 public V dst() {
50 return path1.dst();
51 }
52
53 @Override
54 public double cost() {
55 if (!hasBackup()) {
56 return path1.cost();
57 }
58 return path1.cost() + path2.cost();
59 }
60
61 @Override
62 public List<E> edges() {
63 if (usingPath1 || !hasBackup()) {
64 return path1.edges();
65 } else {
66 return path2.edges();
67 }
68 }
69
70 /**
71 * Checks if this path pair contains a backup/secondary path.
72 *
73 * @return boolean representing whether it has backup
74 */
75 public boolean hasBackup() {
76 return path2 != null && path2.edges() != null;
77 }
78
79 @Override
80 public String toString() {
81 return toStringHelper(this)
82 .add("src", src())
83 .add("dst", dst())
84 .add("cost", cost())
85 .add("edges", edges())
86 .toString();
87 }
88
89 @Override
90 public int hashCode() {
91 Set<Path<V, E>> paths;
92 if (!hasBackup()) {
93 paths = of(path1);
94 } else {
95 paths = of(path1, path2);
96 }
97 return Objects.hash(paths);
98 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105 if (obj instanceof DisjointPathPair) {
106 final DisjointPathPair other = (DisjointPathPair) obj;
107 return Objects.equals(this.src(), other.src()) &&
108 Objects.equals(this.dst(), other.dst()) &&
109 (Objects.equals(this.path1, other.path1) &&
110 Objects.equals(this.path2, other.path2)) ||
111 (Objects.equals(this.path1, other.path2) &&
112 Objects.equals(this.path2, other.path1));
113 }
114 return false;
115 }
116
117 /**
118 * Returns number of paths inside this path pair object.
119 *
120 * @return number of paths
121 */
122 public int size() {
123 if (hasBackup()) {
124 return 2;
125 }
126 return 1;
127 }
128}