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>, new-post and
39 * new-reply notifications are removed completely. If {@code currentSone} is
40 * not {@code null}, only posts that are posted by a friend Sone or the Sone
41 * itself, and replies that are replies to posts of friend Sones or the Sone
42 * itself will be retained in the notifications.
44 * @param notifications
45 * The notifications to filter
47 * The current Sone, or {@code null} if not logged in
48 * @return The filtered notifications
50 public static List<Notification> filterNotifications(List<Notification> notifications, Sone currentSone) {
51 ListNotification<Post> newPostNotification = getNotification(notifications, "new-post-notification", Post.class);
52 System.out.println("Found new-post-notification with " + ((newPostNotification != null) ? newPostNotification.getElements().size() : -1) + " posts.");
53 if (newPostNotification != null) {
54 ListNotification<Post> filteredNotification = filterNewPostNotification(newPostNotification, currentSone);
55 int notificationIndex = notifications.indexOf(newPostNotification);
56 if (filteredNotification == null) {
57 System.out.println("Removing notification.");
58 notifications.remove(notificationIndex);
60 System.out.println("Replacing Notification.");
61 notifications.set(notificationIndex, filteredNotification);
64 ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
65 System.out.println("Found new-reply-notification with " + ((newReplyNotification != null) ? newReplyNotification.getElements().size() : -1) + " replies.");
66 if (newReplyNotification != null) {
67 ListNotification<Reply> filteredNotification = filterNewReplyNotification(newReplyNotification, currentSone);
68 int notificationIndex = notifications.indexOf(newReplyNotification);
69 if (filteredNotification == null) {
70 System.out.println("Removing Notification.");
71 notifications.remove(notificationIndex);
73 System.out.println("Replacing Notification.");
74 notifications.set(notificationIndex, filteredNotification);
81 * Filters the new posts of the given notification. If {@code currentSone}
82 * is {@code null}, {@code null} is returned and the notification is
83 * subsequently removed. Otherwise only posts that are posted by friend
84 * Sones of the given Sone are retained; all other posts are removed.
86 * @param newPostNotification
87 * The new-post notification
89 * The current Sone, or {@code null} if not logged in
90 * @return The filtered new-post notification, or {@code null} if the
91 * notification should be removed
93 private static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
94 if (currentSone == null) {
97 List<Post> newPosts = new ArrayList<Post>();
98 for (Post post : newPostNotification.getElements()) {
99 System.out.println("Checking Post: " + post);
100 if (currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone())) {
101 System.out.println(" CS.hF: " + currentSone.hasFriend(post.getSone().getId()));
102 System.out.println(" CS.e:" + currentSone.equals(post.getSone()));
106 if (newPosts.size() == newPostNotification.getElements().size()) {
107 return newPostNotification;
109 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
110 filteredNotification.setElements(newPosts);
111 return filteredNotification;
115 * Filters the new replies of the given notification. If {@code currentSone}
116 * is {@code null}, {@code null} is returned and the notification is
117 * subsequently removed. Otherwise only replies that are replies to posts
118 * that are posted by friend Sones of the given Sone are retained; all other
119 * replies are removed.
121 * @param newReplyNotification
122 * The new-reply notification
124 * The current Sone, or {@code null} if not logged in
125 * @return The filtered new-reply notification, or {@code null} if the
126 * notification should be removed
128 private static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
129 if (currentSone == null) {
132 List<Reply> newReplies = new ArrayList<Reply>();
133 for (Reply reply : newReplyNotification.getElements()) {
134 System.out.println("Checking Reply: " + reply);
135 if (currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone())) {
136 System.out.println(" CS.hF: " + currentSone.hasFriend(reply.getPost().getSone().getId()));
137 System.out.println(" CS.e: " + currentSone.equals(reply.getPost().getSone()));
138 newReplies.add(reply);
141 if (newReplies.size() == newReplyNotification.getElements().size()) {
142 return newReplyNotification;
144 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
145 filteredNotification.setElements(newReplies);
146 return filteredNotification;
150 * Finds the notification with the given ID in the list of notifications and
154 * The type of the item in the notification
155 * @param notifications
156 * The notification to search
157 * @param notificationId
158 * The ID of the requested notification
159 * @param notificationElementClass
160 * The class of the notification item
161 * @return The requested notification, or {@code null} if no notification
162 * with the given ID could be found
164 @SuppressWarnings("unchecked")
165 private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
166 for (Notification notification : notifications) {
167 if (!notificationId.equals(notification.getId())) {
170 return (ListNotification<T>) notification;