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 if (newPostNotification != null) {
53 ListNotification<Post> filteredNotification = filterNewPostNotification(newPostNotification, currentSone);
54 int notificationIndex = notifications.indexOf(newPostNotification);
55 if (filteredNotification == null) {
56 notifications.remove(notificationIndex);
58 notifications.set(notificationIndex, filteredNotification);
61 ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
62 if (newReplyNotification != null) {
63 ListNotification<Reply> filteredNotification = filterNewReplyNotification(newReplyNotification, currentSone);
64 int notificationIndex = notifications.indexOf(newReplyNotification);
65 if (filteredNotification == null) {
66 notifications.remove(notificationIndex);
68 notifications.set(notificationIndex, filteredNotification);
75 * Filters the new posts of the given notification. If {@code currentSone}
76 * is {@code null}, {@code null} is returned and the notification is
77 * subsequently removed. Otherwise only posts that are posted by friend
78 * Sones of the given Sone are retained; all other posts are removed.
80 * @param newPostNotification
81 * The new-post notification
83 * The current Sone, or {@code null} if not logged in
84 * @return The filtered new-post notification, or {@code null} if the
85 * notification should be removed
87 private static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
88 if (currentSone == null) {
91 List<Post> newPosts = new ArrayList<Post>();
92 for (Post post : newPostNotification.getElements()) {
93 if (currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone()) || currentSone.equals(post.getRecipient())) {
97 if (newPosts.isEmpty()) {
100 if (newPosts.size() == newPostNotification.getElements().size()) {
101 return newPostNotification;
103 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
104 filteredNotification.setElements(newPosts);
105 return filteredNotification;
109 * Filters the new replies of the given notification. If {@code currentSone}
110 * is {@code null}, {@code null} is returned and the notification is
111 * subsequently removed. Otherwise only replies that are replies to posts
112 * that are posted by friend Sones of the given Sone are retained; all other
113 * replies are removed.
115 * @param newReplyNotification
116 * The new-reply notification
118 * The current Sone, or {@code null} if not logged in
119 * @return The filtered new-reply notification, or {@code null} if the
120 * notification should be removed
122 private static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
123 if (currentSone == null) {
126 List<Reply> newReplies = new ArrayList<Reply>();
127 for (Reply reply : newReplyNotification.getElements()) {
128 if (((reply.getPost().getSone() != null) && currentSone.hasFriend(reply.getPost().getSone().getId())) || currentSone.equals(reply.getPost().getSone()) || currentSone.equals(reply.getPost().getRecipient())) {
129 newReplies.add(reply);
132 if (newReplies.isEmpty()) {
135 if (newReplies.size() == newReplyNotification.getElements().size()) {
136 return newReplyNotification;
138 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
139 filteredNotification.setElements(newReplies);
140 return filteredNotification;
144 * Finds the notification with the given ID in the list of notifications and
148 * The type of the item in the notification
149 * @param notifications
150 * The notification to search
151 * @param notificationId
152 * The ID of the requested notification
153 * @param notificationElementClass
154 * The class of the notification item
155 * @return The requested notification, or {@code null} if no notification
156 * with the given ID could be found
158 @SuppressWarnings("unchecked")
159 private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
160 for (Notification notification : notifications) {
161 if (!notificationId.equals(notification.getId())) {
164 return (ListNotification<T>) notification;