18
18
19
19
import com .optimizely .ab .annotations .VisibleForTesting ;
20
20
import com .optimizely .ab .bucketing .Bucketer ;
21
- import com .optimizely .ab .bucketing .UserExperimentRecord ;
21
+ import com .optimizely .ab .bucketing .UserProfile ;
22
22
import com .optimizely .ab .config .Attribute ;
23
23
import com .optimizely .ab .config .EventType ;
24
24
import com .optimizely .ab .config .Experiment ;
39
39
import com .optimizely .ab .event .internal .EventBuilderV2 ;
40
40
import com .optimizely .ab .event .internal .payload .Event .ClientEngine ;
41
41
import com .optimizely .ab .internal .ProjectValidationUtils ;
42
+ import com .optimizely .ab .notification .NotificationListener ;
43
+ import com .optimizely .ab .notification .NotificationBroadcaster ;
42
44
43
45
import org .slf4j .Logger ;
44
46
import org .slf4j .LoggerFactory ;
@@ -89,6 +91,7 @@ public class Optimizely {
89
91
@ VisibleForTesting final ProjectConfig projectConfig ;
90
92
@ VisibleForTesting final EventHandler eventHandler ;
91
93
@ VisibleForTesting final ErrorHandler errorHandler ;
94
+ @ VisibleForTesting final NotificationBroadcaster notificationBroadcaster = new NotificationBroadcaster ();
92
95
93
96
private Optimizely (@ Nonnull ProjectConfig projectConfig ,
94
97
@ Nonnull Bucketer bucketer ,
@@ -104,7 +107,7 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
104
107
105
108
// Do work here that should be done once per Optimizely lifecycle
106
109
@ VisibleForTesting void initialize () {
107
- bucketer .cleanUserExperimentRecords ();
110
+ bucketer .cleanUserProfiles ();
108
111
}
109
112
110
113
//======== activate calls ========//
@@ -180,6 +183,8 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
180
183
logger .error ("Unexpected exception in event dispatcher" , e );
181
184
}
182
185
186
+ notificationBroadcaster .broadcastExperimentActivated (experiment , userId , attributes , variation );
187
+
183
188
return variation ;
184
189
}
185
190
@@ -258,15 +263,15 @@ private void track(@Nonnull String eventName,
258
263
//======== live variable getters ========//
259
264
260
265
public @ Nullable String getVariableString (@ Nonnull String variableKey ,
261
- boolean activateExperiment ,
262
- @ Nonnull String userId ) throws UnknownLiveVariableException {
263
- return getVariableString (variableKey , activateExperiment , userId , Collections .<String , String >emptyMap ());
266
+ @ Nonnull String userId ,
267
+ boolean activateExperiment ) throws UnknownLiveVariableException {
268
+ return getVariableString (variableKey , userId , Collections .<String , String >emptyMap (), activateExperiment );
264
269
}
265
270
266
271
public @ Nullable String getVariableString (@ Nonnull String variableKey ,
267
- boolean activateExperiment ,
268
272
@ Nonnull String userId ,
269
- @ Nonnull Map <String , String > attributes )
273
+ @ Nonnull Map <String , String > attributes ,
274
+ boolean activateExperiment )
270
275
throws UnknownLiveVariableException {
271
276
272
277
LiveVariable variable = getLiveVariableOrThrow (projectConfig , variableKey );
@@ -303,18 +308,18 @@ private void track(@Nonnull String eventName,
303
308
}
304
309
305
310
public @ Nullable Boolean getVariableBoolean (@ Nonnull String variableKey ,
306
- boolean activateExperiment ,
307
- @ Nonnull String userId ) throws UnknownLiveVariableException {
308
- return getVariableBoolean (variableKey , activateExperiment , userId , Collections .<String , String >emptyMap ());
311
+ @ Nonnull String userId ,
312
+ boolean activateExperiment ) throws UnknownLiveVariableException {
313
+ return getVariableBoolean (variableKey , userId , Collections .<String , String >emptyMap (), activateExperiment );
309
314
}
310
315
311
316
public @ Nullable Boolean getVariableBoolean (@ Nonnull String variableKey ,
312
- boolean activateExperiment ,
313
317
@ Nonnull String userId ,
314
- @ Nonnull Map <String , String > attributes )
318
+ @ Nonnull Map <String , String > attributes ,
319
+ boolean activateExperiment )
315
320
throws UnknownLiveVariableException {
316
321
317
- String variableValueString = getVariableString (variableKey , activateExperiment , userId , attributes );
322
+ String variableValueString = getVariableString (variableKey , userId , attributes , activateExperiment );
318
323
if (variableValueString != null ) {
319
324
return Boolean .parseBoolean (variableValueString );
320
325
}
@@ -323,18 +328,18 @@ private void track(@Nonnull String eventName,
323
328
}
324
329
325
330
public @ Nullable Integer getVariableInteger (@ Nonnull String variableKey ,
326
- boolean activateExperiment ,
327
- @ Nonnull String userId ) throws UnknownLiveVariableException {
328
- return getVariableInteger (variableKey , activateExperiment , userId , Collections .<String , String >emptyMap ());
331
+ @ Nonnull String userId ,
332
+ boolean activateExperiment ) throws UnknownLiveVariableException {
333
+ return getVariableInteger (variableKey , userId , Collections .<String , String >emptyMap (), activateExperiment );
329
334
}
330
335
331
336
public @ Nullable Integer getVariableInteger (@ Nonnull String variableKey ,
332
- boolean activateExperiment ,
333
337
@ Nonnull String userId ,
334
- @ Nonnull Map <String , String > attributes )
338
+ @ Nonnull Map <String , String > attributes ,
339
+ boolean activateExperiment )
335
340
throws UnknownLiveVariableException {
336
341
337
- String variableValueString = getVariableString (variableKey , activateExperiment , userId , attributes );
342
+ String variableValueString = getVariableString (variableKey , userId , attributes , activateExperiment );
338
343
if (variableValueString != null ) {
339
344
try {
340
345
return Integer .parseInt (variableValueString );
@@ -348,18 +353,18 @@ private void track(@Nonnull String eventName,
348
353
}
349
354
350
355
public @ Nullable Float getVariableFloat (@ Nonnull String variableKey ,
351
- boolean activateExperiment ,
352
- @ Nonnull String userId ) throws UnknownLiveVariableException {
353
- return getVariableFloat (variableKey , activateExperiment , userId , Collections .<String , String >emptyMap ());
356
+ @ Nonnull String userId ,
357
+ boolean activateExperiment ) throws UnknownLiveVariableException {
358
+ return getVariableFloat (variableKey , userId , Collections .<String , String >emptyMap (), activateExperiment );
354
359
}
355
360
356
361
public @ Nullable Float getVariableFloat (@ Nonnull String variableKey ,
357
- boolean activateExperiment ,
358
362
@ Nonnull String userId ,
359
- @ Nonnull Map <String , String > attributes )
363
+ @ Nonnull Map <String , String > attributes ,
364
+ boolean activateExperiment )
360
365
throws UnknownLiveVariableException {
361
366
362
- String variableValueString = getVariableString (variableKey , activateExperiment , userId , attributes );
367
+ String variableValueString = getVariableString (variableKey , userId , attributes , activateExperiment );
363
368
if (variableValueString != null ) {
364
369
try {
365
370
return Float .parseFloat (variableValueString );
@@ -438,6 +443,33 @@ private static ProjectConfig getProjectConfig(String datafile) throws ConfigPars
438
443
return DefaultConfigParser .getInstance ().parseProjectConfig (datafile );
439
444
}
440
445
446
+ //======== Notification listeners ========//
447
+
448
+ /**
449
+ * Add a {@link NotificationListener} if it does not exist already.
450
+ *
451
+ * @param listener listener to add
452
+ */
453
+ public void addNotificationListener (@ Nonnull NotificationListener listener ) {
454
+ notificationBroadcaster .addListener (listener );
455
+ }
456
+
457
+ /**
458
+ * Remove a {@link NotificationListener} if it exists.
459
+ *
460
+ * @param listener listener to remove
461
+ */
462
+ public void removeNotificationListener (@ Nonnull NotificationListener listener ) {
463
+ notificationBroadcaster .removeListener (listener );
464
+ }
465
+
466
+ /**
467
+ * Remove all {@link NotificationListener}.
468
+ */
469
+ public void clearNotificationListeners () {
470
+ notificationBroadcaster .clearListeners ();
471
+ }
472
+
441
473
//======== Helper methods ========//
442
474
443
475
/**
@@ -596,7 +628,7 @@ public static class Builder {
596
628
597
629
private String datafile ;
598
630
private Bucketer bucketer ;
599
- private UserExperimentRecord userExperimentRecord ;
631
+ private UserProfile userProfile ;
600
632
private ErrorHandler errorHandler ;
601
633
private EventHandler eventHandler ;
602
634
private EventBuilder eventBuilder ;
@@ -615,8 +647,8 @@ public Builder withErrorHandler(ErrorHandler errorHandler) {
615
647
return this ;
616
648
}
617
649
618
- public Builder withUserExperimentRecord ( UserExperimentRecord userExperimentRecord ) {
619
- this .userExperimentRecord = userExperimentRecord ;
650
+ public Builder withUserProfile ( UserProfile userProfile ) {
651
+ this .userProfile = userProfile ;
620
652
return this ;
621
653
}
622
654
@@ -653,7 +685,7 @@ public Optimizely build() throws ConfigParseException {
653
685
654
686
// use the default bucketer and event builder, if no overrides were provided
655
687
if (bucketer == null ) {
656
- bucketer = new Bucketer (projectConfig , userExperimentRecord );
688
+ bucketer = new Bucketer (projectConfig , userProfile );
657
689
}
658
690
659
691
if (clientEngine == null ) {
0 commit comments