From e80be3f00b1d486fd3995d8f29ce3bf04da7b61d 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:05:22 +0200 Subject: [PATCH] Remove booleans from method signatures, create explicit methods. --- .../sone/fcp/AbstractSoneCommand.java | 70 ++++++++-------------- .../pterodactylus/sone/fcp/CreatePostCommand.java | 4 +- .../pterodactylus/sone/fcp/CreateReplyCommand.java | 2 +- .../pterodactylus/sone/fcp/GetPostFeedCommand.java | 2 +- .../pterodactylus/sone/fcp/GetPostsCommand.java | 2 +- .../net/pterodactylus/sone/fcp/GetSoneCommand.java | 4 +- .../pterodactylus/sone/fcp/LikePostCommand.java | 2 +- .../pterodactylus/sone/fcp/LikeReplyCommand.java | 2 +- .../pterodactylus/sone/fcp/LockSoneCommand.java | 8 +-- .../pterodactylus/sone/fcp/UnlockSoneCommand.java | 8 +-- 10 files changed, 41 insertions(+), 63 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 0d2e813..6890589 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -118,59 +118,41 @@ public abstract class AbstractSoneCommand extends AbstractCommand { return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r"); } - /** - * Returns a Sone whose ID is a parameter in the given simple field set. - * - * @param simpleFieldSet - * The simple field set containing the ID of the Sone - * @param parameterName - * The name under which the Sone ID is stored in the simple field - * set - * @param localOnly - * {@code true} to only return local Sones, {@code false} to - * return any Sones - * @return The Sone - * @throws FcpException - * if there is no Sone ID stored under the given parameter name, - * or if the Sone ID is invalid - */ - protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException { - return getSone(simpleFieldSet, parameterName, localOnly, true).get(); + protected Optional getOptionalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { + String soneId = getMandatoryParameter(simpleFieldSet, parameterName); + return core.getSone(soneId); } - /** - * Returns a Sone whose ID is a parameter in the given simple field set. - * - * @param simpleFieldSet - * The simple field set containing the ID of the Sone - * @param parameterName - * The name under which the Sone ID is stored in the simple field - * set - * @param localOnly - * {@code true} to only return local Sones, {@code false} to - * return any Sones - * @param mandatory - * {@code true} if a valid Sone ID is required, {@code false} - * otherwise - * @return The Sone, or {@code null} if {@code mandatory} is {@code false} - * and the Sone ID is invalid - * @throws FcpException - * if there is no Sone ID stored under the given parameter name, - * or if {@code mandatory} is {@code true} and the Sone ID is - * invalid - */ - protected Optional getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException { - String soneId = simpleFieldSet.get(parameterName); - if (mandatory && (soneId == null)) { - throw new FcpException("Could not load Sone ID from “" + parameterName + "”."); + protected Sone getMandatoryLocalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { + Sone sone = getMandatorySone(simpleFieldSet, parameterName); + if (!sone.isLocal()) { + throw new FcpException("Could not load Sone from “" + sone.getId() + "”."); } + return sone; + } + + protected Sone getMandatorySone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { + String soneId = getMandatoryParameter(simpleFieldSet, parameterName); + Optional sone = getMandatorySone(soneId); + return sone.get(); + } + + private Optional getMandatorySone(String soneId) throws FcpException { Optional sone = core.getSone(soneId); - if ((mandatory && !sone.isPresent()) || (mandatory && sone.isPresent() && (localOnly && !sone.get().isLocal()))) { + if (!sone.isPresent()) { throw new FcpException("Could not load Sone from “" + soneId + "”."); } return sone; } + private String getMandatoryParameter(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { + String soneId = simpleFieldSet.get(parameterName); + if (soneId == null) { + throw new FcpException("Could not load Sone ID from “" + parameterName + "”."); + } + return soneId; + } + /** * Returns a post whose ID is a parameter in the given simple field set. * diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java index 31308be..8ffba74 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java @@ -48,11 +48,11 @@ public class CreatePostCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", true); + Sone sone = getMandatoryLocalSone(parameters, "Sone"); String text = getString(parameters, "Text"); Sone recipient = null; if (parameters.get("Recipient") != null) { - recipient = getSone(parameters, "Recipient", false); + recipient = getMandatorySone(parameters, "Recipient"); } if (sone.equals(recipient)) { return new ErrorResponse("Sone and Recipient must not be the same."); diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java index 3d371e9..4a53238 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java @@ -47,7 +47,7 @@ public class CreateReplyCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", true); + Sone sone = getMandatoryLocalSone(parameters, "Sone"); Post post = getPost(parameters, "Post"); String text = getString(parameters, "Text"); PostReply reply = sone.newPostReplyBuilder(post.getId()).withText(text).build(getCore().postReplyCreated()); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java index fb44e3a..4f3fd66 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java @@ -54,7 +54,7 @@ public class GetPostFeedCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", true); + Sone sone = getMandatoryLocalSone(parameters, "Sone"); int startPost = getInt(parameters, "StartPost", 0); int maxPosts = getInt(parameters, "MaxPosts", -1); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java index de8b692..1dca172 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java @@ -47,7 +47,7 @@ public class GetPostsCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", false); + Sone sone = getMandatorySone(parameters, "Sone"); int startPost = getInt(parameters, "StartPost", 0); int maxPosts = getInt(parameters, "MaxPosts", -1); List posts = sone.getPosts(); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java index 711398c..2e2cbc9 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java @@ -47,8 +47,8 @@ public class GetSoneCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", false); - Optional localSone = getSone(parameters, "LocalSone", false, false); + Sone sone = getMandatorySone(parameters, "Sone"); + Optional localSone = getOptionalSone(parameters, "LocalSone"); return new Response("Sone", encodeSone(sone, "", localSone)); } diff --git a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java index 44241cd..ed1d42e 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java @@ -45,7 +45,7 @@ public class LikePostCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { Post post = getPost(parameters, "Post"); - Sone sone = getSone(parameters, "Sone", true); + Sone sone = getMandatoryLocalSone(parameters, "Sone"); sone.addLikedPostId(post.getId()); return new Response("PostLiked", new SimpleFieldSetBuilder().put("LikeCount", getCore().getLikes(post).size()).get()); } diff --git a/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java index aed7ce3..ef8efc8 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java @@ -45,7 +45,7 @@ public class LikeReplyCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { PostReply reply = getReply(parameters, "Reply"); - Sone sone = getSone(parameters, "Sone", true); + Sone sone = getMandatoryLocalSone(parameters, "Sone"); sone.addLikedReplyId(reply.getId()); return new Response("ReplyLiked", new SimpleFieldSetBuilder().put("LikeCount", getCore().getLikes(reply).size()).get()); } diff --git a/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java index 5cad8ca..306d93a 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java @@ -25,8 +25,6 @@ import net.pterodactylus.sone.freenet.fcp.FcpException; import freenet.support.SimpleFieldSet; import freenet.support.api.Bucket; -import com.google.common.base.Optional; - /** * Implements the “LockSone” FCP command. If a valid local Sone was given as * parameter “Sone,” this command will always lock the Sone and reply with @@ -52,9 +50,9 @@ public class LockSoneCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Optional sone = getSone(parameters, "Sone", true, true); - getCore().lockSone(sone.get()); - return new Response("SoneLocked", new SimpleFieldSetBuilder().put("Sone", sone.get().getId()).get()); + Sone sone = getMandatoryLocalSone(parameters, "Sone"); + getCore().lockSone(sone); + return new Response("SoneLocked", new SimpleFieldSetBuilder().put("Sone", sone.getId()).get()); } } diff --git a/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java index 7ecfada..489b6a8 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java @@ -25,8 +25,6 @@ import net.pterodactylus.sone.freenet.fcp.FcpException; import freenet.support.SimpleFieldSet; import freenet.support.api.Bucket; -import com.google.common.base.Optional; - /** * Implements the “UnlockSone” FCP command. If a valid local Sone was given as * parameter “Sone,” this command will always unlock the Sone and reply with @@ -52,9 +50,9 @@ public class UnlockSoneCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Optional sone = getSone(parameters, "Sone", true, true); - getCore().unlockSone(sone.get()); - return new Response("SoneUnlocked", new SimpleFieldSetBuilder().put("Sone", sone.get().getId()).get()); + Sone sone = getMandatoryLocalSone(parameters, "Sone"); + getCore().unlockSone(sone); + return new Response("SoneUnlocked", new SimpleFieldSetBuilder().put("Sone", sone.getId()).get()); } } -- 2.7.4