Add method to encode a collection of Sones into a simple field set.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Apr 2011 13:55:25 +0000 (15:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Apr 2011 13:55:25 +0000 (15:55 +0200)
src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java

index 567fe73..a8d8514 100644 (file)
@@ -26,6 +26,7 @@ import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
 import net.pterodactylus.sone.freenet.fcp.Command;
 import net.pterodactylus.sone.freenet.fcp.FcpException;
+import net.pterodactylus.sone.template.SoneAccessor;
 import freenet.node.FSParseException;
 import freenet.support.SimpleFieldSet;
 
@@ -94,6 +95,29 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
        }
 
        /**
+        * Creates a simple field set from the given collection of Sones.
+        *
+        * @param sones
+        *            The Sones to encode
+        * @return The simple field set containing the given Sones
+        */
+       protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones) {
+               SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
+
+               int soneIndex = 0;
+               soneBuilder.put("Sones.Count", sones.size());
+               for (Sone sone : sones) {
+                       String sonePrefix = "Sones." + soneIndex++;
+                       soneBuilder.put(sonePrefix + ".ID", sone.getId());
+                       soneBuilder.put(sonePrefix + ".Name", sone.getName());
+                       soneBuilder.put(sonePrefix + ".NiceName", SoneAccessor.getNiceName(sone));
+                       soneBuilder.put(sonePrefix + ".Time", sone.getTime());
+               }
+
+               return soneBuilder.get();
+       }
+
+       /**
         * Creates a simple field set from the given collection of posts.
         *
         * @param posts