Make post reply returned by provider optional.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 05:22:59 +0000 (06:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 05:22:59 +0000 (06:22 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/PostReplyProvider.java
src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java
src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java
src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java
src/main/java/net/pterodactylus/sone/web/SearchPage.java
src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java

index f056c0e..70d9ea2 100644 (file)
@@ -88,6 +88,7 @@ import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
 import com.google.common.base.Function;
+import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
@@ -580,9 +581,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * {@inheritDoc}
         */
        @Override
-       public PostReply getPostReply(String replyId) {
+       public Optional<PostReply> getPostReply(String replyId) {
                synchronized (replies) {
-                       return replies.get(replyId);
+                       return Optional.fromNullable(replies.get(replyId));
                }
        }
 
index 9854b6c..5decdc7 100644 (file)
@@ -22,6 +22,8 @@ import java.util.List;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 
+import com.google.common.base.Optional;
+
 /**
  * Interface for objects that can provide {@link PostReply}s.
  *
@@ -36,7 +38,7 @@ public interface PostReplyProvider {
         *            The ID of the reply to get
         * @return The reply, or {@code null} if there is no such reply
         */
-       public PostReply getPostReply(String id);
+       public Optional<PostReply> getPostReply(String id);
 
        /**
         * Returns all replies for the given post, order ascending by time.
index 80802ad..1617523 100644 (file)
@@ -33,6 +33,7 @@ import net.pterodactylus.sone.freenet.fcp.Command;
 import net.pterodactylus.sone.freenet.fcp.FcpException;
 import net.pterodactylus.sone.template.SoneAccessor;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.Collections2;
 
 import freenet.node.FSParseException;
@@ -211,11 +212,11 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
        protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
                try {
                        String replyId = simpleFieldSet.getString(parameterName);
-                       PostReply reply = core.getPostReply(replyId);
-                       if (reply == null) {
+                       Optional<PostReply> reply = core.getPostReply(replyId);
+                       if (!reply.isPresent()) {
                                throw new FcpException("Could not load reply from “" + replyId + "”.");
                        }
-                       return reply;
+                       return reply.get();
                } catch (FSParseException fspe1) {
                        throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
                }
index 25a9e0b..f7ca132 100644 (file)
@@ -23,6 +23,8 @@ import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 import net.pterodactylus.util.web.Method;
 
+import com.google.common.base.Optional;
+
 /**
  * This page lets the user delete a reply.
  *
@@ -53,14 +55,14 @@ public class DeleteReplyPage extends SoneTemplatePage {
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                String replyId = request.getHttpRequest().getPartAsStringFailsafe("reply", 36);
-               PostReply reply = webInterface.getCore().getPostReply(replyId);
+               Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
                String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                if (request.getMethod() == Method.POST) {
-                       if (!reply.getSone().isLocal()) {
+                       if (!reply.get().getSone().isLocal()) {
                                throw new RedirectException("noPermission.html");
                        }
                        if (request.getHttpRequest().isPartSet("confirmDelete")) {
-                               webInterface.getCore().deleteReply(reply);
+                               webInterface.getCore().deleteReply(reply.get());
                                throw new RedirectException(returnPage);
                        } else if (request.getHttpRequest().isPartSet("abortDelete")) {
                                throw new RedirectException(returnPage);
index 0bf5285..04ad304 100644 (file)
@@ -27,6 +27,8 @@ import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 
+import com.google.common.base.Optional;
+
 /**
  * Page that lets the user mark a number of {@link Sone}s, {@link Post}s, or
  * {@link Reply Replie}s as known.
@@ -71,11 +73,11 @@ public class MarkAsKnownPage extends SoneTemplatePage {
                                }
                                webInterface.getCore().markPostKnown(post);
                        } else if (type.equals("reply")) {
-                               PostReply reply = webInterface.getCore().getPostReply(id);
-                               if (reply == null) {
+                               Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
+                               if (!reply.isPresent()) {
                                        continue;
                                }
-                               webInterface.getCore().markReplyKnown(reply);
+                               webInterface.getCore().markReplyKnown(reply.get());
                        } else if (type.equals("sone")) {
                                Sone sone = webInterface.getCore().getSone(id, false);
                                if (sone == null) {
index 337918d..b937b2f 100644 (file)
@@ -44,6 +44,7 @@ import net.pterodactylus.util.text.StringEscaper;
 import net.pterodactylus.util.text.TextException;
 
 import com.google.common.base.Function;
+import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
@@ -336,7 +337,11 @@ public class SearchPage extends SoneTemplatePage {
         */
        private String getReplyPostId(String phrase) {
                String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
-               return (webInterface.getCore().getPostReply(replyId) != null) ? webInterface.getCore().getPostReply(replyId).getPost().getId() : null;
+               Optional<PostReply> postReply = webInterface.getCore().getPostReply(replyId);
+               if (!postReply.isPresent()) {
+                       return null;
+               }
+               return postReply.get().getPost().getId();
        }
 
        /**
index b1a3317..2aa217d 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.web.ajax;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -49,14 +51,14 @@ public class DeleteReplyAjaxPage extends JsonPage {
        @Override
        protected JsonObject createJsonObject(FreenetRequest request) {
                String replyId = request.getHttpRequest().getParam("reply");
-               PostReply reply = webInterface.getCore().getPostReply(replyId);
-               if (reply == null) {
+               Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
+               if (!reply.isPresent()) {
                        return createErrorJsonObject("invalid-reply-id");
                }
-               if (!reply.getSone().isLocal()) {
+               if (!reply.get().getSone().isLocal()) {
                        return createErrorJsonObject("not-authorized");
                }
-               webInterface.getCore().deleteReply(reply);
+               webInterface.getCore().deleteReply(reply.get());
                return createSuccessJsonObject();
        }
 
index 04191fa..f4a20fe 100644 (file)
@@ -31,6 +31,8 @@ import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.json.JsonArray;
 import net.pterodactylus.util.json.JsonObject;
 
+import com.google.common.base.Optional;
+
 /**
  * AJAX page that retrieves the number of “likes” a {@link Post} has.
  *
@@ -67,8 +69,11 @@ public class GetLikesAjaxPage extends JsonPage {
                        Set<Sone> sones = webInterface.getCore().getLikes(post);
                        return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
                } else if ("reply".equals(type)) {
-                       PostReply reply = webInterface.getCore().getPostReply(id);
-                       Set<Sone> sones = webInterface.getCore().getLikes(reply);
+                       Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
+                       if (!reply.isPresent()) {
+                               return createErrorJsonObject("invalid-reply-id");
+                       }
+                       Set<Sone> sones = webInterface.getCore().getLikes(reply.get());
                        return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
                }
                return createErrorJsonObject("invalid-type");
index 6139776..a2ee122 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.sone.web.ajax;
 
 import java.io.StringWriter;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.WebInterface;
@@ -62,11 +64,11 @@ public class GetReplyAjaxPage extends JsonPage {
        @Override
        protected JsonObject createJsonObject(FreenetRequest request) {
                String replyId = request.getHttpRequest().getParam("reply");
-               PostReply reply = webInterface.getCore().getPostReply(replyId);
-               if ((reply == null) || (reply.getSone() == null)) {
+               Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
+               if (!reply.isPresent()) {
                        return createErrorJsonObject("invalid-reply-id");
                }
-               return createSuccessJsonObject().put("reply", createJsonReply(request, reply, getCurrentSone(request.getToadletContext())));
+               return createSuccessJsonObject().put("reply", createJsonReply(request, reply.get(), getCurrentSone(request.getToadletContext())));
        }
 
        /**
index 13cf424..7aa6779 100644 (file)
@@ -28,6 +28,8 @@ import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.json.JsonObject;
 
+import com.google.common.base.Optional;
+
 /**
  * Ajax page that returns a formatted, relative timestamp for replies or posts.
  *
@@ -77,16 +79,16 @@ public class GetTimesAjaxPage extends JsonPage {
                if (allIds.length() > 0) {
                        String[] ids = allIds.split(",");
                        for (String id : ids) {
-                               PostReply reply = webInterface.getCore().getPostReply(id);
-                               if (reply == null) {
+                               Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
+                               if (!reply.isPresent()) {
                                        continue;
                                }
                                JsonObject replyTime = new JsonObject();
-                               Time time = getTime(reply.getTime());
+                               Time time = getTime(reply.get().getTime());
                                replyTime.put("timeText", time.getText());
                                replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
                                synchronized (dateFormat) {
-                                       replyTime.put("tooltip", dateFormat.format(new Date(reply.getTime())));
+                                       replyTime.put("tooltip", dateFormat.format(new Date(reply.get().getTime())));
                                }
                                replyTimes.put(id, replyTime);
                        }
index e9c582a..fec5800 100644 (file)
@@ -26,6 +26,8 @@ import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.json.JsonObject;
 
+import com.google.common.base.Optional;
+
 /**
  * AJAX page that lets the user mark a number of {@link Sone}s, {@link Post}s,
  * or {@link Reply}s as known.
@@ -63,11 +65,11 @@ public class MarkAsKnownAjaxPage extends JsonPage {
                                }
                                core.markPostKnown(post);
                        } else if (type.equals("reply")) {
-                               PostReply reply = core.getPostReply(id);
-                               if (reply == null) {
+                               Optional<PostReply> reply = core.getPostReply(id);
+                               if (!reply.isPresent()) {
                                        continue;
                                }
-                               core.markReplyKnown(reply);
+                               core.markReplyKnown(reply.get());
                        } else if (type.equals("sone")) {
                                Sone sone = core.getSone(id, false);
                                if (sone == null) {