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;
* {@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));
}
}
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.
*
* 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.
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;
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);
}
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.
*
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);
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.
}
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) {
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;
*/
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();
}
/**
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;
@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();
}
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.
*
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");
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;
@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())));
}
/**
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.
*
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);
}
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.
}
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) {