Remove debug output.
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilters.java
1 /*
2  * Sone - ListNotificationFilters.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.notify;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
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;
28
29 /**
30  * Filter for {@link ListNotification}s.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class ListNotificationFilters {
35
36         /**
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.
43          *
44          * @param notifications
45          *            The notifications to filter
46          * @param currentSone
47          *            The current Sone, or {@code null} if not logged in
48          * @return The filtered notifications
49          */
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);
57                         } else {
58                                 notifications.set(notificationIndex, filteredNotification);
59                         }
60                 }
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);
67                         } else {
68                                 notifications.set(notificationIndex, filteredNotification);
69                         }
70                 }
71                 return notifications;
72         }
73
74         /**
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.
79          *
80          * @param newPostNotification
81          *            The new-post notification
82          * @param currentSone
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
86          */
87         private static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
88                 if (currentSone == null) {
89                         return null;
90                 }
91                 List<Post> newPosts = new ArrayList<Post>();
92                 for (Post post : newPostNotification.getElements()) {
93                         if (currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone())) {
94                                 newPosts.add(post);
95                         }
96                 }
97                 if (newPosts.isEmpty()) {
98                         return null;
99                 }
100                 if (newPosts.size() == newPostNotification.getElements().size()) {
101                         return newPostNotification;
102                 }
103                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
104                 filteredNotification.setElements(newPosts);
105                 return filteredNotification;
106         }
107
108         /**
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.
114          *
115          * @param newReplyNotification
116          *            The new-reply notification
117          * @param currentSone
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
121          */
122         private static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
123                 if (currentSone == null) {
124                         return null;
125                 }
126                 List<Reply> newReplies = new ArrayList<Reply>();
127                 for (Reply reply : newReplyNotification.getElements()) {
128                         if (currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone())) {
129                                 newReplies.add(reply);
130                         }
131                 }
132                 if (newReplies.isEmpty()) {
133                         return null;
134                 }
135                 if (newReplies.size() == newReplyNotification.getElements().size()) {
136                         return newReplyNotification;
137                 }
138                 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
139                 filteredNotification.setElements(newReplies);
140                 return filteredNotification;
141         }
142
143         /**
144          * Finds the notification with the given ID in the list of notifications and
145          * returns it.
146          *
147          * @param <T>
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
157          */
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())) {
162                                 continue;
163                         }
164                         return (ListNotification<T>) notification;
165                 }
166                 return null;
167         }
168
169 }