2 * Sone - ListNotificationFilters.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.notify;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Reply;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.util.notify.Notification;
30 * Filter for {@link ListNotification}s.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class ListNotificationFilters {
37 * Filters new-post and new-reply notifications in the given list of
38 * notifications. If {@code currentSone} is <code>null</code>, nothing is
39 * filtered and the given list is returned.
40 * If {@code currentSone} is not {@code null}, only posts that are posted by
41 * a friend Sone or the Sone itself, and replies that are replies to posts
42 * of friend Sones or the Sone itself will be retained
43 * in the notifications.
45 * @param notifications
46 * The notifications to filter
48 * The current Sone, or {@code null} if not logged in
49 * @return The filtered notifications
51 public static List<Notification> filterNotifications(List<Notification> notifications, Sone currentSone) {
52 if (currentSone == null) {
55 ListNotification<Post> newPostNotification = getNotification(notifications, "new-post-notification", Post.class);
56 System.out.println("Found new-post-notification with " + ((newPostNotification != null) ? newPostNotification.getElements().size() : -1) + " posts.");
57 if (newPostNotification != null) {
58 List<Post> newPosts = new ArrayList<Post>();
59 for (Post post : newPostNotification.getElements()) {
60 System.out.println("Checking Post: " + post);
61 if (currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone())) {
62 System.out.println(" CS.hF: " + currentSone.hasFriend(post.getSone().getId()));
63 System.out.println(" CS.e:" + currentSone.equals(post.getSone()));
67 int notificationIndex = notifications.indexOf(newPostNotification);
68 if (newPosts.isEmpty()) {
69 System.out.println("Removing notification.");
70 notifications.remove(notificationIndex);
72 System.out.println("Replacing Notification.");
73 newPostNotification = new ListNotification<Post>(newPostNotification);
74 newPostNotification.setElements(newPosts);
75 notifications.set(notificationIndex, newPostNotification);
78 ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
79 System.out.println("Found new-reply-notification with " + ((newReplyNotification != null) ? newReplyNotification.getElements().size() : -1) + " replies.");
80 if (newReplyNotification != null) {
81 List<Reply> newReplies = new ArrayList<Reply>();
82 for (Reply reply : newReplyNotification.getElements()) {
83 System.out.println("Checking Reply: " + reply);
84 if (currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone())) {
85 System.out.println(" CS.hF: " + currentSone.hasFriend(reply.getPost().getSone().getId()));
86 System.out.println(" CS.e: " + currentSone.equals(reply.getPost().getSone()));
87 newReplies.add(reply);
90 int notificationIndex = notifications.indexOf(newReplyNotification);
91 if (newReplies.isEmpty()) {
92 System.out.println("Removing Notification.");
93 notifications.remove(notificationIndex);
95 System.out.println("Replacing Notification.");
96 newReplyNotification = new ListNotification<Reply>(newReplyNotification);
97 newReplyNotification.setElements(newReplies);
98 notifications.set(notificationIndex, newReplyNotification);
101 return notifications;
105 * Finds the notification with the given ID in the list of notifications and
109 * The type of the item in the notification
110 * @param notifications
111 * The notification to search
112 * @param notificationId
113 * The ID of the requested notification
114 * @param notificationElementClass
115 * The class of the notification item
116 * @return The requested notification, or {@code null} if no notification
117 * with the given ID could be found
119 @SuppressWarnings("unchecked")
120 private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
121 for (Notification notification : notifications) {
122 if (!notificationId.equals(notification.getId())) {
125 return (ListNotification<T>) notification;