|
1 | 1 | package dev.openfeature.sdk;
|
2 | 2 |
|
3 |
| -import java.time.Instant; |
4 |
| -import java.util.List; |
5 |
| -import java.util.Map; |
6 |
| - |
7 | 3 | import lombok.EqualsAndHashCode;
|
8 |
| -import lombok.Getter; |
9 |
| -import lombok.Setter; |
10 | 4 | import lombok.ToString;
|
11 | 5 | import lombok.experimental.Delegate;
|
12 | 6 |
|
| 7 | +import java.time.Instant; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
13 | 12 | /**
|
14 | 13 | * The EvaluationContext is a container for arbitrary contextual data
|
15 | 14 | * that can be used as a basis for dynamic evaluation.
|
16 |
| - * The MutableContext is an EvaluationContext implementation which is not threadsafe, and whose attributes can |
| 15 | + * The MutableContext is an EvaluationContext implementation which is not threadsafe, and whose attributes can |
17 | 16 | * be modified after instantiation.
|
18 | 17 | */
|
19 | 18 | @ToString
|
20 | 19 | @EqualsAndHashCode
|
21 | 20 | @SuppressWarnings("PMD.BeanMembersShouldSerialize")
|
22 | 21 | public class MutableContext implements EvaluationContext {
|
23 | 22 |
|
24 |
| - @Setter() @Getter private String targetingKey; |
25 | 23 | @Delegate(excludes = HideDelegateAddMethods.class) private final MutableStructure structure;
|
26 | 24 |
|
27 | 25 | public MutableContext() {
|
28 |
| - this.structure = new MutableStructure(); |
29 |
| - this.targetingKey = ""; |
| 26 | + this(new HashMap<>()); |
30 | 27 | }
|
31 | 28 |
|
32 | 29 | public MutableContext(String targetingKey) {
|
33 |
| - this(); |
34 |
| - this.targetingKey = targetingKey; |
| 30 | + this(targetingKey, new HashMap<>()); |
35 | 31 | }
|
36 | 32 |
|
37 | 33 | public MutableContext(Map<String, Value> attributes) {
|
38 |
| - this.structure = new MutableStructure(attributes); |
39 |
| - this.targetingKey = ""; |
| 34 | + this("", attributes); |
40 | 35 | }
|
41 | 36 |
|
| 37 | + /** |
| 38 | + * Create a mutable context with given targetingKey and attributes provided. TargetingKey should be non-null |
| 39 | + * and non-empty to be accepted. |
| 40 | + * |
| 41 | + * @param targetingKey targeting key |
| 42 | + * @param attributes evaluation context attributes |
| 43 | + */ |
42 | 44 | public MutableContext(String targetingKey, Map<String, Value> attributes) {
|
43 |
| - this(attributes); |
44 |
| - this.targetingKey = targetingKey; |
| 45 | + if (targetingKey != null && !targetingKey.trim().isEmpty()) { |
| 46 | + attributes.put(TARGETING_KEY, new Value(targetingKey)); |
| 47 | + } |
| 48 | + this.structure = new MutableStructure(attributes); |
45 | 49 | }
|
46 | 50 |
|
47 | 51 | // override @Delegate methods so that we can use "add" methods and still return MutableContext, not Structure
|
@@ -81,40 +85,38 @@ public MutableContext add(String key, List<Value> value) {
|
81 | 85 | }
|
82 | 86 |
|
83 | 87 | /**
|
84 |
| - * Merges this EvaluationContext objects with the second overriding the this in |
85 |
| - * case of conflict. |
| 88 | + * Override or set targeting key for this mutable context. Value should be non-null and non-empty to be accepted. |
| 89 | + */ |
| 90 | + public void setTargetingKey(String targetingKey) { |
| 91 | + if (targetingKey != null && !targetingKey.trim().isEmpty()) { |
| 92 | + this.add(TARGETING_KEY, targetingKey); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + /** |
| 98 | + * Retrieve targetingKey from the context. |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public String getTargetingKey() { |
| 102 | + return this.getValue(TARGETING_KEY).asString(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Merges this EvaluationContext objects with the second overriding the in case of conflict. |
86 | 107 | *
|
87 | 108 | * @param overridingContext overriding context
|
88 | 109 | * @return resulting merged context
|
89 | 110 | */
|
90 | 111 | @Override
|
91 | 112 | public EvaluationContext merge(EvaluationContext overridingContext) {
|
92 | 113 | if (overridingContext == null) {
|
93 |
| - return new MutableContext(this.targetingKey, this.asMap()); |
94 |
| - } |
95 |
| - |
96 |
| - Map<String, Value> merged = this.merge(map -> new MutableStructure(map), |
97 |
| - this.asMap(), |
98 |
| - overridingContext.asMap()); |
99 |
| - |
100 |
| - String newTargetingKey = ""; |
101 |
| - |
102 |
| - if (this.getTargetingKey() != null && !this.getTargetingKey().trim().equals("")) { |
103 |
| - newTargetingKey = this.getTargetingKey(); |
104 |
| - } |
105 |
| - |
106 |
| - if (overridingContext.getTargetingKey() != null && !overridingContext.getTargetingKey().trim().equals("")) { |
107 |
| - newTargetingKey = overridingContext.getTargetingKey(); |
108 |
| - } |
109 |
| - |
110 |
| - EvaluationContext ec = null; |
111 |
| - if (newTargetingKey != null && !newTargetingKey.trim().equals("")) { |
112 |
| - ec = new MutableContext(newTargetingKey, merged); |
113 |
| - } else { |
114 |
| - ec = new MutableContext(merged); |
| 114 | + return new MutableContext(this.asMap()); |
115 | 115 | }
|
116 | 116 |
|
117 |
| - return ec; |
| 117 | + Map<String, Value> merged = this.merge( |
| 118 | + MutableStructure::new, this.asMap(), overridingContext.asMap()); |
| 119 | + return new MutableContext(merged); |
118 | 120 | }
|
119 | 121 |
|
120 | 122 | /**
|
|
0 commit comments