Add filter to sort replies differently.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Apr 2011 19:06:00 +0000 (21:06 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Apr 2011 19:06:00 +0000 (21:06 +0200)
src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java b/src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java
new file mode 100644 (file)
index 0000000..2567284
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Sone - ReplyGroupFilter.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.template;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.TemplateContext;
+
+/**
+ * {@link Filter} implementation that groups replies by the post the are in
+ * reply to, returning a map with the post as key and the list of replies as
+ * values.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ReplyGroupFilter implements Filter {
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
+               @SuppressWarnings("unchecked")
+               List<Reply> allReplies = (List<Reply>) data;
+               Map<Post, Set<Sone>> postSones = new HashMap<Post, Set<Sone>>();
+               Map<Post, Set<Reply>> postReplies = new HashMap<Post, Set<Reply>>();
+               for (Reply reply : allReplies) {
+                       Post post = reply.getPost();
+                       Set<Sone> sones = postSones.get(post);
+                       if (sones == null) {
+                               sones = new HashSet<Sone>();
+                               postSones.put(post, sones);
+                       }
+                       sones.add(reply.getSone());
+                       Set<Reply> replies = postReplies.get(post);
+                       if (replies == null) {
+                               replies = new HashSet<Reply>();
+                               postReplies.put(post, replies);
+                       }
+                       replies.add(reply);
+               }
+               Map<Post, Map<String, Set<?>>> result = new HashMap<Post, Map<String, Set<?>>>();
+               for (Post post : postSones.keySet()) {
+                       if (result.containsKey(post)) {
+                               continue;
+                       }
+                       Map<String, Set<?>> postResult = new HashMap<String, Set<?>>();
+                       postResult.put("sones", postSones.get(post));
+                       postResult.put("replies", postReplies.get(post));
+                       result.put(post, postResult);
+               }
+               return result;
+       }
+
+}