aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/json/RelationalCondition.java
blob: 49dc7c75752884c814351efed71b1193e3b8f5c1 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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 com.yahoo.component.Version;
import com.yahoo.vespa.flags.FetchVector;
import com.yahoo.vespa.flags.json.wire.WireCondition;

import java.util.Objects;
import java.util.function.Predicate;

/**
 * @author hakonhall
 */
public class RelationalCondition implements Condition {
    private final RelationalPredicate relationalPredicate;
    private final Predicate<String> predicate;
    private final FetchVector.Dimension dimension;

    public static RelationalCondition create(CreateParams params) {
        if (!params.values().isEmpty()) {
            throw new IllegalArgumentException(RelationalCondition.class.getSimpleName() +
                    " does not support the 'values' field");
        }

        String predicate = params.predicate().orElseThrow(() ->
                new IllegalArgumentException(RelationalCondition.class.getSimpleName() +
                        " requires the predicate field in the condition"));
        RelationalPredicate relationalPredicate = RelationalPredicate.fromWire(predicate);

        switch (params.dimension()) {
            case VESPA_VERSION:
                final Version rightVersion = Version.fromString(relationalPredicate.rightOperand());
                Predicate<String> p = (String leftString) -> {
                    Version leftVersion = Version.fromString(leftString);
                    return relationalPredicate.operator().evaluate(leftVersion, rightVersion);
                };
                return new RelationalCondition(relationalPredicate, p, params.dimension());
            default:
                throw new IllegalArgumentException(RelationalCondition.class.getSimpleName() +
                        " not supported for dimension " + FetchVector.Dimension.VESPA_VERSION.name());
        }
    }

    private RelationalCondition(RelationalPredicate relationalPredicate, Predicate<String> predicate,
                                FetchVector.Dimension dimension) {
        this.relationalPredicate = relationalPredicate;
        this.predicate = predicate;
        this.dimension = dimension;
    }

    @Override
    public Type type() {
        return Type.RELATIONAL;
    }

    @Override
    public FetchVector.Dimension dimension() {
        return dimension;
    }

    @Override
    public CreateParams toCreateParams() {
        return new CreateParams(dimension).withPredicate(relationalPredicate.toWire());
    }

    @Override
    public boolean test(FetchVector fetchVector) {
        return fetchVector.getValue(dimension).map(predicate::test).orElse(false);
    }

    public RelationalPredicate relationalPredicate() {
        return relationalPredicate;
    }

    @Override
    public WireCondition toWire() {
        var condition = new WireCondition();
        condition.type = Condition.Type.RELATIONAL.toWire();
        condition.dimension = DimensionHelper.toWire(dimension);
        condition.predicate = relationalPredicate.toWire();
        return condition;
    }

    @Override
    public String toString() {
        return "RelationalCondition{" +
               "relationalPredicate=" + relationalPredicate +
               ", predicate=" + predicate +
               ", dimension=" + dimension +
               '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RelationalCondition that = (RelationalCondition) o;
        return relationalPredicate.equals(that.relationalPredicate) && predicate.equals(that.predicate) && dimension == that.dimension;
    }

    @Override
    public int hashCode() {
        return Objects.hash(relationalPredicate, predicate, dimension);
    }
}