Add method to encode a collection of replies.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Apr 2011 14:04:43 +0000 (16:04 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Apr 2011 14:04:43 +0000 (16:04 +0200)
src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java

index a8d8514..7cc4b0f 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Collection;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
@@ -143,4 +144,30 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                return postBuilder.get();
        }
 
+       /**
+        * Creates a simple field set from the given collection of replies.
+        *
+        * @param replies
+        *            The replies to encode
+        * @param prefix
+        *            The prefix for the field names (may be empty, but not
+        *            {@code null})
+        * @return The simple field set containing the replies
+        */
+       public SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
+               SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
+
+               int replyIndex = 0;
+               replyBuilder.put(prefix + "Replies.Count", replies.size());
+               for (Reply reply : replies) {
+                       String replyPrefix = prefix + "Replies." + replyIndex++;
+                       replyBuilder.put(replyPrefix + ".ID", reply.getId());
+                       replyBuilder.put(replyPrefix + ".Sone", reply.getSone().getId());
+                       replyBuilder.put(replyPrefix + ".Time", reply.getTime());
+                       replyBuilder.put(replyPrefix + ".Text", reply.getText());
+               }
+
+               return replyBuilder.get();
+       }
+
 }