blob: 0d50f1e283f412c96e4e040095359ba186b3e571 (
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
|
// Copyright 2018 Yahoo Holdings. 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.vespa.flags.FetchVector;
import com.yahoo.vespa.flags.JsonNodeRawFlag;
import com.yahoo.vespa.flags.RawFlag;
import com.yahoo.vespa.flags.json.wire.WireRule;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author hakonhall
*/
public class Rule {
private final List<Condition> andConditions;
private final Optional<RawFlag> valueToApply;
public Rule(Optional<RawFlag> valueToApply, Condition... andConditions) {
this(valueToApply, List.of(andConditions));
}
public Rule(Optional<RawFlag> valueToApply, List<Condition> andConditions) {
this.andConditions = List.copyOf(andConditions);
this.valueToApply = valueToApply;
}
public List<Condition> conditions() {
return andConditions;
}
/** Returns true if all the conditions satisfy the given fetch vector */
public boolean match(FetchVector fetchVector) {
return andConditions.stream().allMatch(condition -> condition.test(fetchVector));
}
/**
* Returns true if all the conditions on dimensions set in the fetch vector are satisfied.
* Conditions on dimensions not specified in the given fetch vector are ignored.
*/
public boolean partialMatch(FetchVector fetchVector) {
return andConditions.stream()
.allMatch(condition -> !fetchVector.hasDimension(condition.dimension()) || condition.test(fetchVector));
}
public Optional<RawFlag> getValueToApply() {
return valueToApply;
}
public WireRule toWire() {
WireRule wireRule = new WireRule();
if (!andConditions.isEmpty()) {
wireRule.andConditions = andConditions.stream().map(Condition::toWire).collect(Collectors.toList());
}
wireRule.value = valueToApply.map(RawFlag::asJsonNode).orElse(null);
return wireRule;
}
public static Rule fromWire(WireRule wireRule) {
List<Condition> conditions = wireRule.andConditions == null ?
Collections.emptyList() :
wireRule.andConditions.stream().map(Condition::fromWire).collect(Collectors.toList());
Optional<RawFlag> value = wireRule.value == null ? Optional.empty() : Optional.of(JsonNodeRawFlag.fromJsonNode(wireRule.value));
return new Rule(value, conditions);
}
}
|