aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/json/RelationalOperator.java
blob: 566ff0fa8eb3f08583fd37403f1be31729fd66f8 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.flags.json;

import java.util.Objects;
import java.util.function.Function;

/**
 * @author hakonhall
 */
public enum RelationalOperator {
    EQUAL        ("==", compareToValue -> compareToValue == 0),
    NOT_EQUAL    ("!=", compareToValue -> compareToValue != 0),
    LESS_EQUAL   ("<=", compareToValue -> compareToValue <= 0),
    LESS         ("<" , compareToValue -> compareToValue <  0),
    GREATER_EQUAL(">=", compareToValue -> compareToValue >= 0),
    GREATER      (">" , compareToValue -> compareToValue >  0);

    private String text;
    private final Function<Integer, Boolean> compareToValuePredicate;

    RelationalOperator(String text, Function<Integer, Boolean> compareToValuePredicate) {
        this.text = text;
        this.compareToValuePredicate = compareToValuePredicate;
    }

    public String toText() { return text; }

    /** Returns true if 'left op right' is true, with 'op' being the operator represented by this. */
    public <T extends Comparable<T>> boolean evaluate(T left, T right) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        return evaluate(left.compareTo(right));
    }

    public boolean evaluate(int compareToValue) {
        return compareToValuePredicate.apply(compareToValue);
    }
}