1
1
package dev .openfeature .javasdk ;
2
2
3
- import com . fasterxml . jackson . databind . ObjectMapper ;
3
+ import dev . openfeature . javasdk . internal . Pair ;
4
4
import lombok .*;
5
5
6
6
import java .time .ZonedDateTime ;
11
11
@ ToString @ EqualsAndHashCode
12
12
@ SuppressWarnings ("PMD.BeanMembersShouldSerialize" )
13
13
public class EvaluationContext {
14
- @ EqualsAndHashCode .Exclude private final ObjectMapper objMapper ;
15
14
@ Setter @ Getter private String targetingKey ;
16
- private final Map <String , Integer > integerAttributes ;
17
- private final Map <String , String > stringAttributes ;
18
- private final Map <String , Boolean > booleanAttributes ;
19
- final Map <String , String > jsonAttributes ;
15
+ private final Map <String , dev .openfeature .javasdk .internal .Pair <FlagValueType , Object >> attributes ;
20
16
21
17
public EvaluationContext () {
22
- objMapper = new ObjectMapper ();
23
18
this .targetingKey = "" ;
24
- this .integerAttributes = new HashMap <>();
25
- this .stringAttributes = new HashMap <>();
26
- booleanAttributes = new HashMap <>();
27
- jsonAttributes = new HashMap <>();
19
+ this .attributes = new HashMap <>();
28
20
}
29
21
30
22
// TODO Not sure if I should have sneakythrows or checked exceptions here..
31
23
@ SneakyThrows
32
24
public <T > void addStructureAttribute (String key , T value ) {
33
- jsonAttributes .put (key , objMapper . writeValueAsString ( value ));
25
+ attributes .put (key , new Pair <>( FlagValueType . OBJECT , value ));
34
26
}
35
27
36
28
@ SneakyThrows
37
- public <T > T getStructureAttribute (String key , Class <T > klass ) {
38
- String val = jsonAttributes .get (key );
39
- return objMapper .readValue (val , klass );
29
+ public <T > T getStructureAttribute (String key ) {
30
+ return getAttributeByType (key , FlagValueType .OBJECT );
40
31
}
41
32
42
33
public Map <String , String > getStructureAttributes () {
43
- return new HashMap <>( jsonAttributes );
34
+ return getAttributesByType ( FlagValueType . OBJECT );
44
35
}
45
36
46
37
public void addStringAttribute (String key , String value ) {
47
- stringAttributes .put (key , value );
38
+ attributes .put (key , new Pair <>( FlagValueType . STRING , value ) );
48
39
}
49
40
50
41
public String getStringAttribute (String key ) {
51
- return stringAttributes .get (key );
42
+ return getAttributeByType (key , FlagValueType .STRING );
43
+ }
44
+
45
+ private <T > Map <String , T > getAttributesByType (FlagValueType type ) {
46
+ HashMap <String , T > hm = new HashMap <>();
47
+ for (Map .Entry <String , Pair <FlagValueType , Object >> entry : attributes .entrySet ()) {
48
+ String key = entry .getKey ();
49
+ if (entry .getValue ().getFirst () == type ) {
50
+ hm .put (key , (T ) entry .getValue ().getSecond ());
51
+ }
52
+ }
53
+ return hm ;
54
+ }
55
+
56
+ private <T > T getAttributeByType (String key , FlagValueType type ) {
57
+ Pair <FlagValueType , Object > val = attributes .get (key );
58
+ if (val .getFirst () == type ) {
59
+ return (T ) val .getSecond ();
60
+ }
61
+ return null ;
52
62
}
53
63
54
64
public Map <String , String > getStringAttributes () {
55
- return new HashMap <>( stringAttributes );
65
+ return getAttributesByType ( FlagValueType . STRING );
56
66
}
57
67
58
68
public void addIntegerAttribute (String key , Integer value ) {
59
- integerAttributes .put (key , value );
69
+ attributes .put (key , new Pair <>( FlagValueType . INTEGER , value ) );
60
70
}
61
71
62
72
public Integer getIntegerAttribute (String key ) {
63
- return integerAttributes . get (key );
73
+ return getAttributeByType (key , FlagValueType . INTEGER );
64
74
}
65
75
66
76
public Map <String , Integer > getIntegerAttributes () {
67
- return new HashMap <>( integerAttributes );
77
+ return getAttributesByType ( FlagValueType . INTEGER );
68
78
}
69
79
70
80
public Boolean getBooleanAttribute (String key ) {
71
- return booleanAttributes . get (key );
81
+ return getAttributeByType (key , FlagValueType . BOOLEAN );
72
82
}
73
83
74
84
public void addBooleanAttribute (String key , Boolean b ) {
75
- booleanAttributes .put (key , b );
85
+ attributes .put (key , new Pair <>( FlagValueType . BOOLEAN , b ) );
76
86
}
77
87
78
88
public Map <String , Boolean > getBooleanAttributes () {
79
- return new HashMap <>( booleanAttributes );
89
+ return getAttributesByType ( FlagValueType . BOOLEAN );
80
90
}
81
91
82
92
public void addDatetimeAttribute (String key , ZonedDateTime value ) {
83
- this . stringAttributes . put (key , value .format (DateTimeFormatter .ISO_ZONED_DATE_TIME ));
93
+ attributes . put (key , new Pair <>( FlagValueType . STRING , value .format (DateTimeFormatter .ISO_ZONED_DATE_TIME ) ));
84
94
}
85
95
86
96
public ZonedDateTime getDatetimeAttribute (String key ) {
87
- String attr = this . stringAttributes . get (key );
97
+ String attr = getAttributeByType (key , FlagValueType . STRING );
88
98
if (attr == null ) {
89
99
return null ;
90
100
}
@@ -96,18 +106,8 @@ public ZonedDateTime getDatetimeAttribute(String key) {
96
106
*/
97
107
public static EvaluationContext merge (EvaluationContext ctx1 , EvaluationContext ctx2 ) {
98
108
EvaluationContext ec = new EvaluationContext ();
99
-
100
- ec .stringAttributes .putAll (ctx1 .stringAttributes );
101
- ec .stringAttributes .putAll (ctx2 .stringAttributes );
102
-
103
- ec .integerAttributes .putAll (ctx1 .integerAttributes );
104
- ec .integerAttributes .putAll (ctx2 .integerAttributes );
105
-
106
- ec .booleanAttributes .putAll (ctx1 .booleanAttributes );
107
- ec .booleanAttributes .putAll (ctx2 .booleanAttributes );
108
-
109
- ec .jsonAttributes .putAll (ctx1 .jsonAttributes );
110
- ec .jsonAttributes .putAll (ctx2 .jsonAttributes );
109
+ ec .attributes .putAll (ctx1 .attributes );
110
+ ec .attributes .putAll (ctx2 .attributes );
111
111
112
112
if (ctx1 .getTargetingKey () != null ) {
113
113
ec .setTargetingKey (ctx1 .getTargetingKey ());
0 commit comments