2 * Sone - ReplyGroupFilter.java - Copyright © 2010–2016 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.template;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.Map.Entry;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.PostReply;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.util.template.Filter;
31 import net.pterodactylus.util.template.TemplateContext;
34 * {@link Filter} implementation that groups replies by the post the are in
35 * reply to, returning a map with the post as key and the list of replies as
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class ReplyGroupFilter implements Filter {
46 public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
47 @SuppressWarnings("unchecked")
48 List<PostReply> allReplies = (List<PostReply>) data;
49 Map<Post, Set<Sone>> postSones = new HashMap<>();
50 Map<Post, Set<PostReply>> postReplies = new HashMap<>();
51 for (PostReply reply : allReplies) {
53 * All replies from a new-reply notification have posts,
54 * ListNotificationFilters takes care of that.
56 Post post = reply.getPost().get();
57 Set<Sone> sones = postSones.get(post);
59 sones = new HashSet<>();
60 postSones.put(post, sones);
62 sones.add(reply.getSone());
63 Set<PostReply> replies = postReplies.get(post);
64 if (replies == null) {
65 replies = new HashSet<>();
66 postReplies.put(post, replies);
70 Map<Post, Map<String, Set<?>>> result = new HashMap<>();
71 for (Entry<Post, Set<Sone>> postEntry : postSones.entrySet()) {
72 Map<String, Set<?>> postResult = new HashMap<>();
73 postResult.put("sones", postEntry.getValue());
74 postResult.put("replies", postReplies.get(postEntry.getKey()));
75 result.put(postEntry.getKey(), postResult);