1
1
package dev .openfeature .javasdk ;
2
2
3
+ import lombok .EqualsAndHashCode ;
3
4
import lombok .Getter ;
5
+ import lombok .Setter ;
6
+ import lombok .ToString ;
4
7
5
8
import java .time .ZonedDateTime ;
6
9
import java .time .format .DateTimeFormatter ;
7
10
import java .util .HashMap ;
8
11
import java .util .Map ;
9
12
13
+ @ ToString @ EqualsAndHashCode
10
14
public class EvaluationContext {
11
- @ Getter private final String targetingKey ;
15
+ @ Setter @ Getter private String targetingKey ;
12
16
private final Map <String , Integer > integerAttributes ;
13
17
private final Map <String , String > stringAttributes ;
14
18
15
-
16
- private enum KNOWN_KEYS {
17
- EMAIL ,
18
- FIRST_NAME ,
19
- LAST_NAME ,
20
- NAME ,
21
- IP ,
22
- TZ ,
23
- LOCALE ,
24
- COUNTRY_CODE ,
25
- ENVIRONMENT ,
26
- APPLICATION ,
27
- VERSION ,
28
- TIMESTAMP ,
29
- }
30
-
31
19
EvaluationContext () {
32
20
this .targetingKey = "" ;
33
21
this .integerAttributes = new HashMap <>();
@@ -62,6 +50,8 @@ public void addDatetimeAttribute(String key, ZonedDateTime value) {
62
50
this .stringAttributes .put (key , value .format (DateTimeFormatter .ISO_ZONED_DATE_TIME ));
63
51
}
64
52
53
+ // TODO: addStructure or similar.
54
+
65
55
public ZonedDateTime getDatetimeAttribute (String key ) {
66
56
String attr = this .stringAttributes .get (key );
67
57
if (attr == null ) {
@@ -70,107 +60,34 @@ public ZonedDateTime getDatetimeAttribute(String key) {
70
60
return ZonedDateTime .parse (attr , DateTimeFormatter .ISO_ZONED_DATE_TIME );
71
61
}
72
62
73
- public String getEmail () {
74
- return this .stringAttributes .get (KNOWN_KEYS .EMAIL .toString ());
75
- }
76
-
77
- public String getFirstName () {
78
- return this .stringAttributes .get (KNOWN_KEYS .FIRST_NAME .toString ());
79
- }
80
-
81
- public String getLastName () {
82
- return this .stringAttributes .get (KNOWN_KEYS .LAST_NAME .toString ());
83
- }
84
-
85
- public String getName () {
86
- return this .stringAttributes .get (KNOWN_KEYS .NAME .toString ());
87
- }
88
-
89
- public String getIp () {
90
- return this .stringAttributes .get (KNOWN_KEYS .IP .toString ());
91
- }
92
-
93
- public String getTz () {
94
- return this .stringAttributes .get (KNOWN_KEYS .TZ .toString ());
95
- }
96
-
97
- public String getLocale () {
98
- return this .stringAttributes .get (KNOWN_KEYS .LOCALE .toString ());
99
- }
100
-
101
- public String getCountryCode () {
102
- return this .stringAttributes .get (KNOWN_KEYS .COUNTRY_CODE .toString ());
103
- }
104
-
105
- public String getEnvironment () {
106
- return this .stringAttributes .get (KNOWN_KEYS .ENVIRONMENT .toString ());
107
- }
108
-
109
- public String getApplication () {
110
- return this .stringAttributes .get (KNOWN_KEYS .APPLICATION .toString ());
111
- }
112
-
113
- public String getVersion () {
114
- return this .stringAttributes .get (KNOWN_KEYS .VERSION .toString ());
115
- }
116
-
117
- public ZonedDateTime getTimestamp () {
118
- return getDatetimeAttribute (KNOWN_KEYS .TIMESTAMP .toString ());
119
- }
120
-
121
- public void setEmail (String email ) {
122
- this .stringAttributes .put (KNOWN_KEYS .EMAIL .toString (), email );
123
- }
124
-
125
- public void setFirstName (String firstname ) {
126
- this .stringAttributes .put (KNOWN_KEYS .FIRST_NAME .toString (), firstname );
127
- }
128
-
129
- public void setLastName (String lastname ) {
130
- this .stringAttributes .put (KNOWN_KEYS .LAST_NAME .toString (), lastname );
131
- }
132
-
133
- public void setName (String name ) {
134
- this .stringAttributes .put (KNOWN_KEYS .NAME .toString (), name );
135
- }
136
-
137
- public void setIp (String ip ) {
138
- this .stringAttributes .put (KNOWN_KEYS .IP .toString (), ip );
139
- }
140
-
141
- public void setTz (String tz ) {
142
- this .stringAttributes .put (KNOWN_KEYS .TZ .toString (), tz );
143
- }
144
-
145
- public void setLocale (String locale ) {
146
- this .stringAttributes .put (KNOWN_KEYS .LOCALE .toString (), locale );
147
- }
148
-
149
- public void setCountryCode (String countryCode ) {
150
- this .stringAttributes .put (KNOWN_KEYS .COUNTRY_CODE .toString (), countryCode );
151
- }
63
+ /**
64
+ * Merges two EvaluationContext objects with the second overriding the first in case of conflict.
65
+ */
66
+ public static EvaluationContext merge (EvaluationContext ctx1 , EvaluationContext ctx2 ) {
67
+ EvaluationContext ec = new EvaluationContext ();
68
+ for (Map .Entry <String , Integer > e : ctx1 .integerAttributes .entrySet ()) {
69
+ ec .addIntegerAttribute (e .getKey (), e .getValue ());
70
+ }
152
71
153
- public void setEnvironment ( String environment ) {
154
- this . stringAttributes . put ( KNOWN_KEYS . ENVIRONMENT . toString (), environment );
155
- }
72
+ for ( Map . Entry < String , Integer > e : ctx2 . integerAttributes . entrySet () ) {
73
+ ec . addIntegerAttribute ( e . getKey (), e . getValue () );
74
+ }
156
75
157
- public void setApplication ( String application ) {
158
- this . stringAttributes . put ( KNOWN_KEYS . APPLICATION . toString (), application );
159
- }
76
+ for ( Map . Entry < String , String > e : ctx1 . stringAttributes . entrySet () ) {
77
+ ec . addStringAttribute ( e . getKey (), e . getValue () );
78
+ }
160
79
161
- public void setVersion (String version ) {
162
- this .stringAttributes .put (KNOWN_KEYS .VERSION .toString (), version );
163
- }
80
+ for (Map .Entry <String , String > e : ctx2 .stringAttributes .entrySet ()) {
81
+ ec .addStringAttribute (e .getKey (), e .getValue ());
82
+ }
83
+ if (ctx1 .getTargetingKey () != null ) {
84
+ ec .setTargetingKey (ctx1 .getTargetingKey ());
85
+ }
164
86
165
- public void setTimestamp ( ZonedDateTime timestamp ) {
166
- addDatetimeAttribute ( KNOWN_KEYS . TIMESTAMP . toString (), timestamp );
167
- }
87
+ if ( ctx2 . getTargetingKey () != null ) {
88
+ ec . setTargetingKey ( ctx2 . getTargetingKey () );
89
+ }
168
90
169
- /**
170
- * Merges two EvaluationContext objects with the second overriding the first in case of conflict.
171
- */
172
- public static EvaluationContext merge (EvaluationContext ctx1 , EvaluationContext ctx2 ) {
173
- // TODO(abrahms): Actually implement this when we know what the fields of EC are.
174
- return ctx1 ;
91
+ return ec ;
175
92
}
176
93
}
0 commit comments