1818
1919import com .optimizely .ab .annotations .VisibleForTesting ;
2020import com .optimizely .ab .bucketing .Bucketer ;
21- import com .optimizely .ab .bucketing .UserExperimentRecord ;
21+ import com .optimizely .ab .bucketing .UserProfile ;
2222import com .optimizely .ab .config .Attribute ;
2323import com .optimizely .ab .config .EventType ;
2424import com .optimizely .ab .config .Experiment ;
3939import com .optimizely .ab .event .internal .EventBuilderV2 ;
4040import com .optimizely .ab .event .internal .payload .Event .ClientEngine ;
4141import com .optimizely .ab .internal .ProjectValidationUtils ;
42+ import com .optimizely .ab .notification .NotificationListener ;
43+ import com .optimizely .ab .notification .NotificationBroadcaster ;
4244
4345import org .slf4j .Logger ;
4446import org .slf4j .LoggerFactory ;
@@ -89,6 +91,7 @@ public class Optimizely {
8991 @ VisibleForTesting final ProjectConfig projectConfig ;
9092 @ VisibleForTesting final EventHandler eventHandler ;
9193 @ VisibleForTesting final ErrorHandler errorHandler ;
94+ @ VisibleForTesting final NotificationBroadcaster notificationBroadcaster = new NotificationBroadcaster ();
9295
9396 private Optimizely (@ Nonnull ProjectConfig projectConfig ,
9497 @ Nonnull Bucketer bucketer ,
@@ -104,7 +107,7 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
104107
105108 // Do work here that should be done once per Optimizely lifecycle
106109 @ VisibleForTesting void initialize () {
107- bucketer .cleanUserExperimentRecords ();
110+ bucketer .cleanUserProfiles ();
108111 }
109112
110113 //======== activate calls ========//
@@ -180,6 +183,8 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
180183 logger .error ("Unexpected exception in event dispatcher" , e );
181184 }
182185
186+ notificationBroadcaster .broadcastExperimentActivated (experiment , userId , attributes , variation );
187+
183188 return variation ;
184189 }
185190
@@ -258,15 +263,15 @@ private void track(@Nonnull String eventName,
258263 //======== live variable getters ========//
259264
260265 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 );
264269 }
265270
266271 public @ Nullable String getVariableString (@ Nonnull String variableKey ,
267- boolean activateExperiment ,
268272 @ Nonnull String userId ,
269- @ Nonnull Map <String , String > attributes )
273+ @ Nonnull Map <String , String > attributes ,
274+ boolean activateExperiment )
270275 throws UnknownLiveVariableException {
271276
272277 LiveVariable variable = getLiveVariableOrThrow (projectConfig , variableKey );
@@ -303,18 +308,18 @@ private void track(@Nonnull String eventName,
303308 }
304309
305310 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 );
309314 }
310315
311316 public @ Nullable Boolean getVariableBoolean (@ Nonnull String variableKey ,
312- boolean activateExperiment ,
313317 @ Nonnull String userId ,
314- @ Nonnull Map <String , String > attributes )
318+ @ Nonnull Map <String , String > attributes ,
319+ boolean activateExperiment )
315320 throws UnknownLiveVariableException {
316321
317- String variableValueString = getVariableString (variableKey , activateExperiment , userId , attributes );
322+ String variableValueString = getVariableString (variableKey , userId , attributes , activateExperiment );
318323 if (variableValueString != null ) {
319324 return Boolean .parseBoolean (variableValueString );
320325 }
@@ -323,18 +328,18 @@ private void track(@Nonnull String eventName,
323328 }
324329
325330 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 );
329334 }
330335
331336 public @ Nullable Integer getVariableInteger (@ Nonnull String variableKey ,
332- boolean activateExperiment ,
333337 @ Nonnull String userId ,
334- @ Nonnull Map <String , String > attributes )
338+ @ Nonnull Map <String , String > attributes ,
339+ boolean activateExperiment )
335340 throws UnknownLiveVariableException {
336341
337- String variableValueString = getVariableString (variableKey , activateExperiment , userId , attributes );
342+ String variableValueString = getVariableString (variableKey , userId , attributes , activateExperiment );
338343 if (variableValueString != null ) {
339344 try {
340345 return Integer .parseInt (variableValueString );
@@ -348,18 +353,18 @@ private void track(@Nonnull String eventName,
348353 }
349354
350355 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 );
354359 }
355360
356361 public @ Nullable Float getVariableFloat (@ Nonnull String variableKey ,
357- boolean activateExperiment ,
358362 @ Nonnull String userId ,
359- @ Nonnull Map <String , String > attributes )
363+ @ Nonnull Map <String , String > attributes ,
364+ boolean activateExperiment )
360365 throws UnknownLiveVariableException {
361366
362- String variableValueString = getVariableString (variableKey , activateExperiment , userId , attributes );
367+ String variableValueString = getVariableString (variableKey , userId , attributes , activateExperiment );
363368 if (variableValueString != null ) {
364369 try {
365370 return Float .parseFloat (variableValueString );
@@ -438,6 +443,33 @@ private static ProjectConfig getProjectConfig(String datafile) throws ConfigPars
438443 return DefaultConfigParser .getInstance ().parseProjectConfig (datafile );
439444 }
440445
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+
441473 //======== Helper methods ========//
442474
443475 /**
@@ -596,7 +628,7 @@ public static class Builder {
596628
597629 private String datafile ;
598630 private Bucketer bucketer ;
599- private UserExperimentRecord userExperimentRecord ;
631+ private UserProfile userProfile ;
600632 private ErrorHandler errorHandler ;
601633 private EventHandler eventHandler ;
602634 private EventBuilder eventBuilder ;
@@ -615,8 +647,8 @@ public Builder withErrorHandler(ErrorHandler errorHandler) {
615647 return this ;
616648 }
617649
618- public Builder withUserExperimentRecord ( UserExperimentRecord userExperimentRecord ) {
619- this .userExperimentRecord = userExperimentRecord ;
650+ public Builder withUserProfile ( UserProfile userProfile ) {
651+ this .userProfile = userProfile ;
620652 return this ;
621653 }
622654
@@ -653,7 +685,7 @@ public Optimizely build() throws ConfigParseException {
653685
654686 // use the default bucketer and event builder, if no overrides were provided
655687 if (bucketer == null ) {
656- bucketer = new Bucketer (projectConfig , userExperimentRecord );
688+ bucketer = new Bucketer (projectConfig , userProfile );
657689 }
658690
659691 if (clientEngine == null ) {
0 commit comments