blob: 5198a2b974e724c8058b2945e4911ff3fedee12e [file] [log] [blame]
Andrey Komarov2398d962016-09-26 15:11:23 +03001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrey Komarov2398d962016-09-26 15:11:23 +03003 *
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
17package org.onlab.graph;
18
19import static com.google.common.base.MoreObjects.toStringHelper;
20
21import com.google.common.math.DoubleMath;
22
23import java.util.Objects;
24
25/**
26 * Weight implementation based on a double value.
27 */
28public class ScalarWeight implements Weight {
29
30 /**
31 * Instance of scalar weight to mark links/paths which
32 * can not be traversed.
33 */
34 public static final ScalarWeight NON_VIABLE_WEIGHT =
35 new ScalarWeight(Double.POSITIVE_INFINITY);
36
37 private static double samenessThreshold = Double.MIN_VALUE;
38
Yuta HIGUCHIfa9ee8c2017-02-02 20:00:14 -080039 private final double value;
40
41 /**
42 * Creates a new scalar weight with the given double value.
43 * @param value double value
44 * @return scalar weight instance
45 */
46 public static ScalarWeight toWeight(double value) {
47 return new ScalarWeight(value);
48 }
Andrey Komarov2398d962016-09-26 15:11:23 +030049
50 /**
51 * Creates a new scalar weight with the given double value.
52 * @param value double value
53 */
54 public ScalarWeight(double value) {
55 this.value = value;
56 }
57
58 @Override
59 public Weight merge(Weight otherWeight) {
60 return new ScalarWeight(value + ((ScalarWeight) otherWeight).value);
61 }
62
63 @Override
64 public Weight subtract(Weight otherWeight) {
65 return new ScalarWeight(value - ((ScalarWeight) otherWeight).value);
66 }
67
68 @Override
69 public boolean isViable() {
Yuta HIGUCHI6c28aa42017-01-25 20:54:09 -080070 return !this.equals(NON_VIABLE_WEIGHT);
Andrey Komarov2398d962016-09-26 15:11:23 +030071 }
72
73 @Override
74 public int compareTo(Weight otherWeight) {
75 //check equality with samenessThreshold
76 if (equals(otherWeight)) {
77 return 0;
78 }
79 return Double.compare(value, ((ScalarWeight) otherWeight).value);
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 return ((obj instanceof ScalarWeight) &&
85 (DoubleMath.fuzzyEquals(value, ((ScalarWeight) obj).value,
86 samenessThreshold)));
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hash(value);
92 }
93
94 @Override
95 public boolean isNegative() {
96 return value < 0;
97 }
98
99 @Override
100 public String toString() {
101 return toStringHelper(this).add("value", value).toString();
102 }
103
104 /**
105 * Returns inner double value.
106 *
107 * @return double value
108 */
109 public double value() {
110 return value;
111 }
112
113 /**
114 * Sets a new sameness threshold for comparing cost values; default is
115 * is {@link Double#MIN_VALUE}.
116 *
117 * @param threshold fractional double value
118 */
119 public static void setSamenessThreshold(double threshold) {
120 samenessThreshold = threshold;
121 }
122
123 /**
124 * Returns the current sameness threshold for comparing cost values.
125 *
126 * @return current threshold
127 */
128 public static double samenessThreshold() {
129 return samenessThreshold;
130 }
131}