Skip to content

Commit 4e28052

Browse files
Mike Davismikeproeng37
authored andcommitted
refac: Notification center to use generics. (#288)
1 parent 684ab9c commit 4e28052

23 files changed

+1067
-276
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,12 @@ private void sendImpression(@Nonnull ProjectConfig projectConfig,
254254
logger.error("Unexpected exception in event dispatcher", e);
255255
}
256256

257-
notificationCenter.sendNotifications(NotificationCenter.NotificationType.Activate, experiment, userId,
258-
filteredAttributes, variation, impressionEvent);
257+
// Kept For backwards compatibility.
258+
// This notification is deprecated and the new DecisionNotifications
259+
// are sent via their respective method calls.
260+
ActivateNotification activateNotification = new ActivateNotification(
261+
experiment, userId, filteredAttributes, variation, impressionEvent);
262+
notificationCenter.send(activateNotification);
259263
} else {
260264
logger.info("Experiment has \"Launched\" status so not dispatching event during activation.");
261265
}
@@ -330,8 +334,10 @@ public void track(@Nonnull String eventName,
330334
logger.error("Unexpected exception in event dispatcher", e);
331335
}
332336

333-
notificationCenter.sendNotifications(NotificationCenter.NotificationType.Track, eventName, userId,
337+
TrackNotification notification = new TrackNotification(eventName, userId,
334338
copiedAttributes, eventTags, conversionEvent);
339+
340+
notificationCenter.send(notification);
335341
}
336342

