From: David ‘Bombe’ Roden Date: Thu, 7 Apr 2011 19:06:00 +0000 (+0200) Subject: Add filter to sort replies differently. X-Git-Tag: 0.6.1^2~19 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=90d19c992a93d1a54ecf46704acf16361c888465;p=Sone.git Add filter to sort replies differently. --- 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; + } + +}