Move post creation into Sone, remove memory-based post builder.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 15 Oct 2013 17:02:32 +0000 (19:02 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:26 +0000 (22:25 +0100)
12 files changed:
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/Sone.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/DefaultPostBuilderFactory.java [deleted file]
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/database/PostDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryPostBuilder.java [deleted file]
src/main/java/net/pterodactylus/sone/main/SonePlugin.java

index b035d58..14667de 100644 (file)
@@ -445,15 +445,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return trustedIdentities.containsEntry(origin.getIdentity(), target.getIdentity());
        }
 
-       /**
-        * Returns a post builder.
-        *
-        * @return A new post builder
-        */
-       public PostBuilder postBuilder() {
-               return database.newPostBuilder();
-       }
-
        /** {@inheritDoc} */
        @Override
        public Optional<Post> getPost(String postId) {
@@ -1072,7 +1063,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid post found, aborting load!");
                                return;
                        }
-                       PostBuilder postBuilder = postBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
+                       PostBuilder postBuilder = sone.newPostBuilder().withId(postId).withTime(postTime).withText(postText);
                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
                                postBuilder.to(postRecipientId);
                        }
@@ -1292,8 +1283,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
                        return null;
                }
-               PostBuilder postBuilder = database.newPostBuilder();
-               postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
+               PostBuilder postBuilder = sone.newPostBuilder();
+               postBuilder.randomId().withTime(time).withText(text.trim());
                if (recipient.isPresent()) {
                        postBuilder.to(recipient.get().getId());
                }
index 4be836b..c026a37 100644 (file)
@@ -368,9 +368,9 @@ public class SoneDownloader extends AbstractService {
                                        return null;
                                }
                                try {
-                                       PostBuilder postBuilder = core.postBuilder();
+                                       PostBuilder postBuilder = sone.newPostBuilder();
                                        /* TODO - parse time correctly. */
-                                       postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText);
+                                       postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
                                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
                                                postBuilder.to(postRecipientId);
                                        }
index 67ecccb..41fdcc1 100644 (file)
@@ -28,8 +28,9 @@ import java.util.List;
 import java.util.Set;
 
 import net.pterodactylus.sone.core.Options;
