forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutableContext.java
156 lines (126 loc) · 4.67 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
package dev.openfeature.sdk;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Delegate;
/**
* 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 {
@Setter() @Getter private String targetingKey;
@Delegate(excludes = HideDelegateAddMethods.class) private final MutableStructure structure;
public MutableContext() {
this.structure = new MutableStructure();
this.targetingKey = "";
}
public MutableContext(String targetingKey) {
this();
this.targetingKey = targetingKey;
}
public MutableContext(Map<String, Value> attributes) {
this.structure = new MutableStructure(attributes);
this.targetingKey = "";
}
public MutableContext(String targetingKey, Map<String, Value> attributes) {
this(attributes);
this.targetingKey = targetingKey;
}
// 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;
}
/**
* Merges this EvaluationContext objects with the second overriding the this 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.targetingKey, this.asMap());
}
Map<String, Value> merged = this.merge(map -> new MutableStructure(map),
this.asMap(),
overridingContext.asMap());
String newTargetingKey = "";
if (this.getTargetingKey() != null && !this.getTargetingKey().trim().equals("")) {
newTargetingKey = this.getTargetingKey();
}
if (overridingContext.getTargetingKey() != null && !overridingContext.getTargetingKey().trim().equals("")) {
newTargetingKey = overridingContext.getTargetingKey();
}
EvaluationContext ec = null;
if (newTargetingKey != null && !newTargetingKey.trim().equals("")) {
ec = new MutableContext(newTargetingKey, merged);
} else {
ec = new MutableContext(merged);
}
return ec;
}
/**
* 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, MutableStructure ignoredValue) {
return null;
}
public MutableStructure add(String ignoredKey, Instant ignoredValue) {
return null;
}
}
}