8bc17a7f6e061d96eacc4f1f9a1f2640e0d89cda
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ReplyGroupFilter.java
1 /*
2  * Sone - ReplyGroupFilter.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.template;
19
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import net.pterodactylus.sone.data.Post;
27 import net.pterodactylus.sone.data.PostReply;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.util.template.Filter;
30 import net.pterodactylus.util.template.TemplateContext;
31
32 /**
33  * {@link Filter} implementation that groups replies by the post the are in
34  * reply to, returning a map with the post as key and the list of replies as
35  * values.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class ReplyGroupFilter implements Filter {
40
41         /**
42          * {@inheritDoc}
43          */
44         @Override
45         public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
46                 @SuppressWarnings("unchecked")
47                 List<PostReply> allReplies = (List<PostReply>) data;
48                 Map<Post, Set<Sone>> postSones = new HashMap<Post, Set<Sone>>();
49                 Map<Post, Set<PostReply>> postReplies = new HashMap<Post, Set<PostReply>>();
50                 for (PostReply reply : allReplies) {
51                         Post post = reply.getPost();
52                         Set<Sone> sones = postSones.get(post);
53                         if (sones == null) {
54                                 sones = new HashSet<Sone>();
55                                 postSones.put(post, sones);
56                         }
57                         sones.add(reply.getSone());
58                         Set<PostReply> replies = postReplies.get(post);
59                         if (replies == null) {
60                                 replies = new HashSet<PostReply>();
61                                 postReplies.put(post, replies);
62                         }
63                         replies.add(reply);
64                 }
65                 Map<Post, Map<String, Set<?>>> result = new HashMap<Post, Map<String, Set<?>>>();
66                 for (Post post : postSones.keySet()) {
67                         if (result.containsKey(post)) {
68                                 continue;
69                         }
70                         Map<String, Set<?>> postResult = new HashMap<String, Set<?>>();
71                         postResult.put("sones", postSones.get(post));
72                         postResult.put("replies", postReplies.get(post));
73                         result.put(post, postResult);
74                 }
75                 return result;
76         }
77
78 }