1
1
package dev .openfeature .sdk ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .Arrays ;
5
- import java .util .Collections ;
6
- import java .util .HashMap ;
7
- import java .util .Map ;
8
- import java .util .List ;
9
- import java .util .function .Consumer ;
10
-
11
3
import dev .openfeature .sdk .exceptions .ExceptionUtils ;
12
4
import dev .openfeature .sdk .exceptions .FatalError ;
13
5
import dev .openfeature .sdk .exceptions .GeneralError ;
19
11
import lombok .Getter ;
20
12
import lombok .extern .slf4j .Slf4j ;
21
13
14
+ import javax .annotation .Nullable ;
15
+ import java .util .ArrayList ;
16
+ import java .util .Arrays ;
17
+ import java .util .Collections ;
18
+ import java .util .HashMap ;
19
+ import java .util .List ;
20
+ import java .util .Map ;
21
+ import java .util .Objects ;
22
+ import java .util .function .Consumer ;
23
+
22
24
/**
23
25
* OpenFeature Client implementation.
24
26
* You should not instantiate this or reference this class.
28
30
* @deprecated // TODO: eventually we will make this non-public. See issue #872
29
31
*/
30
32
@ Slf4j
31
- @ SuppressWarnings ({ "PMD.DataflowAnomalyAnalysis" , "PMD.BeanMembersShouldSerialize" , "PMD.UnusedLocalVariable" ,
32
- "unchecked" , "rawtypes" })
33
+ @ SuppressWarnings ({"PMD.DataflowAnomalyAnalysis" , "PMD.BeanMembersShouldSerialize" , "PMD.UnusedLocalVariable" ,
34
+ "unchecked" , "rawtypes" })
33
35
@ Deprecated () // TODO: eventually we will make this non-public. See issue #872
34
36
public class OpenFeatureClient implements Client {
35
37
@@ -67,11 +69,56 @@ public OpenFeatureClient(
67
69
this .hookSupport = new HookSupport ();
68
70
}
69
71
72
+ /**
73
+ * {@inheritDoc}
74
+ */
70
75
@ Override
71
76
public ProviderState getProviderState () {
72
77
return openfeatureApi .getFeatureProviderStateManager (domain ).getState ();
73
78
}
74
79
80
+ /**
81
+ * {@inheritDoc}
82
+ */
83
+ @ Override
84
+ public void track (String trackingEventName ) {
85
+ Objects .requireNonNull (trackingEventName );
86
+ invokeTrack (trackingEventName , null , null );
87
+ }
88
+
89
+
90
+ /**
91
+ * {@inheritDoc}
92
+ */
93
+ @ Override
94
+ public void track (String trackingEventName , EvaluationContext context ) {
95
+ Objects .requireNonNull (trackingEventName );
96
+ Objects .requireNonNull (context );
97
+ invokeTrack (trackingEventName , context , null );
98
+ }
99
+
100
+ /**
101
+ * {@inheritDoc}
102
+ */
103
+ @ Override
104
+ public void track (String trackingEventName , TrackingEventDetails details ) {
105
+ Objects .requireNonNull (trackingEventName );
106
+ Objects .requireNonNull (details );
107
+ invokeTrack (trackingEventName , null , details );
108
+ }
109
+
110
+ /**
111
+ * {@inheritDoc}
112
+ */
113
+ @ Override
114
+ public void track (String trackingEventName , EvaluationContext context , TrackingEventDetails details ) {
115
+ Objects .requireNonNull (trackingEventName );
116
+ Objects .requireNonNull (context );
117
+ Objects .requireNonNull (details );
118
+ invokeTrack (trackingEventName , mergeEvaluationContext (context ), details );
119
+ }
120
+
121
+
75
122
/**
76
123
* {@inheritDoc}
77
124
*/
@@ -115,7 +162,7 @@ public EvaluationContext getEvaluationContext() {
115
162
}
116
163
117
164
private <T > FlagEvaluationDetails <T > evaluateFlag (FlagValueType type , String key , T defaultValue ,
118
- EvaluationContext ctx , FlagEvaluationOptions options ) {
165
+ EvaluationContext ctx , FlagEvaluationOptions options ) {
119
166
FlagEvaluationOptions flagOptions = ObjectUtils .defaultIfNull (options ,
120
167
() -> FlagEvaluationOptions .builder ().build ());
121
168
Map <String , Object > hints = Collections .unmodifiableMap (flagOptions .getHookHints ());
@@ -183,6 +230,17 @@ private static <T> void enrichDetailsWithErrorDefaults(T defaultValue, FlagEvalu
183
230
details .setReason (Reason .ERROR .toString ());
184
231
}
185
232
233
+ private void invokeTrack (String trackingEventName ,
234
+ @ Nullable EvaluationContext context ,
235
+ @ Nullable TrackingEventDetails details ) {
236
+ if ("" .equals (trackingEventName )) {
237
+ throw new IllegalArgumentException ("trackingEventName cannot be empty" );
238
+ }
239
+ openfeatureApi .getFeatureProviderStateManager (domain )
240
+ .getProvider ()
241
+ .track (trackingEventName , mergeEvaluationContext (context ), details );
242
+ }
243
+
186
244
/**
187
245
* Merge invocation contexts with API, transaction and client contexts.
188
246
* Does not merge before context.
@@ -244,7 +302,7 @@ public Boolean getBooleanValue(String key, Boolean defaultValue, EvaluationConte
244
302
245
303
@ Override
246
304
public Boolean getBooleanValue (String key , Boolean defaultValue , EvaluationContext ctx ,
247
- FlagEvaluationOptions options ) {
305
+ FlagEvaluationOptions options ) {
248
306
return getBooleanDetails (key , defaultValue , ctx , options ).getValue ();
249
307
}
250
308
@@ -260,7 +318,7 @@ public FlagEvaluationDetails<Boolean> getBooleanDetails(String key, Boolean defa
260
318
261
319
@ Override
262
320
public FlagEvaluationDetails <Boolean > getBooleanDetails (String key , Boolean defaultValue , EvaluationContext ctx ,
263
- FlagEvaluationOptions options ) {
321
+ FlagEvaluationOptions options ) {
264
322
return this .evaluateFlag (FlagValueType .BOOLEAN , key , defaultValue , ctx , options );
265
323
}
266
324
@@ -276,7 +334,7 @@ public String getStringValue(String key, String defaultValue, EvaluationContext
276
334
277
335
@ Override
278
336
public String getStringValue (String key , String defaultValue , EvaluationContext ctx ,
279
- FlagEvaluationOptions options ) {
337
+ FlagEvaluationOptions options ) {
280
338
return getStringDetails (key , defaultValue , ctx , options ).getValue ();
281
339
}
282
340
@@ -292,7 +350,7 @@ public FlagEvaluationDetails<String> getStringDetails(String key, String default
292
350
293
351
@ Override
294
352
public FlagEvaluationDetails <String > getStringDetails (String key , String defaultValue , EvaluationContext ctx ,
295
- FlagEvaluationOptions options ) {
353
+ FlagEvaluationOptions options ) {
296
354
return this .evaluateFlag (FlagValueType .STRING , key , defaultValue , ctx , options );
297
355
}
298
356
@@ -308,7 +366,7 @@ public Integer getIntegerValue(String key, Integer defaultValue, EvaluationConte
308
366
309
367
@ Override
310
368
public Integer getIntegerValue (String key , Integer defaultValue , EvaluationContext ctx ,
311
- FlagEvaluationOptions options ) {
369
+ FlagEvaluationOptions options ) {
312
370
return getIntegerDetails (key , defaultValue , ctx , options ).getValue ();
313
371
}
314
372
@@ -324,7 +382,7 @@ public FlagEvaluationDetails<Integer> getIntegerDetails(String key, Integer defa
324
382
325
383
@ Override
326
384
public FlagEvaluationDetails <Integer > getIntegerDetails (String key , Integer defaultValue , EvaluationContext ctx ,
327
- FlagEvaluationOptions options ) {
385
+ FlagEvaluationOptions options ) {
328
386
return this .evaluateFlag (FlagValueType .INTEGER , key , defaultValue , ctx , options );
329
387
}
330
388
@@ -340,7 +398,7 @@ public Double getDoubleValue(String key, Double defaultValue, EvaluationContext
340
398
341
399
@ Override
342
400
public Double getDoubleValue (String key , Double defaultValue , EvaluationContext ctx ,
343
- FlagEvaluationOptions options ) {
401
+ FlagEvaluationOptions options ) {
344
402
return this .evaluateFlag (FlagValueType .DOUBLE , key , defaultValue , ctx , options ).getValue ();
345
403
}
346
404
@@ -356,7 +414,7 @@ public FlagEvaluationDetails<Double> getDoubleDetails(String key, Double default
356
414
357
415
@ Override
358
416
public FlagEvaluationDetails <Double > getDoubleDetails (String key , Double defaultValue , EvaluationContext ctx ,
359
- FlagEvaluationOptions options ) {
417
+ FlagEvaluationOptions options ) {
360
418
return this .evaluateFlag (FlagValueType .DOUBLE , key , defaultValue , ctx , options );
361
419
}
362
420
@@ -372,7 +430,7 @@ public Value getObjectValue(String key, Value defaultValue, EvaluationContext ct
372
430
373
431
@ Override
374
432
public Value getObjectValue (String key , Value defaultValue , EvaluationContext ctx ,
375
- FlagEvaluationOptions options ) {
433
+ FlagEvaluationOptions options ) {
376
434
return getObjectDetails (key , defaultValue , ctx , options ).getValue ();
377
435
}
378
436
@@ -383,13 +441,13 @@ public FlagEvaluationDetails<Value> getObjectDetails(String key, Value defaultVa
383
441
384
442
@ Override
385
443
public FlagEvaluationDetails <Value > getObjectDetails (String key , Value defaultValue ,
386
- EvaluationContext ctx ) {
444
+ EvaluationContext ctx ) {
387
445
return getObjectDetails (key , defaultValue , ctx , FlagEvaluationOptions .builder ().build ());
388
446
}
389
447
390
448
@ Override
391
449
public FlagEvaluationDetails <Value > getObjectDetails (String key , Value defaultValue , EvaluationContext ctx ,
392
- FlagEvaluationOptions options ) {
450
+ FlagEvaluationOptions options ) {
393
451
return this .evaluateFlag (FlagValueType .OBJECT , key , defaultValue , ctx , options );
394
452
}
395
453
0 commit comments