11/**
22 *
3- * Copyright 2017, Optimizely and contributors
3+ * Copyright 2017-2018 , Optimizely and contributors
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
1616 */
1717package com .optimizely .ab .notification ;
1818
19+ import com .optimizely .ab .config .Experiment ;
20+ import com .optimizely .ab .config .Variation ;
21+ import com .optimizely .ab .event .LogEvent ;
1922import org .slf4j .Logger ;
2023import org .slf4j .LoggerFactory ;
2124
25+ import javax .annotation .Nonnull ;
2226import java .util .ArrayList ;
2327import java .util .HashMap ;
2428import java .util .Map ;
@@ -78,6 +82,43 @@ public NotificationCenter() {
7882 // we used a list so that notification order can mean something.
7983 private Map <NotificationType , ArrayList <NotificationHolder >> notificationsListeners =new HashMap <NotificationType , ArrayList <NotificationHolder >>();
8084
85+ /**
86+ * Convenience method to support lambdas as callbacks in later version of Java (8+).
87+ * @param activateNotificationListenerInterface
88+ * @return greater than zero if added.
89+ */
90+ public int addActivateNotificationListener (final ActivateNotificationListenerInterface activateNotificationListenerInterface ) {
91+ if (activateNotificationListenerInterface instanceof ActivateNotificationListener ) {
92+ return addNotificationListener (NotificationType .Activate , (NotificationListener )activateNotificationListenerInterface );
93+ }
94+ else {
95+ return addNotificationListener (NotificationType .Activate , new ActivateNotificationListener () {
96+ @ Override
97+ public void onActivate (@ Nonnull Experiment experiment , @ Nonnull String userId , @ Nonnull Map <String , String > attributes , @ Nonnull Variation variation , @ Nonnull LogEvent event ) {
98+ activateNotificationListenerInterface .onActivate (experiment , userId , attributes , variation , event );
99+ }
100+ });
101+ }
102+ }
103+
104+ /**
105+ * Convenience method to support lambdas as callbacks in later versions of Java (8+)
106+ * @param trackNotificationListenerInterface
107+ * @return greater than zero if added.
108+ */
109+ public int addTrackNotificationListener (final TrackNotificationListenerInterface trackNotificationListenerInterface ) {
110+ if (trackNotificationListenerInterface instanceof TrackNotificationListener ) {
111+ return addNotificationListener (NotificationType .Activate , (NotificationListener )trackNotificationListenerInterface );
112+ }
113+ else {
114+ return addNotificationListener (NotificationType .Track , new TrackNotificationListener () {
115+ @ Override
116+ public void onTrack (@ Nonnull String eventKey , @ Nonnull String userId , @ Nonnull Map <String , String > attributes , @ Nonnull Map <String , ?> eventTags , @ Nonnull LogEvent event ) {
117+ trackNotificationListenerInterface .onTrack (eventKey , userId , attributes , eventTags , event );
118+ }
119+ });
120+ }
121+ }
81122
82123 /**
83124 * Add a notification listener to the notification center.
@@ -86,7 +127,7 @@ public NotificationCenter() {
86127 * @param notificationListener - Notification to add.
87128 * @return the notification id used to remove the notification. It is greater than 0 on success.
88129 */
89- public int addNotification (NotificationType notificationType , NotificationListener notificationListener ) {
130+ public int addNotificationListener (NotificationType notificationType , NotificationListener notificationListener ) {
90131
91132 Class clazz = notificationType .notificationTypeClass ;
92133 if (clazz == null || !clazz .isInstance (notificationListener )) {
@@ -107,11 +148,11 @@ public int addNotification(NotificationType notificationType, NotificationListen
107148 }
108149
109150 /**
110- * Remove the notification listener based on the notificationId passed back from addNotification .
151+ * Remove the notification listener based on the notificationId passed back from addNotificationListener .
111152 * @param notificationID the id passed back from add notification.
112153 * @return true if removed otherwise false (if the notification is already registered, it returns false).
113154 */
114- public boolean removeNotification (int notificationID ) {
155+ public boolean removeNotificationListener (int notificationID ) {
115156 for (NotificationType type : NotificationType .values ()) {
116157 for (NotificationHolder holder : notificationsListeners .get (type )) {
117158 if (holder .notificationId == notificationID ) {
@@ -130,17 +171,17 @@ public boolean removeNotification(int notificationID) {
130171 /**
131172 * Clear out all the notification listeners.
132173 */
133- public void clearAllNotifications () {
174+ public void clearAllNotificationListeners () {
134175 for (NotificationType type : NotificationType .values ()) {
135- clearNotifications (type );
176+ clearNotificationListeners (type );
136177 }
137178 }
138179
139180 /**
140181 * Clear notification listeners by notification type.
141182 * @param notificationType type of notificationsListeners to remove.
142183 */
143- public void clearNotifications (NotificationType notificationType ) {
184+ public void clearNotificationListeners (NotificationType notificationType ) {
144185 notificationsListeners .get (notificationType ).clear ();
145186 }
146187
0 commit comments