summaryrefslogtreecommitdiffstats
path: root/core/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/slitopologyutils/graph/ScalarWeight.java
blob: a9c2a0e524764ec5051fa385f786e3fe534397f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.onap.ccsdk.sli.core.slipluginutils.slitopologyutils.graph;

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;

/**
 * Weight implementation based on a double value.
 */
public class ScalarWeight implements Weight {

    /**
     * Instance of scalar weight to mark links/paths which
     * can not be traversed.
     */
    public static final ScalarWeight NON_VIABLE_WEIGHT =
            new ScalarWeight(Double.POSITIVE_INFINITY);

    private static double samenessThreshold = Double.MIN_VALUE;

    private final double value;

    /**
     * Creates a new scalar weight with the given double value.
     * @param value double value
     * @return scalar weight instance
     */
    public static ScalarWeight toWeight(double value) {
        return new ScalarWeight(value);
    }

    /**
     * Creates a new scalar weight with the given double value.
     * @param value double value
     */
    public ScalarWeight(double value) {
        this.value = value;
    }

    @Override
    public Weight merge(Weight otherWeight) {
        return new ScalarWeight(value + ((ScalarWeight) otherWeight).value);
    }

    @Override
    public Weight subtract(Weight otherWeight) {
        return new ScalarWeight(value - ((ScalarWeight) otherWeight).value);
    }

    @Override
    public boolean isViable() {
        return !this.equals(NON_VIABLE_WEIGHT);
    }

    @Override
    public int compareTo(Weight otherWeight) {
        //check equality with samenessThreshold
        if (equals(otherWeight)) {
            return 0;
        }
        return Double.compare(value, ((ScalarWeight) otherWeight).value);
    }

    @Override
    public boolean equals(Object obj) {
        return ((obj instanceof ScalarWeight) &&
                (Math.abs(value - ((ScalarWeight) obj).value) < samenessThreshold)
        );
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }

    @Override
    public boolean isNegative() {
        return value < 0;
    }

    @Override
    public String toString() {
        return toStringHelper(this).add("value", value).toString();
    }


    /**
     * Returns inner double value.
     *
     * @return double value
     */
    public double value() {
        return value;
    }

}