Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 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 | package org.onlab.graph; |
| 18 | |
| 19 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 20 | |
| 21 | import com.google.common.math.DoubleMath; |
| 22 | |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | /** |
| 26 | * Weight implementation based on a double value. |
| 27 | */ |
| 28 | public 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 HIGUCHI | fa9ee8c | 2017-02-02 20:00:14 -0800 | [diff] [blame] | 39 | 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 Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 49 | |
| 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 HIGUCHI | 6c28aa4 | 2017-01-25 20:54:09 -0800 | [diff] [blame] | 70 | return !this.equals(NON_VIABLE_WEIGHT); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 71 | } |
| 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 | } |