Remove createPost(*) methods from Core.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 15 Oct 2013 19:15:28 +0000 (21:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:26 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java
src/main/java/net/pterodactylus/sone/database/PostBuilder.java
src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java
src/main/java/net/pterodactylus/sone/web/CreatePostPage.java
src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java

index 14667de..898e314 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.core;
 
+import static com.google.common.base.Optional.of;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Predicates.not;
@@ -1065,7 +1066,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        }
                        PostBuilder postBuilder = sone.newPostBuilder().withId(postId).withTime(postTime).withText(postText);
                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
-                               postBuilder.to(postRecipientId);
+                               postBuilder.to(of(postRecipientId));
                        }
                        posts.add(postBuilder.build());
                }
@@ -1219,94 +1220,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Creates a new post.
-        *
-        * @param sone
-        *              The Sone that creates the post
-        * @param text
-        *              The text of the post
-        * @return The created post
-        */
-       public Post createPost(Sone sone, String text) {
-               return createPost(sone, System.currentTimeMillis(), text);
-       }
-
-       /**
-        * Creates a new post.
-        *
-        * @param sone
-        *              The Sone that creates the post
-        * @param time
-        *              The time of the post
-        * @param text
-        *              The text of the post
-        * @return The created post
-        */
-       public Post createPost(Sone sone, long time, String text) {
-               return createPost(sone, null, time, text);
-       }
-
-       /**
-        * Creates a new post.
-        *
-        * @param sone
-        *              The Sone that creates the post
-        * @param recipient
-        *              The recipient Sone, or {@code null} if this post does not have a
-        *              recipient
-        * @param text
-        *              The text of the post
-        * @return The created post
-        */
-       public Post createPost(Sone sone, Optional<Sone> recipient, String text) {
-               return createPost(sone, recipient, System.currentTimeMillis(), text);
-       }
-
-       /**
-        * Creates a new post.
-        *
-        * @param sone
-        *              The Sone that creates the post
-        * @param recipient
-        *              The recipient Sone, or {@code null} if this post does not have a
-        *              recipient
-        * @param time
-        *              The time of the post
-        * @param text
-        *              The text of the post
-        * @return The created post
-        */
-       public Post createPost(Sone sone, Optional<Sone> recipient, long time, String text) {
-               checkNotNull(text, "text must not be null");
-               checkArgument(text.trim().length() > 0, "text must not be empty");
-               if (!sone.isLocal()) {
-                       logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
-                       return null;
-               }
-               PostBuilder postBuilder = sone.newPostBuilder();
-               postBuilder.randomId().withTime(time).withText(text.trim());
-               if (recipient.isPresent()) {
-                       postBuilder.to(recipient.get().getId());
-               }
-               final Post post = postBuilder.build();
-               database.storePost(post);
-               eventBus.post(new NewPostFoundEvent(post));
-               sone.addPost(post);
-               touchConfiguration();
-               localElementTicker.schedule(new Runnable() {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @Override
-                       public void run() {
-                               markPostKnown(post);
-                       }
-               }, 10, TimeUnit.SECONDS);
-               return post;
-       }
-
-       /**
         * Deletes the given post.
         *
         * @param post
index c026a37..640a3a0 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.core;
 
+import static com.google.common.base.Optional.of;
+
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.util.HashSet;
@@ -372,7 +374,7 @@ public class SoneDownloader extends AbstractService {
                                        /* TODO - parse time correctly. */
                                        postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
                                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
