Split post encoding methods to remove boolean parameters.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 25 Oct 2013 23:55:10 +0000 (01:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:45 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java
src/main/java/net/pterodactylus/sone/fcp/GetPostCommand.java
src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java
src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java

index 6890589..b4c24bb 100644 (file)
@@ -266,20 +266,20 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                return soneBuilder.get();
        }
 
-       /**
-        * Creates a simple field set from the given post.
-        *
-        * @param post
-        *            The post to encode
-        * @param prefix
-        *            The prefix for the field names (may be empty but not
-        *            {@code null})
-        * @param includeReplies
-        *            {@code true} to include replies, {@code false} to not include
-        *            replies
-        * @return The simple field set containing the post
-        */
-       protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
+       protected SimpleFieldSet encodePost(Post post, String prefix) {
+               return createPostBuilderFromPost(post, prefix).get();
+       }
+
+       protected SimpleFieldSet encodePostWithReplies(Post post, String prefix) {
+               SimpleFieldSetBuilder postBuilder = createPostBuilderFromPost(post, prefix);
+
+               List<PostReply> replies = post.getReplies();
+               postBuilder.put(encodeReplies(replies, prefix));
+
+               return postBuilder.get();
+       }
+
+       private SimpleFieldSetBuilder createPostBuilderFromPost(Post post, String prefix) {
                SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
 
                postBuilder.put(prefix + "ID", post.getId());
@@ -291,38 +291,35 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                postBuilder.put(prefix + "Text", encodeString(post.getText()));
                postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
 
-               if (includeReplies) {
-                       List<PostReply> replies = post.getReplies();
-                       postBuilder.put(encodeReplies(replies, prefix));
-               }
+               return postBuilder;
+       }
+
+       protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix) {
+               SimpleFieldSetBuilder postBuilder = createPostBuilderFromPosts(posts, prefix);
 
                return postBuilder.get();
        }
 
-       /**
-        * Creates a simple field set from the given collection of posts.
-        *
-        * @param posts
-        *            The posts to encode
-        * @param prefix
-        *            The prefix for the field names (may be empty but not
-        *            {@code null})
-        * @param includeReplies
-        *            {@code true} to include the replies, {@code false} to not
-        *            include the replies
-        * @return The simple field set containing the posts
-        */
-       protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
+       private SimpleFieldSetBuilder createPostBuilderFromPosts(Collection<? extends Post> posts, String prefix) {
                SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
 
                int postIndex = 0;
                postBuilder.put(prefix + "Count", posts.size());
                for (Post post : posts) {
                        String postPrefix = prefix + postIndex++;
-                       postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
-                       if (includeReplies) {
-                               postBuilder.put(encodeReplies(from(post.getReplies()).filter(Reply.FUTURE_REPLY_FILTER).toList(), postPrefix + "."));
-                       }
+                       postBuilder.put(encodePost(post, postPrefix + "."));
+               }
+
+               return postBuilder;
+       }
+
+       protected SimpleFieldSet encodePostsWithReplies(Collection<? extends Post> posts, String prefix) {
+               SimpleFieldSetBuilder postBuilder = createPostBuilderFromPosts(posts, prefix);
+
+               int postIndex = 0;
+               for (Post post : posts) {
+                       String postPrefix = prefix + postIndex++;
+                       postBuilder.put(encodeReplies(from(post.getReplies()).filter(Reply.FUTURE_REPLY_FILTER).toList(), postPrefix + "."));
                }
 
                return postBuilder.get();
index e220429..664202c 100644 (file)
@@ -45,7 +45,7 @@ public class GetPostCommand extends AbstractSoneCommand {
                Post post = getPost(parameters, "Post");
                boolean includeReplies = getBoolean(parameters, "IncludeReplies", true);
 
-               return new Response("Post", encodePost(post, "Post.", includeReplies));
+               return new Response("Post", includeReplies ? encodePostWithReplies(post, "Post.") : encodePost(post, "Post."));
        }
 
 }
index 4f3fd66..60f6e41 100644 (file)
@@ -74,10 +74,10 @@ public class GetPostFeedCommand extends AbstractSoneCommand {
                Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
 
                if (sortedPosts.size() < startPost) {
-                       return new Response("PostFeed", encodePosts(Collections.<Post> emptyList(), "Posts.", false));
+                       return new Response("PostFeed", encodePosts(Collections.<Post> emptyList(), "Posts."));
                }
 
-               return new Response("PostFeed", encodePosts(sortedPosts.subList(startPost, (maxPosts == -1) ? sortedPosts.size() : Math.min(startPost + maxPosts, sortedPosts.size())), "Posts.", true));
+               return new Response("PostFeed", encodePostsWithReplies(sortedPosts.subList(startPost, (maxPosts == -1) ? sortedPosts.size() : Math.min(startPost + maxPosts, sortedPosts.size())), "Posts."));
        }
 
 }
index 1dca172..bd579ca 100644 (file)
@@ -52,9 +52,9 @@ public class GetPostsCommand extends AbstractSoneCommand {
                int maxPosts = getInt(parameters, "MaxPosts", -1);
                List<Post> posts = sone.getPosts();
                if (posts.size() < startPost) {
-                       return new Response("Posts", encodePosts(Collections.<Post> emptyList(), "Posts.", false));
+                       return new Response("Posts", encodePosts(Collections.<Post> emptyList(), "Posts."));
                }
-               return new Response("Posts", encodePosts(sone.getPosts().subList(startPost, (maxPosts == -1) ? posts.size() : Math.min(startPost + maxPosts, posts.size())), "Posts.", true));
+               return new Response("Posts", encodePostsWithReplies(sone.getPosts().subList(startPost, (maxPosts == -1) ? posts.size() : Math.min(startPost + maxPosts, posts.size())), "Posts."));
        }
 
 }