-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathEvaluationContext.java
130 lines (104 loc) · 3.61 KB
/
EvaluationContext.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package dev.openfeature.sdk;
import java.time.Instant;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Delegate;
@ToString
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
public class EvaluationContext {
@Setter @Getter private String targetingKey;
@Delegate(excludes = HideDelegateAddMethods.class) private final Structure structure = new Structure();
public EvaluationContext() {
super();
this.targetingKey = "";
}
public EvaluationContext(String targetingKey) {
this();
this.targetingKey = targetingKey;
}
/**
* Merges two EvaluationContext objects with the second overriding the first in
* case of conflict.
*
* @param ctx1 base context
* @param ctx2 overriding context
* @return resulting merged context
*/
public static EvaluationContext merge(EvaluationContext ctx1, EvaluationContext ctx2) {
EvaluationContext ec = new EvaluationContext();
if (ctx1 == null) {
return ctx2;
} else if (ctx2 == null) {
return ctx1;
}
ec.structure.attributes.putAll(ctx1.structure.attributes);
ec.structure.attributes.putAll(ctx2.structure.attributes);
if (ctx1.getTargetingKey() != null && !ctx1.getTargetingKey().trim().equals("")) {
ec.setTargetingKey(ctx1.getTargetingKey());
}
if (ctx2.getTargetingKey() != null && !ctx2.getTargetingKey().trim().equals("")) {
ec.setTargetingKey(ctx2.getTargetingKey());
}
return ec;
}
// override @Delegate methods so that we can use "add" methods and still return EvaluationContext, not Structure
public EvaluationContext add(String key, Boolean value) {
this.structure.add(key, value);
return this;
}
public EvaluationContext add(String key, String value) {
this.structure.add(key, value);
return this;
}
public EvaluationContext add(String key, Integer value) {
this.structure.add(key, value);
return this;
}
public EvaluationContext add(String key, Double value) {
this.structure.add(key, value);
return this;
}
public EvaluationContext add(String key, Instant value) {
this.structure.add(key, value);
return this;
}
public EvaluationContext add(String key, Structure value) {
this.structure.add(key, value);
return this;
}
public EvaluationContext add(String key, List<Value> value) {
this.structure.add(key, value);
return this;
}
/**
* Hidden class to tell Lombok not to copy these methods over via delegation.
*/
private static class HideDelegateAddMethods {
public Structure add(String ignoredKey, Boolean ignoredValue) {
return null;
}
public Structure add(String ignoredKey, Double ignoredValue) {
return null;
}
public Structure add(String ignoredKey, String ignoredValue) {
return null;
}
public Structure add(String ignoredKey, Value ignoredValue) {
return null;
}
public Structure add(String ignoredKey, Integer ignoredValue) {
return null;
}
public Structure add(String ignoredKey, List<Value> ignoredValue) {
return null;
}
public Structure add(String ignoredKey, Structure ignoredValue) {
return null;
}
public Structure add(String ignoredKey, Instant ignoredValue) {
return null;
}
}
}