/*
- * Sone - ReplyGroupFilter.java - Copyright © 2010–2012 David Roden
+ * Sone - ReplyGroupFilter.java - Copyright © 2010–2016 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
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;
public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
@SuppressWarnings("unchecked")
List<PostReply> allReplies = (List<PostReply>) data;
- Map<Post, Set<Sone>> postSones = new HashMap<Post, Set<Sone>>();
- Map<Post, Set<PostReply>> postReplies = new HashMap<Post, Set<PostReply>>();
+ Map<Post, Set<Sone>> postSones = new HashMap<>();
+ Map<Post, Set<PostReply>> postReplies = new HashMap<>();
for (PostReply reply : allReplies) {
- Post post = reply.getPost();
+ /*
+ * All replies from a new-reply notification have posts,
+ * ListNotificationFilters takes care of that.
+ */
+ Post post = reply.getPost().get();
Set<Sone> sones = postSones.get(post);
if (sones == null) {
- sones = new HashSet<Sone>();
+ sones = new HashSet<>();
postSones.put(post, sones);
}
sones.add(reply.getSone());
Set<PostReply> replies = postReplies.get(post);
if (replies == null) {
- replies = new HashSet<PostReply>();
+ replies = new HashSet<>();
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);
+ Map<Post, Map<String, Set<?>>> result = new HashMap<>();
+ for (Entry<Post, Set<Sone>> postEntry : postSones.entrySet()) {
+ Map<String, Set<?>> postResult = new HashMap<>();
+ postResult.put("sones", postEntry.getValue());
+ postResult.put("replies", postReplies.get(postEntry.getKey()));
+ result.put(postEntry.getKey(), postResult);
}
return result;
}