blob: 2881e9ab9adcbbeb32021cff55142556a491d7e7 (
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
|
// 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.vespa.flags.FetchVector;
import com.yahoo.vespa.flags.json.wire.WireCondition;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
/**
* @author hakonhall
*/
public interface Condition extends Predicate<FetchVector> {
enum Type {
WHITELIST,
BLACKLIST,
RELATIONAL;
public String toWire() { return name().toLowerCase(); }
public static Type fromWire(String typeString) {
for (Type type : values()) {
if (type.name().equalsIgnoreCase(typeString)) {
return type;
}
}
throw new IllegalArgumentException("Unknown type: '" + typeString + "'");
}
}
class CreateParams {
private final FetchVector.Dimension dimension;
private List<String> values = List.of();
private Optional<String> predicate = Optional.empty();
public CreateParams(FetchVector.Dimension dimension) { this.dimension = Objects.requireNonNull(dimension); }
public CreateParams withValues(String... values) { return withValues(List.of(values)); }
public CreateParams withValues(List<String> values) {
this.values = List.copyOf(values);
return this;
}
public CreateParams withPredicate(String predicate) {
this.predicate = Optional.of(predicate);
return this;
}
public FetchVector.Dimension dimension() { return dimension; }
public List<String> values() { return values; }
public Optional<String> predicate() { return predicate; }
public Condition createAs(Condition.Type type) {
switch (type) {
case WHITELIST: return WhitelistCondition.create(this);
case BLACKLIST: return BlacklistCondition.create(this);
case RELATIONAL: return RelationalCondition.create(this);
}
throw new IllegalArgumentException("Unknown type '" + type + "'");
}
}
static Condition fromWire(WireCondition wireCondition) {
Objects.requireNonNull(wireCondition.type);
Condition.Type type = Condition.Type.fromWire(wireCondition.type);
Objects.requireNonNull(wireCondition.dimension);
FetchVector.Dimension dimension = DimensionHelper.fromWire(wireCondition.dimension);
var params = new CreateParams(dimension);
if (wireCondition.values != null) {
params.withValues(wireCondition.values);
}
if (wireCondition.predicate != null) {
params.withPredicate(wireCondition.predicate);
}
return params.createAs(type);
}
Condition.Type type();
FetchVector.Dimension dimension();
CreateParams toCreateParams();
WireCondition toWire();
}
|