aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/result/Relevance.java
blob: 5c3e322e881479005b832b50967db9b295f44fc3 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;

/**
 * A relevance double value. These values should ideally be normalized between 0 and 1 (where 1 means "perfect"),
 * however this is not enforced.
 * <p>
 * Sources may create subclasses of this to include additional information or functionality.
 *
 * @author bratseth
 */
public class Relevance implements Comparable<Relevance> {

    /** The relevancy score. */
    private double score;

    /**
     * Construct a relevancy object with an initial value.
     * This initial value should ideally be a normalized value
     * between 0 and 1, but that is not enforced.
     *
     * @param score the initial value (rank score)
     */
    public Relevance(double score) {
        this.score = score;
    }

    /**
     * Set score value to this value. This should ideally be a
     * normalized value between 0 and 1, but that is not enforced.
     * NaN is also a legal value, for elements where it makes no sense to assign a particular value.
     */
    public void setScore(double score) { this.score = score; }

    /**
     * Returns the relevancy score of this, preferably a normalized value
     * between 0 and 1 but this is not guaranteed by this framework
     */
    public double getScore() { return score; }

    /**
     * Returns the score value as a string
     */
    @Override
    public String toString() {
        return String.valueOf(score);
    }

    /** Compares relevancy values with */
    @Override
    public int compareTo(Relevance other) {
        double thisScore = getScore();
        double otherScore = other.getScore();
        if (Double.isNaN(thisScore)) {
            return Double.isNaN(otherScore) ? 0 : -1;
        } else if (Double.isNaN(otherScore)) {
            return 1;
        } else {
            return Double.compare(thisScore, otherScore);
        }
    }

    /** Compares relevancy values */
    @Override
    public boolean equals(Object other) {
        if (other == this) return true;
        if ( ! (other instanceof Relevance)) { return false; }
        return this.compareTo((Relevance)other) == 0;
    }

    /** Returns a hash from the relevancy value */
    @Override
    public int hashCode() {
        return Double.hashCode(score);
    }

}