3
3
import java .util .HashMap ;
4
4
import java .util .Map ;
5
5
import java .util .function .Function ;
6
+
6
7
import dev .openfeature .sdk .internal .ExcludeFromGeneratedCoverageReport ;
7
8
import lombok .ToString ;
8
9
import lombok .experimental .Delegate ;
9
10
10
11
/**
11
12
* The EvaluationContext is a container for arbitrary contextual data
12
13
* that can be used as a basis for dynamic evaluation.
13
- * The ImmutableContext is an EvaluationContext implementation which is threadsafe, and whose attributes can
14
+ * The ImmutableContext is an EvaluationContext implementation which is
15
+ * threadsafe, and whose attributes can
14
16
* not be modified after instantiation.
15
17
*/
16
18
@ ToString
@@ -21,7 +23,8 @@ public final class ImmutableContext implements EvaluationContext {
21
23
private final ImmutableStructure structure ;
22
24
23
25
/**
24
- * Create an immutable context with an empty targeting_key and attributes provided.
26
+ * Create an immutable context with an empty targeting_key and attributes
27
+ * provided.
25
28
*/
26
29
public ImmutableContext () {
27
30
this (new HashMap <>());
@@ -42,7 +45,7 @@ public ImmutableContext(String targetingKey) {
42
45
* @param attributes evaluation context attributes
43
46
*/
44
47
public ImmutableContext (Map <String , Value > attributes ) {
45
- this ("" , attributes );
48
+ this (null , attributes );
46
49
}
47
50
48
51
/**
@@ -53,9 +56,7 @@ public ImmutableContext(Map<String, Value> attributes) {
53
56
*/
54
57
public ImmutableContext (String targetingKey , Map <String , Value > attributes ) {
55
58
if (targetingKey != null && !targetingKey .trim ().isEmpty ()) {
56
- final Map <String , Value > actualAttribs = new HashMap <>(attributes );
57
- actualAttribs .put (TARGETING_KEY , new Value (targetingKey ));
58
- this .structure = new ImmutableStructure (actualAttribs );
59
+ this .structure = new ImmutableStructure (targetingKey , attributes );
59
60
} else {
60
61
this .structure = new ImmutableStructure (attributes );
61
62
}
@@ -71,31 +72,33 @@ public String getTargetingKey() {
71
72
}
72
73
73
74
/**
74
- * Merges this EvaluationContext object with the passed EvaluationContext, overriding in case of conflict.
75
+ * Merges this EvaluationContext object with the passed EvaluationContext,
76
+ * overriding in case of conflict.
75
77
*
76
78
* @param overridingContext overriding context
77
79
* @return new, resulting merged context
78
80
*/
79
81
@ Override
80
82
public EvaluationContext merge (EvaluationContext overridingContext ) {
81
83
if (overridingContext == null || overridingContext .isEmpty ()) {
82
- return new ImmutableContext (this .asMap ());
84
+ return new ImmutableContext (this .asUnmodifiableMap ());
83
85
}
84
86
if (this .isEmpty ()) {
85
- return new ImmutableContext (overridingContext .asMap ());
87
+ return new ImmutableContext (overridingContext .asUnmodifiableMap ());
86
88
}
87
89
88
- return new ImmutableContext (
89
- this .merge (ImmutableStructure ::new , this .asMap (), overridingContext .asMap ()));
90
+ Map <String , Value > attributes = this .asMap ();
91
+ EvaluationContext .mergeMaps (ImmutableStructure ::new , attributes ,
92
+ overridingContext .asUnmodifiableMap ());
93
+ return new ImmutableContext (attributes );
90
94
}
91
95
92
96
@ SuppressWarnings ("all" )
93
97
private static class DelegateExclusions {
94
98
@ ExcludeFromGeneratedCoverageReport
95
- public <T extends Structure > Map <String , Value > merge (Function <Map <String , Value >, Structure > newStructure ,
99
+ public <T extends Structure > Map <String , Value > merge (Function <Map <String , Value >, Structure > newStructure ,
96
100
Map <String , Value > base ,
97
101
Map <String , Value > overriding ) {
98
-
99
102
return null ;
100
103
}
101
104
}
0 commit comments