11package dev .openfeature .javasdk ;
22
3- import com . fasterxml . jackson . databind . ObjectMapper ;
3+ import dev . openfeature . javasdk . internal . Pair ;
44import lombok .*;
55
66import java .time .ZonedDateTime ;
1111@ ToString @ EqualsAndHashCode
1212@ SuppressWarnings ("PMD.BeanMembersShouldSerialize" )
1313public class EvaluationContext {
14- @ EqualsAndHashCode .Exclude private final ObjectMapper objMapper ;
1514 @ 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 ;
2016
2117 public EvaluationContext () {
22- objMapper = new ObjectMapper ();
2318 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 <>();
2820 }
2921
3022 // TODO Not sure if I should have sneakythrows or checked exceptions here..
3123 @ SneakyThrows
3224 public <T > void addStructureAttribute (String key , T value ) {
33- jsonAttributes .put (key , objMapper . writeValueAsString ( value ));
25+ attributes .put (key , new Pair <>( FlagValueType . OBJECT , value ));
3426 }
3527
3628 @ 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 );
4031 }
4132
4233 public Map <String , String > getStructureAttributes () {
43- return new HashMap <>( jsonAttributes );
34+ return getAttributesByType ( FlagValueType . OBJECT );
4435 }
4536
4637 public void addStringAttribute (String key , String value ) {
47- stringAttributes .put (key , value );
38+ attributes .put (key , new Pair <>( FlagValueType . STRING , value ) );
4839 }
4940
5041 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 ;
5262 }
5363
5464 public Map <String , String > getStringAttributes () {
55- return new HashMap <>( stringAttributes );
65+ return getAttributesByType ( FlagValueType . STRING );
5666 }
5767
5868 public void addIntegerAttribute (String key , Integer value ) {
59- integerAttributes .put (key , value );
69+ attributes .put (key , new Pair <>( FlagValueType . INTEGER , value ) );
6070 }
6171
6272 public Integer getIntegerAttribute (String key ) {
63- return integerAttributes . get (key );
73+ return getAttributeByType (key , FlagValueType . INTEGER );
6474 }
6575
6676 public Map <String , Integer > getIntegerAttributes () {
67- return new HashMap <>( integerAttributes );
77+ return getAttributesByType ( FlagValueType . INTEGER );
6878 }
6979
7080 public Boolean getBooleanAttribute (String key ) {
71- return booleanAttributes . get (key );
81+ return getAttributeByType (key , FlagValueType . BOOLEAN );
7282 }
7383
7484 public void addBooleanAttribute (String key , Boolean b ) {
75- booleanAttributes .put (key , b );
85+ attributes .put (key , new Pair <>( FlagValueType . BOOLEAN , b ) );
7686 }
7787
7888 public Map <String , Boolean > getBooleanAttributes () {
79- return new HashMap <>( booleanAttributes );
89+ return getAttributesByType ( FlagValueType . BOOLEAN );
8090 }
8191
8292 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 ) ));
8494 }
8595
8696 public ZonedDateTime getDatetimeAttribute (String key ) {
87- String attr = this . stringAttributes . get (key );
97+ String attr = getAttributeByType (key , FlagValueType . STRING );
8898 if (attr == null ) {
8999 return null ;
90100 }
@@ -96,18 +106,8 @@ public ZonedDateTime getDatetimeAttribute(String key) {
96106 */
97107 public static EvaluationContext merge (EvaluationContext ctx1 , EvaluationContext ctx2 ) {
98108 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 );
111111
112112 if (ctx1 .getTargetingKey () != null ) {
113113 ec .setTargetingKey (ctx1 .getTargetingKey ());
0 commit comments