aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/select/Result.java
blob: 3109caa3277366546372f7d6cc5c51ec16af8543 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.select;

import com.yahoo.document.select.rule.AttributeNode;

/**
 * @author Simon Thoresen Hult
 */
public enum Result {

    /**
     * Defines all enumeration constants.
     */
    TRUE,
    FALSE,
    INVALID;

    @Override
    public String toString() {
        return name().toLowerCase();
    }
    
    /**
     * Inverts the result value to the appropriate value. True → False, False → True and Invalid → Invalid.
     * @return inverted result
     */
    public static Result invert(Result result) {
        if (result == Result.TRUE) return Result.FALSE;
        if (result == Result.FALSE) return Result.TRUE;
        return Result.INVALID;
    }

    /**
     * Converts the given object value into an instance of this Result enumeration.
     *
     * @param value The object to convert.
     * @return The corresponding result value.
     */
    public static Result toResult(Object value) {
        if (value == null || value == Result.FALSE || value == Boolean.FALSE ||
            (value instanceof Number && ((Number)value).doubleValue() == 0)) {
            return Result.FALSE;
        } else if (value == INVALID) {
            return Result.INVALID;
        } else if (value instanceof AttributeNode.VariableValueList) {
            return ((AttributeNode.VariableValueList)value).isEmpty() ? Result.FALSE : Result.TRUE;
        } else if (value instanceof ResultList) {
            return ((ResultList)value).toResult();
        } else {
            return Result.TRUE;
        }
    }
}