|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2017, 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 org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * This class handles impression and conversion notificationsListeners. It replaces NotificationBroadcaster and is intended to be |
| 29 | + * more flexible. |
| 30 | + */ |
| 31 | +public class NotificationCenter { |
| 32 | + /** |
| 33 | + * NotificationType is used for the notification types supported. |
| 34 | + */ |
| 35 | + public enum NotificationType { |
| 36 | + |
| 37 | + Activate(ActivateNotification.class), // Activate was called. Track an impression event |
| 38 | + Track(TrackNotification.class); // Track was called. Track a conversion event |
| 39 | + |
| 40 | + private Class notificationTypeClass; |
| 41 | + |
| 42 | + NotificationType(Class notificationClass) { |
| 43 | + this.notificationTypeClass = notificationClass; |
| 44 | + } |
| 45 | + |
| 46 | + public Class getNotificationTypeClass() { |
| 47 | + return notificationTypeClass; |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + |
| 52 | + // the notification id is incremented and is assigned as the callback id, it can then be used to remove the notification. |
| 53 | + private int notificationListenerID = 1; |
| 54 | + |
| 55 | + final private static Logger logger = LoggerFactory.getLogger(NotificationCenter.class); |
| 56 | + |
| 57 | + // notification holder holds the id as well as the notification. |
| 58 | + private static class NotificationHolder |
| 59 | + { |
| 60 | + int notificationId; |
| 61 | + NotificationListener notificationListener; |
| 62 | + |
| 63 | + NotificationHolder(int id, NotificationListener notificationListener) { |
| 64 | + notificationId = id; |
| 65 | + this.notificationListener = notificationListener; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Instantiate a new NotificationCenter |
| 71 | + */ |
| 72 | + public NotificationCenter() { |
| 73 | + notificationsListeners.put(NotificationType.Activate, new ArrayList<NotificationHolder>()); |
| 74 | + notificationsListeners.put(NotificationType.Track, new ArrayList<NotificationHolder>()); |
| 75 | + } |
| 76 | + |
| 77 | + // private list of notification by notification type. |
| 78 | + // we used a list so that notification order can mean something. |
| 79 | + private Map<NotificationType, ArrayList<NotificationHolder>> notificationsListeners =new HashMap<NotificationType, ArrayList<NotificationHolder>>(); |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * Add a notification listener to the notification center. |
| 84 | + * |
| 85 | + * @param notificationType - enum NotificationType to add. |
| 86 | + * @param notificationListener - Notification to add. |
| 87 | + * @return the notification id used to remove the notification. It is greater than 0 on success. |
| 88 | + */ |
| 89 | + public int addNotification(NotificationType notificationType, NotificationListener notificationListener) { |
| 90 | + |
| 91 | + Class clazz = notificationType.notificationTypeClass; |
| 92 | + if (clazz == null || !clazz.isInstance(notificationListener)) { |
| 93 | + logger.warn("Notification listener was the wrong type. It was not added to the notification center."); |
| 94 | + return -1; |
| 95 | + } |
| 96 | + |
| 97 | + for (NotificationHolder holder : notificationsListeners.get(notificationType)) { |
| 98 | + if (holder.notificationListener == notificationListener) { |
| 99 | + logger.warn("Notificication listener was already added"); |
| 100 | + return -1; |
| 101 | + } |
| 102 | + } |
| 103 | + int id = notificationListenerID++; |
| 104 | + notificationsListeners.get(notificationType).add(new NotificationHolder(id, notificationListener )); |
| 105 | + logger.info("Notification listener {} was added with id {}", notificationListener.toString(), id); |
| 106 | + return id; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Remove the notification listener based on the notificationId passed back from addNotification. |
| 111 | + * @param notificationID the id passed back from add notification. |
| 112 | + * @return true if removed otherwise false (if the notification is already registered, it returns false). |
| 113 | + */ |
| 114 | + public boolean removeNotification(int notificationID) { |
| 115 | + for (NotificationType type : NotificationType.values()) { |
| 116 | + for (NotificationHolder holder : notificationsListeners.get(type)) { |
| 117 | + if (holder.notificationId == notificationID) { |
| 118 | + notificationsListeners.get(type).remove(holder); |
| 119 | + logger.info("Notification listener removed {}", notificationID); |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + logger.warn("Notification listener with id {} not found", notificationID); |
| 126 | + |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Clear out all the notification listeners. |
| 132 | + */ |
| 133 | + public void clearAllNotifications() { |
| 134 | + for (NotificationType type : NotificationType.values()) { |
| 135 | + clearNotifications(type); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Clear notification listeners by notification type. |
| 141 | + * @param notificationType type of notificationsListeners to remove. |
| 142 | + */ |
| 143 | + public void clearNotifications(NotificationType notificationType) { |
| 144 | + notificationsListeners.get(notificationType).clear(); |
| 145 | + } |
| 146 | + |
| 147 | + // fire a notificaiton of a certain type. The arg list changes depending on the type of notification sent. |
| 148 | + public void sendNotifications(NotificationType notificationType, Object ...args) { |
| 149 | + ArrayList<NotificationHolder> holders = notificationsListeners.get(notificationType); |
| 150 | + for (NotificationHolder holder : holders) { |
| 151 | + try { |
| 152 | + holder.notificationListener.notify(args); |
| 153 | + } |
| 154 | + catch (Exception e) { |
| 155 | + logger.error("Unexpected exception calling notification listener {}", holder.notificationId, e); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments