From be4ea80290e01809356b960d9cd771f041ff3fce Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 26 Oct 2013 01:55:10 +0200 Subject: [PATCH] Split post encoding methods to remove boolean parameters. --- .../sone/fcp/AbstractSoneCommand.java | 69 +++++++++++----------- .../net/pterodactylus/sone/fcp/GetPostCommand.java | 2 +- .../pterodactylus/sone/fcp/GetPostFeedCommand.java | 4 +- .../pterodactylus/sone/fcp/GetPostsCommand.java | 4 +- 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 6890589..b4c24bb 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -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 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 replies = post.getReplies(); - postBuilder.put(encodeReplies(replies, prefix)); - } + return postBuilder; + } + + protected SimpleFieldSet encodePosts(Collection 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 posts, String prefix, boolean includeReplies) { + private SimpleFieldSetBuilder createPostBuilderFromPosts(Collection 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 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(); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostCommand.java index e220429..664202c 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostCommand.java @@ -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.")); } } diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java index 4f3fd66..60f6e41 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java @@ -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. emptyList(), "Posts.", false)); + return new Response("PostFeed", encodePosts(Collections. 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.")); } } diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java index 1dca172..bd579ca 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java @@ -52,9 +52,9 @@ public class GetPostsCommand extends AbstractSoneCommand { int maxPosts = getInt(parameters, "MaxPosts", -1); List posts = sone.getPosts(); if (posts.size() < startPost) { - return new Response("Posts", encodePosts(Collections. emptyList(), "Posts.", false)); + return new Response("Posts", encodePosts(Collections. 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.")); } } -- 2.7.4