forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutableContext.java
158 lines (130 loc) · 4.64 KB
/
MutableContext.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package dev.openfeature.sdk;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Delegate;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The EvaluationContext is a container for arbitrary contextual data
* that can be used as a basis for dynamic evaluation.
* The MutableContext is an EvaluationContext implementation which is not threadsafe, and whose attributes can
* be modified after instantiation.
*/
@ToString
@EqualsAndHashCode
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
public class MutableContext implements EvaluationContext {
@Delegate(excludes = HideDelegateAddMethods.class) private final MutableStructure structure;
public MutableContext() {
this(new HashMap<>());
}
public MutableContext(String targetingKey) {
this(targetingKey, new HashMap<>());
}
public MutableContext(Map<String, Value> attributes) {
this("", attributes);
}
/**
* Create a mutable context with given targetingKey and attributes provided. TargetingKey should be non-null
* and non-empty to be accepted.
*
* @param targetingKey targeting key
* @param attributes evaluation context attributes
*/
public MutableContext(String targetingKey, Map<String, Value> attributes) {
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
attributes.put(TARGETING_KEY, new Value(targetingKey));
}
this.structure = new MutableStructure(attributes);
}
// override @Delegate methods so that we can use "add" methods and still return MutableContext, not Structure
public MutableContext add(String key, Boolean value) {
this.structure.add(key, value);
return this;
}
public MutableContext add(String key, String value) {
this.structure.add(key, value);
return this;
}
public MutableContext add(String key, Integer value) {
this.structure.add(key, value);
return this;
}
public MutableContext add(String key, Double value) {
this.structure.add(key, value);
return this;
}
public MutableContext add(String key, Instant value) {
this.structure.add(key, value);
return this;
}
public MutableContext add(String key, Structure value) {
this.structure.add(key, value);
return this;
}
public MutableContext add(String key, List<Value> value) {
this.structure.add(key, value);
return this;
}
/**
* Override or set targeting key for this mutable context. Value should be non-null and non-empty to be accepted.
*/
public void setTargetingKey(String targetingKey) {
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
this.add(TARGETING_KEY, targetingKey);
}
}
/**
* Retrieve targetingKey from the context.
*/
@Override
public String getTargetingKey() {
return this.getValue(TARGETING_KEY).asString();
}
/**
* Merges this EvaluationContext objects with the second overriding the in case of conflict.
*
* @param overridingContext overriding context
* @return resulting merged context
*/
@Override
public EvaluationContext merge(EvaluationContext overridingContext) {
if (overridingContext == null) {
return new MutableContext(this.asMap());
}
Map<String, Value> merged = this.merge(
MutableStructure::new, this.asMap(), overridingContext.asMap());
return new MutableContext(merged);
}
/**
* Hidden class to tell Lombok not to copy these methods over via delegation.
*/
private static class HideDelegateAddMethods {
public MutableStructure add(String ignoredKey, Boolean ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, Double ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, String ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, Value ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, Integer ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, List<Value> ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, Structure ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, Instant ignoredValue) {
return null;
}
}
}