aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/BooleanValue.java
blob: 49f267ca52228e023254f29f4b7db37c705f03f1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.evaluation;

/**
 * A value which is either true or false.
 * In numerical context true is interpreted as 1 and false as 0.
 *
 * @author bratseth
 */
public class BooleanValue extends DoubleCompatibleValue {

    private final boolean value;

    /**
     * Create a boolean value which is frozen at the outset.
     */
    public static BooleanValue frozen(boolean value) {
        BooleanValue booleanValue = new BooleanValue(value);
        booleanValue.freeze();
        return booleanValue;
    }

    public BooleanValue(boolean value) {
        this.value = value;
    }

    public boolean asBoolean() { return value; };

    @Override
    public double asDouble() {
        return value ? 1 : 0;
    }

    @Override
    public Value asMutable() {
        if ( ! isFrozen()) return this;
        return new BooleanValue(value);
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    @Override
    public boolean equals(Object other) {
        if (this==other) return true;
        if ( ! (other instanceof Value)) return false;
        if ( ! ((Value) other).hasDouble()) return false;
        return this.value == ((Value) other).asBoolean();
    }

    @Override
    public int hashCode() {
        return value ? 1 : 3;
    }

}