-
Notifications
You must be signed in to change notification settings - Fork 32
refactor so that any java 8 or later can use lambdas for Activate and… #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b9cc418
db8f932
a7718ae
d6e9881
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.optimizely.ab.notification; | ||
|
||
import com.optimizely.ab.config.Experiment; | ||
import com.optimizely.ab.config.Variation; | ||
import com.optimizely.ab.event.LogEvent; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Map; | ||
|
||
public interface ActivateNotificationListenerInterface { | ||
/** | ||
* onActivate called when an activate was triggered | ||
* @param experiment - The experiment object being activated. | ||
* @param userId - The userId passed into activate. | ||
* @param attributes - The filtered attribute list passed into activate | ||
* @param variation - The variation that was returned from activate. | ||
* @param event - The impression event that was triggered. | ||
*/ | ||
public void onActivate(@Nonnull Experiment experiment, | ||
@Nonnull String userId, | ||
@Nonnull Map<String, String> attributes, | ||
@Nonnull Variation variation, | ||
@Nonnull LogEvent event) ; | ||
|
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/** | ||
* | ||
* Copyright 2017, Optimizely and contributors | ||
* Copyright 2017-2018, Optimizely and contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -16,9 +16,13 @@ | |
*/ | ||
package com.optimizely.ab.notification; | ||
|
||
import com.optimizely.ab.config.Experiment; | ||
import com.optimizely.ab.config.Variation; | ||
import com.optimizely.ab.event.LogEvent; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
@@ -78,6 +82,43 @@ public NotificationCenter() { | |
// we used a list so that notification order can mean something. | ||
private Map<NotificationType, ArrayList<NotificationHolder>> notificationsListeners =new HashMap<NotificationType, ArrayList<NotificationHolder>>(); | ||
|
||
/** | ||
* Convenience method to support lambdas as callbacks in later version of Java (8+). | ||
* @param activateNotificationListenerInterface | ||
* @return greater than zero if added. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we will run into concurrency/threading issues with incrementing this static variable. What if we end up with the same ID for different listeners? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not static. you are looking at the holder class def and not the notification id. |
||
*/ | ||
public int addActivateNotificationListener(final ActivateNotificationListenerInterface activateNotificationListenerInterface) { | ||
if (activateNotificationListenerInterface instanceof ActivateNotificationListener) { | ||
return addNotificationListener(NotificationType.Activate, (NotificationListener)activateNotificationListenerInterface); | ||
} | ||
else { | ||
return addNotificationListener(NotificationType.Activate, new ActivateNotificationListener() { | ||
@Override | ||
public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Variation variation, @Nonnull LogEvent event) { | ||
activateNotificationListenerInterface.onActivate(experiment, userId, attributes, variation, event); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Convenience method to support lambdas as callbacks in later versions of Java (8+) | ||
* @param trackNotificationListenerInterface | ||
* @return greater than zero if added. | ||
*/ | ||
public int addTrackNotificationListener(final TrackNotificationListenerInterface trackNotificationListenerInterface) { | ||
if (trackNotificationListenerInterface instanceof TrackNotificationListener) { | ||
return addNotificationListener(NotificationType.Activate, (NotificationListener)trackNotificationListenerInterface); | ||
} | ||
else { | ||
return addNotificationListener(NotificationType.Track, new TrackNotificationListener() { | ||
@Override | ||
public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) { | ||
trackNotificationListenerInterface.onTrack(eventKey, userId, attributes, eventTags, event); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Add a notification listener to the notification center. | ||
|
@@ -86,7 +127,7 @@ public NotificationCenter() { | |
* @param notificationListener - Notification to add. | ||
* @return the notification id used to remove the notification. It is greater than 0 on success. | ||
*/ | ||
public int addNotification(NotificationType notificationType, NotificationListener notificationListener) { | ||
public int addNotificationListener(NotificationType notificationType, NotificationListener notificationListener) { | ||
|
||
Class clazz = notificationType.notificationTypeClass; | ||
if (clazz == null || !clazz.isInstance(notificationListener)) { | ||
|
@@ -107,11 +148,11 @@ public int addNotification(NotificationType notificationType, NotificationListen | |
} | ||
|
||
/** | ||
* Remove the notification listener based on the notificationId passed back from addNotification. | ||
* Remove the notification listener based on the notificationId passed back from addNotificationListener. | ||
* @param notificationID the id passed back from add notification. | ||
* @return true if removed otherwise false (if the notification is already registered, it returns false). | ||
*/ | ||
public boolean removeNotification(int notificationID) { | ||
public boolean removeNotificationListener(int notificationID) { | ||
for (NotificationType type : NotificationType.values()) { | ||
for (NotificationHolder holder : notificationsListeners.get(type)) { | ||
if (holder.notificationId == notificationID) { | ||
|
@@ -130,17 +171,17 @@ public boolean removeNotification(int notificationID) { | |
/** | ||
* Clear out all the notification listeners. | ||
*/ | ||
public void clearAllNotifications() { | ||
public void clearAllNotificationListeners() { | ||
for (NotificationType type : NotificationType.values()) { | ||
clearNotifications(type); | ||
clearNotificationListeners(type); | ||
} | ||
} | ||
|
||
/** | ||
* Clear notification listeners by notification type. | ||
* @param notificationType type of notificationsListeners to remove. | ||
*/ | ||
public void clearNotifications(NotificationType notificationType) { | ||
public void clearNotificationListeners(NotificationType notificationType) { | ||
notificationsListeners.get(notificationType).clear(); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change the license header to say 2017-2018. Let's make sure to do this for the files we touch in the PR