Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ReplyGroupFilter.java
index 2567284..709ffbc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - ReplyGroupFilter.java - Copyright © 2010 David Roden
+ * Sone - ReplyGroupFilter.java - Copyright © 2010–2015 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
@@ -21,14 +21,17 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
 import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.util.template.Filter;
 import net.pterodactylus.util.template.TemplateContext;
 
+import com.google.common.base.Optional;
+
 /**
  * {@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
@@ -42,35 +45,39 @@ public class ReplyGroupFilter implements Filter {
         * {@inheritDoc}
         */
        @Override
-       public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
+       public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
                @SuppressWarnings("unchecked")
-               List<Reply> allReplies = (List<Reply>) data;
+               List<PostReply> allReplies = (List<PostReply>) 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);
+               Map<Post, Set<PostReply>> postReplies = new HashMap<Post, Set<PostReply>>();
+               for (PostReply reply : allReplies) {
+                       /*
+                        * All replies from a new-reply notification have posts,
+                        * ListNotificationFilters takes care of that.
+                        */
+                       Optional<Post> post = reply.getPost();
+                       Set<Sone> sones = postSones.get(post.get());
                        if (sones == null) {
                                sones = new HashSet<Sone>();
-                               postSones.put(post, sones);
+                               postSones.put(post.get(), sones);
                        }
                        sones.add(reply.getSone());
-                       Set<Reply> replies = postReplies.get(post);
+                       Set<PostReply> replies = postReplies.get(post.get());
                        if (replies == null) {
-                               replies = new HashSet<Reply>();
-                               postReplies.put(post, replies);
+                               replies = new HashSet<PostReply>();
+                               postReplies.put(post.get(), 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)) {
+               for (Entry<Post, Set<Sone>> postEntry : postSones.entrySet()) {
+                       if (result.containsKey(postEntry.getKey())) {
                                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);
+                       postResult.put("sones", postEntry.getValue());
+                       postResult.put("replies", postReplies.get(postEntry.getKey()));
+                       result.put(postEntry.getKey(), postResult);
                }
                return result;
        }