blob: 361c87fcf9076916fb5e7ce91e9a9fe7318ed5a8 [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 com.google.common.math.DoubleMath;
20
21import java.util.Objects;
22
23/**
24 * Test weight (based on double).
25 */
26public class TestDoubleWeight implements Weight {
27
28 /**
29 * Instance of negative test weight.
30 */
31 public static final TestDoubleWeight NEGATIVE_WEIGHT = new TestDoubleWeight(-1);
32
33 /**
34 * Instance of test weight to mark links/paths which
35 * can not be traversed.
36 */
37 public static final TestDoubleWeight NON_VIABLE_WEIGHT =
38 new TestDoubleWeight(Double.POSITIVE_INFINITY);
39
40 private final double value;
41
42 /**
43 * Creates a new test weight with the given double value.
44 * @param value double weight
45 */
46 public TestDoubleWeight(double value) {
47 this.value = value;
48 }
49
50 @Override
51 public Weight merge(Weight otherWeight) {
52 return new TestDoubleWeight(value + ((TestDoubleWeight) otherWeight).value);
53 }
54
55 @Override
56 public Weight subtract(Weight otherWeight) {
57 return new TestDoubleWeight(value - ((TestDoubleWeight) otherWeight).value);
58 }
59
60 @Override
61 public boolean isViable() {
Yuta HIGUCHI6c28aa42017-01-25 20:54:09 -080062 return !this.equals(NON_VIABLE_WEIGHT);
Andrey Komarov2398d962016-09-26 15:11:23 +030063 }
64
65 @Override
66 public int compareTo(Weight otherWeight) {
67 return Double.compare(value, ((TestDoubleWeight) otherWeight).value);
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 return (DoubleMath.fuzzyEquals(value, ((TestDoubleWeight) obj).value, 0.1));
73 }
74
75 @Override
76 public int hashCode() {
77 return Objects.hash(value);
78 }
79
80 @Override
81 public boolean isNegative() {
82 return value < 0;
83 }
Yuta HIGUCHIe3ebe692017-04-17 10:00:42 -070084
85 @Override
86 public String toString() {
87 return String.valueOf(value);
88 }
Andrey Komarov2398d962016-09-26 15:11:23 +030089}