-import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.AlbumBuilderFactory;
+import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.PostBuilderFactory;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.template.SoneAccessor;
@@ -45,7 +46,7 @@ import com.google.common.primitives.Ints;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public interface Sone extends Identified, Fingerprintable, AlbumBuilderFactory, Comparable<Sone> {
+public interface Sone extends Identified, Fingerprintable, PostBuilderFactory, AlbumBuilderFactory, Comparable<Sone> {
 
        /**
         * Enumeration for the possible states of a {@link Sone}.
@@ -548,4 +549,6 @@ public interface Sone extends Identified, Fingerprintable, AlbumBuilderFactory,
        /* TODO - remove this method again, maybe add an option provider */
        void setOptions(Options options);
 
+       PostBuilder newPostBuilder();
+
 }
index 0bc7a59..ad580ee 100644 (file)
 
 package net.pterodactylus.sone.data.impl;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.PostBuilder;
 
 import org.apache.commons.lang.StringUtils;
@@ -32,15 +34,15 @@ import org.apache.commons.lang.StringUtils;
  */
 public abstract class AbstractPostBuilder implements PostBuilder {
 
+       protected final Database database;
+       protected final String senderId;
+
        /** Wether to create a post with a random ID. */
        protected boolean randomId;
 
        /** The ID of the post. */
        protected String id;
 
-       /** The sender of the post. */
-       protected String senderId;
-
        /** Whether to use the current time when creating the post. */
        protected boolean currentTime;
 
@@ -53,6 +55,11 @@ public abstract class AbstractPostBuilder implements PostBuilder {
        /** The (optional) recipient of the post. */
        protected String recipientId;
 
+       protected AbstractPostBuilder(Database database, String soneId) {
+               this.database = checkNotNull(database, "database must not be null");
+               this.senderId = checkNotNull(soneId, "sender ID must not be null");
+       }
+
        //
        // POSTBUILDER METHODS
        //
@@ -64,7 +71,6 @@ public abstract class AbstractPostBuilder implements PostBuilder {
        public PostBuilder copyPost(Post post) {
                this.randomId = false;
                this.id = post.getId();
-               this.senderId = post.getSone().getId();
                this.currentTime = false;
                this.time = post.getTime();
                this.text = post.getText();
@@ -94,15 +100,6 @@ public abstract class AbstractPostBuilder implements PostBuilder {
         * {@inheritDoc}
         */
        @Override
-       public PostBuilder from(String senderId) {
-               this.senderId = senderId;
-               return this;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
        public PostBuilder currentTime() {
                currentTime = true;
                return this;
@@ -147,10 +144,9 @@ public abstract class AbstractPostBuilder implements PostBuilder {
         */
        protected void validate() throws IllegalStateException {
                checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
-               checkState(senderId != null, "sender must not be null");
                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((recipientId == null) || !recipientId.equals(senderId), "sender and recipient must not be the same");
+               checkState(!senderId.equals(recipientId), "sender and recipient must not be the same");
        }
 
 }
index 9ac82c8..3ae2f6e 100644 (file)
@@ -30,15 +30,13 @@ import net.pterodactylus.sone.database.PostBuilder;
  */
 public class DefaultPostBuilder extends AbstractPostBuilder {
 
-       private final Database database;
-
        /**
         * Creates a new post builder.
         *
         * @param database
         */
-       public DefaultPostBuilder(Database database) {
-               this.database = database;
+       public DefaultPostBuilder(Database database, String soneId) {
+               super(database, soneId);
        }
 
        /** {@inheritDoc} */
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java
deleted file mode 100644 (file)
index 83548bd..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Sone - DefaultPostBuilderFactory.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.data.impl;
-
-import net.pterodactylus.sone.database.Database;
-import net.pterodactylus.sone.database.PostBuilder;
-import net.pterodactylus.sone.database.PostBuilderFactory;
-
-import com.google.inject.Inject;
-
-/**
- * {@link PostBuilderFactory} implementation that creates {@link
- * DefaultPostBuilder}s.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class DefaultPostBuilderFactory implements PostBuilderFactory {
-
-       private final Database database;
-
-       /**
-        * Creates a new default post builder factory.
-        *
-        * @param database
-        *              The database
-        */
-       @Inject
-       public DefaultPostBuilderFactory(Database database) {
-               this.database = database;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public PostBuilder newPostBuilder() {
-               return new DefaultPostBuilder(database);
-       }
-
-}
index 87ad6bf..213c266 100644 (file)
@@ -38,6 +38,7 @@ import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.util.logging.Logging;
 
@@ -676,6 +677,10 @@ public class DefaultSone implements Sone {
                return new DefaultAlbumBuilder(database, this, rootAlbum.getId());
        }
 
+       public PostBuilder newPostBuilder() {
+               return new DefaultPostBuilder(database, getId());
+       }
+
        //
        // FINGERPRINTABLE METHODS
        //
index 449147a..c46bd69 100644 (file)
@@ -54,15 +54,6 @@ public interface PostBuilder {
        public PostBuilder copyPost(Post post) throws NullPointerException;
 
        /**
-        * Configures this builder to use the given Sone as sender of the new post.
-        *
-        * @param senderId
-        *            The ID of the sender of the post
-        * @return This post builder
-        */
-       public PostBuilder from(String senderId);
-
-       /**
         * Configures this builder to use a random ID for the new post. If this
         * method is used, {@link #withId(String)} must not be used.
         *
index 40e6290..0832807 100644 (file)
@@ -23,7 +23,7 @@ package net.pterodactylus.sone.database;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public interface PostDatabase extends PostProvider, PostBuilderFactory, PostStore {
+public interface PostDatabase extends PostProvider, PostStore {
 
        /* nothing here. */
 
index 9a2637d..d1ceee9 100644 (file)
@@ -242,16 +242,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        //
-       // POSTBUILDERFACTORY METHODS
-       //
-
-       /** {@inheritDocs} */
-       @Override
-       public PostBuilder newPostBuilder() {
-               return new MemoryPostBuilder(this);
-       }
-
-       //
        // POSTSTORE METHODS
        //
 
diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostBuilder.java
deleted file mode 100644 (file)
index a7afcdc..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Sone - MemoryPostBuilder.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.database.memory;
-
-import java.util.UUID;
-
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.impl.AbstractPostBuilder;
-import net.pterodactylus.sone.database.PostBuilder;
-import net.pterodactylus.sone.database.SoneProvider;
-
-/**
- * {@link PostBuilder} implementation that creates a {@link MemoryPost}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-class MemoryPostBuilder extends AbstractPostBuilder {
-
-       /** The database. */
-       private final MemoryDatabase database;
-
-       /**
-        * Creates a new memory post builder.
-        *
-        * @param memoryDatabase
-        *            The database
-        * @param soneProvider
-        *            The Sone provider
-        */
-       public MemoryPostBuilder(MemoryDatabase memoryDatabase) {
-               database = memoryDatabase;
-       }
-
-       /**
-        * {@inheritDocs}
-        */
-       @Override
-       public Post build() throws IllegalStateException {
-               validate();
-               Post post = new MemoryPost(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
-               post.setKnown(database.isPostKnown(post));
-               return post;
-       }
-
-}
index e31bbf5..4813028 100644 (file)
@@ -226,7 +226,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
                                bind(SonePlugin.class).toInstance(SonePlugin.this);
                                bind(FcpInterface.class).in(Singleton.class);
                                bind(Database.class).to(MemoryDatabase.class);
-                               bind(PostBuilderFactory.class).to(MemoryDatabase.class);
                                bind(PostReplyBuilderFactory.class).to(MemoryDatabase.class);
                                bind(SoneProvider.class).to(Core.class).in(Singleton.class);
                                bind(PostProvider.class).to(MemoryDatabase.class);