337343
//======== FeatureFlag APIs ========//
@@ -419,7 +425,8 @@ public Boolean isFeatureEnabled(@Nonnull String featureKey,
419425
.withSource(decisionSource)
420426
.withSourceInfo(sourceInfo)
421427
.build();
422-
notificationCenter.sendNotifications(decisionNotification);
428+
429+
notificationCenter.send(decisionNotification);
423430

424431
logger.info("Feature \"{}\" is not enabled for user \"{}\".", featureKey, userId);
425432
return featureEnabled;
@@ -695,7 +702,7 @@ <T extends Object> T getFeatureVariableValueForType(@Nonnull String featureKey,
695702
.build();
696703

697704

698-
notificationCenter.sendNotifications(decisionNotification);
705+
notificationCenter.send(decisionNotification);
699706

700707
return (T) convertedValue;
701708
}
@@ -790,7 +797,7 @@ public Variation getVariation(@Nonnull Experiment experiment,
790797
.withType(notificationType)
791798
.build();
792799

793-
notificationCenter.sendNotifications(decisionNotification);
800+
notificationCenter.send(decisionNotification);
794801

795802
return variation;
796803
}
@@ -919,6 +926,31 @@ private boolean validateUserId(String userId) {
919926
return copiedAttributes;
920927
}
921928

929+
//======== Notification APIs ========//
930+
931+
public NotificationCenter getNotificationCenter() {
932+
return notificationCenter;
933+
}
934+
935+
/**
936+
* Convenience method for adding DecisionNotification Handlers
937+
*/
938+
public int addDecisionNotificationHandler(NotificationHandler<DecisionNotification> handler) {
939+
NotificationManager<DecisionNotification> manager = notificationCenter
940+
.getNotificationManager(DecisionNotification.class);
941+
return manager.addHandler(handler);
942+
}
943+
944+
/**
945+
* Convenience method for adding TrackNotification Handlers
946+
*/
947+
public int addTrackNotificationHandler(NotificationHandler<TrackNotification> handler) {
948+
NotificationManager<TrackNotification> notificationManager =
949+
notificationCenter.getNotificationManager(TrackNotification.class);
950+
return notificationManager.addHandler(handler);
951+
}
952+
953+
922954
//======== Builder ========//
923955

924956
public static Builder builder(@Nonnull String datafile,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
*
3+
* Copyright 2019, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.optimizely.ab.notification;
18+
19+
import com.optimizely.ab.annotations.VisibleForTesting;
20+
import com.optimizely.ab.config.Experiment;
21+
import com.optimizely.ab.config.Variation;
22+
import com.optimizely.ab.event.LogEvent;
23+
24+
import java.util.Map;
25+
26+
/**
27+
* ActivateNotification supplies notification for AB activatation.
28+
*
29+
* @deprecated in favor of {@link DecisionNotification} which provides notifications for Experiment, Feature
30+
* and Rollout decisions.
31+
*/
32+
@Deprecated
33+
public final class ActivateNotification {
34+
35+
private final Experiment experiment;
36+
private final String userId;
37+
private final Map<String, ?> attributes;
38+
private final Variation variation;
39+
private final LogEvent event;
40+
41+
@VisibleForTesting
42+
ActivateNotification() {
43+
this(null, null, null, null, null);
44+
}
45+
46+
/**
47+
* @param experiment - The experiment object being activated.
48+
* @param userId - The userId passed into activate.
49+
* @param attributes - The filtered attribute list passed into activate
50+
* @param variation - The variation that was returned from activate.
51+
* @param event - The impression event that was triggered.
52+
*/
53+
public ActivateNotification(Experiment experiment, String userId, Map<String, ?> attributes, Variation variation, LogEvent event) {
54+
this.experiment = experiment;
55+
this.userId = userId;
56+
this.attributes = attributes;
57+
this.variation = variation;
58+
this.event = event;
59+
}
60+
61+
public Experiment getExperiment() {
62+
return experiment;
63+
}
64+
65+
public String getUserId() {
66+
return userId;
67+
}
68+
69+
public Map<String, ?> getAttributes() {
70+
return attributes;
71+
}
72+
73+
public Variation getVariation() {
74+
return variation;
75+
}
76+
77+
public LogEvent getEvent() {
78+
return event;
79+
}
80+
81+
82+
}

core-api/src/main/java/com/optimizely/ab/notification/ActivateNotificationListener.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@
2424
import javax.annotation.Nonnull;
2525
import java.util.Map;
2626

27+
/**
28+
* ActivateNotificationListener handles the activate event notification.
29+
*
30+
* @deprecated along with {@link ActivateNotification} and users should implement
31+
* NotificationHandler&lt;DecisionNotification&gt; directly.
32+
*/
2733
@Deprecated
28-
public abstract class ActivateNotificationListener implements NotificationListener, ActivateNotificationListenerInterface {
34+
public abstract class ActivateNotificationListener implements NotificationHandler<ActivateNotification>, NotificationListener, ActivateNotificationListenerInterface {
2935

3036
/**
3137
* Base notify called with var args. This method parses the parameters and calls the abstract method.
3238
*
3339
* @param args - variable argument list based on the type of notification.
40+
*
41+
* @deprecated by {@link ActivateNotificationListener#handle(ActivateNotification)}
3442
*/
3543
@Override
44+
@Deprecated
3645
public final void notify(Object... args) {
3746
assert (args[0] instanceof Experiment);
3847
Experiment experiment = (Experiment) args[0];
@@ -51,6 +60,17 @@ public final void notify(Object... args) {
5160
onActivate(experiment, userId, attributes, variation, logEvent);
5261
}
5362

63+
@Override
64+
public final void handle(ActivateNotification message) {
65+
onActivate(
66+
message.getExperiment(),
67+
message.getUserId(),
68+
message.getAttributes(),
69+
message.getVariation(),
70+
message.getEvent()
71+
);
72+
}
73+
5474
/**
5575
* onActivate called when an activate was triggered
5676
*

core-api/src/main/java/com/optimizely/ab/notification/ActivateNotificationListenerInterface.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
import javax.annotation.Nonnull;
2424
import java.util.Map;
2525

26+
/**
27+
* ActivateNotificationListenerInterface provides and interface for activate event notification.
28+
*
29+
* @deprecated along with {@link ActivateNotification} and {@link ActivateNotificationListener}
30+
* and users should implement NotificationHandler&lt;DecisionNotification&gt; directly.
31+
*/
2632
@Deprecated
2733
public interface ActivateNotificationListenerInterface {
2834
/**

core-api/src/main/java/com/optimizely/ab/notification/DecisionNotification.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
3030
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
3131

32-
public class DecisionNotification {
32+
public final class DecisionNotification {
3333
protected String type;
3434
protected String userId;
3535
protected Map<String, ?> attributes;

core-api/src/main/java/com/optimizely/ab/notification/DecisionNotificationListener.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)