From 3de13c41f62532117c638c09edbd328ccf6764c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 8 Apr 2011 16:04:43 +0200 Subject: [PATCH] Add method to encode a collection of replies. --- .../sone/fcp/AbstractSoneCommand.java | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index a8d8514..7cc4b0f 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -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 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(); + } + } -- 2.7.4