blob: ccf0c2a20b2b673f6086dcfcdf7829959353bf01 [file] [log] [blame]
Andrey Komarov2398d962016-09-26 15:11:23 +03001/*
2 * Copyright 2016-present 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
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
39 private double value;
40
41 /**
42 * Creates a new scalar weight with the given double value.
43 * @param value double value
44 */
45 public ScalarWeight(double value) {
46 this.value = value;
47 }
48
49 @Override
50 public Weight merge(Weight otherWeight) {
51 return new ScalarWeight(value + ((ScalarWeight) otherWeight).value);
52 }
53
54 @Override
55 public Weight subtract(Weight otherWeight) {
56 return new ScalarWeight(value - ((ScalarWeight) otherWeight).value);
57 }
58
59 @Override
60 public boolean isViable() {
61 return this != NON_VIABLE_WEIGHT;
62 }
63
64 @Override
65 public int compareTo(Weight otherWeight) {
66 //check equality with samenessThreshold
67 if (equals(otherWeight)) {
68 return 0;
69 }
70 return Double.compare(value, ((ScalarWeight) otherWeight).value);
71 }
72
73 @Override
74 public boolean equals(Object obj) {
75 return ((obj instanceof ScalarWeight) &&
76 (DoubleMath.fuzzyEquals(value, ((ScalarWeight) obj).value,
77 samenessThreshold)));
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(value);
83 }
84
85 @Override
86 public boolean isNegative() {
87 return value < 0;
88 }
89
90 @Override
91 public String toString() {
92 return toStringHelper(this).add("value", value).toString();
93 }
94
95 /**
96 * Returns inner double value.
97 *
98 * @return double value
99 */
100 public double value() {
101 return value;
102 }
103
104 /**
105 * Sets a new sameness threshold for comparing cost values; default is
106 * is {@link Double#MIN_VALUE}.
107 *
108 * @param threshold fractional double value
109 */
110 public static void setSamenessThreshold(double threshold) {
111 samenessThreshold = threshold;
112 }
113
114 /**
115 * Returns the current sameness threshold for comparing cost values.
116 *
117 * @return current threshold
118 */
119 public static double samenessThreshold() {
120 return samenessThreshold;
121 }
122}