-                                               postBuilder.to(postRecipientId);
+                                               postBuilder.to(of(postRecipientId));
                                        }
                                        posts.add(postBuilder.build());
                                } catch (NumberFormatException nfe1) {
index 22f98db..65dd1bd 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.data.impl;
 
+import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
@@ -24,6 +25,7 @@ import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.PostBuilder;
 
+import com.google.common.base.Optional;
 import org.apache.commons.lang.StringUtils;
 
 /**
@@ -53,7 +55,7 @@ public abstract class AbstractPostBuilder implements PostBuilder {
        protected String text;
 
        /** The (optional) recipient of the post. */
-       protected String recipientId;
+       protected Optional<String> recipientId = absent();
 
        protected AbstractPostBuilder(Database database, String soneId) {
                this.database = checkNotNull(database, "database must not be null");
@@ -113,7 +115,7 @@ public abstract class AbstractPostBuilder implements PostBuilder {
         * {@inheritDoc}
         */
        @Override
-       public PostBuilder to(String recipientId) {
+       public PostBuilder to(Optional<String> recipientId) {
                this.recipientId = recipientId;
                return this;
        }
@@ -132,7 +134,7 @@ public abstract class AbstractPostBuilder implements PostBuilder {
                checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
                checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
                checkState(!StringUtils.isBlank(text), "text must not be empty");
-               checkState(!senderId.equals(recipientId), "sender and recipient must not be the same");
+               checkState(!recipientId.isPresent() || !senderId.equals(recipientId.get()), "sender and recipient must not be the same");
        }
 
 }
index 3ae2f6e..1aa1596 100644 (file)
@@ -43,7 +43,7 @@ public class DefaultPostBuilder extends AbstractPostBuilder {
        @Override
        public Post build() {
                validate();
-               return new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
+               return new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId.orNull(), currentTime ? System.currentTimeMillis() : time, text);
        }
 
 }
index 213c266..800fd0a 100644 (file)
@@ -678,7 +678,14 @@ public class DefaultSone implements Sone {
        }
 
        public PostBuilder newPostBuilder() {
-               return new DefaultPostBuilder(database, getId());
+               return new DefaultPostBuilder(database, getId()) {
+                       @Override
+                       public Post build() {
+                               Post post = super.build();
+                               database.storePost(post);
+                               return post;
+                       }
+               };
        }
 
        //
index 0563fed..0114ac5 100644 (file)
@@ -20,6 +20,8 @@ package net.pterodactylus.sone.database;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 
+import com.google.common.base.Optional;
+
 /**
  * Builder for {@link Post} objects.
  * <p>
@@ -95,7 +97,7 @@ public interface PostBuilder {
         *            The ID of the recipient of the post
         * @return This post builder
         */
-       public PostBuilder to(String recipientId);
+       public PostBuilder to(Optional<String> recipientId);
 
        /**
         * Verifies this builder’s configuration and creates a new post.
index f6c5dd9..ae45957 100644 (file)
 
 package net.pterodactylus.sone.fcp;
 
+import static com.google.common.base.Optional.fromNullable;
+import static net.pterodactylus.sone.data.Identified.GET_ID;
+
 import com.google.common.base.Optional;
 
 import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Identified;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
@@ -30,7 +34,6 @@ import freenet.support.api.Bucket;
 /**
  * FCP command that creates a new {@link Post}.
  *
- * @see Core#createPost(Sone, Sone, String)
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class CreatePostCommand extends AbstractSoneCommand {
@@ -59,7 +62,7 @@ public class CreatePostCommand extends AbstractSoneCommand {
                if (sone.equals(recipient)) {
                        return new ErrorResponse("Sone and Recipient must not be the same.");
                }
-               Post post = getCore().createPost(sone, Optional.fromNullable(recipient), text);
+               Post post = sone.newPostBuilder().randomId().currentTime().to(fromNullable(recipient).transform(GET_ID)).withText(text).build();
                return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get());
        }
 
index f32e089..032271e 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.web;
 
 import static com.google.common.base.Optional.of;
 
+import net.pterodactylus.sone.data.Identified;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.text.TextFilter;
@@ -71,7 +72,7 @@ public class CreatePostPage extends SoneTemplatePage {
                                }
                                Optional<Sone> recipient = webInterface.getCore().getSone(recipientId);
                                text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
-                               webInterface.getCore().createPost(sender.get(), recipient, System.currentTimeMillis(), text);
+                               sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(Identified.GET_ID)).withText(text).build();
                                throw new RedirectException(returnPage);
                        }
                        templateContext.set("errorTextEmpty", true);
index a4ab02c..bf67db7 100644 (file)
@@ -18,7 +18,9 @@
 package net.pterodactylus.sone.web.ajax;
 
 import static com.google.common.base.Optional.of;
+import static net.pterodactylus.sone.data.Identified.GET_ID;
 
+import net.pterodactylus.sone.data.Identified;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.text.TextFilter;
@@ -65,7 +67,7 @@ public class CreatePostAjaxPage extends JsonPage {
                        return createErrorJsonObject("text-required");
                }
                text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
-               Post newPost = webInterface.getCore().createPost(sender.get(), recipient, text);
+               Post newPost = sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(GET_ID)).withText(text).build();
                return createSuccessJsonObject().put("postId", newPost.getId()).put("sone", sender.get().getId()).put("recipient", newPost.getRecipientId().orNull());
        }