Filter notifications differently.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 6 May 2011 20:11:19 +0000 (22:11 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 6 May 2011 20:11:19 +0000 (22:11 +0200)
src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java

index 264e77c..2362d6a 100644 (file)
@@ -50,28 +50,25 @@ public class ListNotificationFilters {
         *            The current Sone, or {@code null} if not logged in
         * @return The filtered notifications
         */
-       public static List<Notification> filterNotifications(List<Notification> notifications, Sone currentSone) {
-               ListNotification<Post> newPostNotification = getNotification(notifications, "new-post-notification", Post.class);
-               if (newPostNotification != null) {
-                       ListNotification<Post> filteredNotification = filterNewPostNotification(newPostNotification, currentSone);
-                       int notificationIndex = notifications.indexOf(newPostNotification);
-                       if (filteredNotification == null) {
-                               notifications.remove(notificationIndex);
-                       } else {
-                               notifications.set(notificationIndex, filteredNotification);
-                       }
-               }
-               ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
-               if (newReplyNotification != null) {
-                       ListNotification<Reply> filteredNotification = filterNewReplyNotification(newReplyNotification, currentSone);
-                       int notificationIndex = notifications.indexOf(newReplyNotification);
-                       if (filteredNotification == null) {
-                               notifications.remove(notificationIndex);
+       @SuppressWarnings("unchecked")
+       public static List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
+               List<Notification> filteredNotifications = new ArrayList<Notification>();
+               for (Notification notification : notifications) {
+                       if (notification.getId().equals("new-post-notification")) {
+                               ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone);
+                               if (filteredNotification != null) {
+                                       filteredNotifications.add(filteredNotification);
+                               }
+                       } else if (notification.getId().equals("new-replies-notification")) {
+                               ListNotification<Reply> filteredNotification = filterNewReplyNotification((ListNotification<Reply>) notification, currentSone);
+                               if (filteredNotification != null) {
+                                       filteredNotifications.add(filteredNotification);
+                               }
                        } else {
-                               notifications.set(notificationIndex, filteredNotification);
+                               filteredNotifications.add(notification);
                        }
                }
-               return notifications;
+               return filteredNotifications;
        }
 
        /**
@@ -144,32 +141,6 @@ public class ListNotificationFilters {
        }
 
        /**
-        * Finds the notification with the given ID in the list of notifications and
-        * returns it.
-        *
-        * @param <T>
-        *            The type of the item in the notification
-        * @param notifications
-        *            The notification to search
-        * @param notificationId
-        *            The ID of the requested notification
-        * @param notificationElementClass
-        *            The class of the notification item
-        * @return The requested notification, or {@code null} if no notification
-        *         with the given ID could be found
-        */
-       @SuppressWarnings("unchecked")
-       private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
-               for (Notification notification : notifications) {
-                       if (!notificationId.equals(notification.getId())) {
-                               continue;
-                       }
-                       return (ListNotification<T>) notification;
-               }
-               return null;
-       }
-
-       /**
         * Checks whether a post is visible to the given Sone. A post is not
         * considered visible if one of the following statements is true:
         * <ul>