From: David ‘Bombe’ Roden Date: Fri, 8 Apr 2011 14:04:43 +0000 (+0200) Subject: Add method to encode a collection of replies. X-Git-Tag: 0.6.5^2~39^2~34 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3de13c41f62532117c638c09edbd328ccf6764c5;p=Sone.git Add method to encode a collection of replies. --- 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(); + } + }