From: David ‘Bombe’ Roden <bombe@pterodactylus.net>
Date: Fri, 15 Oct 2010 19:50:05 +0000 (+0200)
Subject: Add method to get all replies for a post.
X-Git-Tag: 0.1-RC1~317
X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f2b0a90f537568be345280c43797125b51698c60;p=Sone.git

Add method to get all replies for a post.
---

diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java
index 459431c..a0f0a59 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -18,9 +18,12 @@
 package net.pterodactylus.sone.core;
 
 import java.net.MalformedURLException;
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
@@ -297,6 +300,33 @@ public class Core extends AbstractService {
 		return replyCache.get(replyId);
 	}
 
+	/**
+	 * Gets all replies to the given post, sorted by date, oldest first.
+	 *
+	 * @param post
+	 *            The post the replies refer to
+	 * @return The sorted list of replies for the post
+	 */
+	public List<Reply> getReplies(Post post) {
+		List<Reply> replies = new ArrayList<Reply>();
+		for (Reply reply : replyCache.values()) {
+			if (reply.getPost().equals(post)) {
+				replies.add(reply);
+			}
+		}
+		Collections.sort(replies, new Comparator<Reply>() {
+
+			/**
+			 * {@inheritDoc}
+			 */
+			@Override
+			public int compare(Reply leftReply, Reply rightReply) {
+				return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
+			}
+		});
+		return replies;
+	}
+
 	//
 	// SERVICE METHODS
 	//