X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FReplyGroupFilter.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FReplyGroupFilter.java;h=25672845a3e30b2c88e7516992a9e97750437aeb;hb=90d19c992a93d1a54ecf46704acf16361c888465;hp=0000000000000000000000000000000000000000;hpb=e17523ae9c800ddf6a8e31bfa4a56f4aa0278aec;p=Sone.git
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
index 0000000..2567284
--- /dev/null
+++ b/src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java
@@ -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 .
+ */
+
+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 David âBombeâ Roden
+ */
+public class ReplyGroupFilter implements Filter {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Object format(TemplateContext templateContext, Object data, Map parameters) {
+ @SuppressWarnings("unchecked")
+ List allReplies = (List) data;
+ Map> postSones = new HashMap>();
+ Map> postReplies = new HashMap>();
+ for (Reply reply : allReplies) {
+ Post post = reply.getPost();
+ Set sones = postSones.get(post);
+ if (sones == null) {
+ sones = new HashSet();
+ postSones.put(post, sones);
+ }
+ sones.add(reply.getSone());
+ Set replies = postReplies.get(post);
+ if (replies == null) {
+ replies = new HashSet();
+ postReplies.put(post, replies);
+ }
+ replies.add(reply);
+ }
+ Map>> result = new HashMap>>();
+ for (Post post : postSones.keySet()) {
+ if (result.containsKey(post)) {
+ continue;
+ }
+ Map> postResult = new HashMap>();
+ postResult.put("sones", postSones.get(post));
+ postResult.put("replies", postReplies.get(post));
+ result.put(post, postResult);
+ }
+ return result;
+ }
+
+}