Rename post and reply implementations; use builder to create replies.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 15 Oct 2013 20:49:14 +0000 (22:49 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:27 +0000 (22:25 +0100)
24 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/AbstractPostReplyBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilder.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java [deleted file]
src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java
src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java [deleted file]
src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java [deleted file]
src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java [deleted file]
src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java [deleted file]
src/main/java/net/pterodactylus/sone/database/PostReplyBuilder.java
src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java
src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java
src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java

index 48afb36..7f74e14 100644 (file)
@@ -73,6 +73,7 @@ import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
 import net.pterodactylus.sone.database.PostProvider;
 import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
 import net.pterodactylus.sone.database.PostReplyProvider;
 import net.pterodactylus.sone.database.SoneProvider;
 import net.pterodactylus.sone.fcp.FcpInterface;
@@ -466,15 +467,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return database.getDirectedPosts(recipientId);
        }
 
-       /**
-        * Returns a post reply builder.
-        *
-        * @return A new post reply builder
-        */
-       public PostReplyBuilder postReplyBuilder() {
-               return database.newPostReplyBuilder();
-       }
-
        /** {@inheritDoc} */
        @Override
        public Optional<PostReply> getPostReply(String replyId) {
@@ -1087,8 +1079,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                                return;
                        }
-                       PostReplyBuilder postReplyBuilder = postReplyBuilder().withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText);
-                       replies.add(postReplyBuilder.build());
+                       PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(postId).withId(replyId).withTime(replyTime).withText(replyText);
+                       replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
                }
 
                /* load post likes. */
@@ -1298,44 +1290,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Creates a new reply.
-        *
-        * @param sone
-        *              The Sone that creates the reply
-        * @param post
-        *              The post that this reply refers to
-        * @param text
-        *              The text of the reply
-        * @return The created reply
-        */
-       public PostReply createReply(Sone sone, Post post, 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 reply for non-local Sone: %s", sone));
-                       return null;
-               }
-               PostReplyBuilder postReplyBuilder = postReplyBuilder();
-               postReplyBuilder.from(sone.getId()).to(post.getId()).withText(text.trim());
-               final PostReply reply = postReplyBuilder.build();
-               database.storePostReply(reply);
-               eventBus.post(new NewPostReplyFoundEvent(reply));
-               sone.addReply(reply);
-               touchConfiguration();
-               localElementTicker.schedule(new Runnable() {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @Override
-                       public void run() {
-                               markReplyKnown(reply);
-                       }
-               }, 10, TimeUnit.SECONDS);
-               return reply;
-       }
-
-       /**
         * Deletes the given reply.
         *
         * @param reply
@@ -1970,4 +1924,28 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                });
        }
 
+       public Optional<PostReplyCreated> postReplyCreated() {
+               return Optional.<PostReplyCreated>of(new PostReplyCreated() {
+                       @Override
+                       public void postReplyCreated(final PostReply postReply) {
+                               if (postReply.isKnown()) {
+                                       return;
+                               }
+                               eventBus.post(new NewPostReplyFoundEvent(postReply));
+                               if (postReply.getSone().isLocal()) {
+                                       localElementTicker.schedule(new Runnable() {
+
+                                               /**
+                                                * {@inheritDoc}
+                                                */
+                                               @Override
+                                               public void run() {
+                                                       markReplyKnown(postReply);
+                                               }
+                                       }, 10, TimeUnit.SECONDS);
+                               }
+                       }
+               });
+       }
+
 }
index ed8ad98..a4e73c7 100644 (file)
@@ -40,6 +40,7 @@ import net.pterodactylus.sone.data.impl.DefaultSone;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
 import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.number.Numbers;
@@ -405,10 +406,9 @@ public class SoneDownloader extends AbstractService {
                                        return null;
                                }
                                try {
-                                       PostReplyBuilder postReplyBuilder = core.postReplyBuilder();
                                        /* TODO - parse time correctly. */
-                                       postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
-                                       replies.add(postReplyBuilder.build());
+                                       PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(replyPostId).withId(replyId).withTime(Long.parseLong(replyTime)).withText(replyText);
+                                       replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
                                } catch (NumberFormatException nfe1) {
                                        /* TODO - mark Sone as bad. */
                                        logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
index f1d9ffe..4ff9643 100644 (file)
@@ -31,6 +31,7 @@ import net.pterodactylus.sone.core.Options;
 import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostBuilderFactory;
+import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.template.SoneAccessor;
@@ -553,4 +554,6 @@ public interface Sone extends Identified, Fingerprintable, PostBuilderFactory, C
 
        PostBuilder newPostBuilder();
 
+       PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;
+
 }
index 05e5ccc..c8bca7c 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.data.impl;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 import org.apache.commons.lang.StringUtils;
@@ -32,32 +33,11 @@ import net.pterodactylus.sone.database.PostReplyBuilder;
  */
 public abstract class AbstractPostReplyBuilder extends AbstractReplyBuilder<PostReplyBuilder> implements PostReplyBuilder {
 
-       /** The ID of the post the created reply refers to. */
        protected String postId;
 
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public PostReplyBuilder to(String postId) {
-               this.postId = postId;
-               return this;
-       }
-
-       //
-       // PROTECTED METHODS
-       //
-
-       /**
-        * Validates the state of this post reply builder.
-        *
-        * @throws IllegalStateException
-        *             if the state is not valid for building a new post reply
-        */
-       protected void validate() throws IllegalStateException {
-               checkState(senderId != null, "sender must not be null");
-               checkState(!StringUtils.isBlank(text), "text must not be empty");
-               checkState(postId != null, "post must not be null");
+       protected AbstractPostReplyBuilder(String senderId, String postId) {
+               super(senderId);
+               this.postId = checkNotNull(postId, "post ID must not be null");
        }
 
 }
index 5c1b976..389eb85 100644 (file)
@@ -20,12 +20,14 @@ package net.pterodactylus.sone.data.impl;
 import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Optional.of;
+import static com.google.common.base.Preconditions.checkState;
 import static java.lang.System.currentTimeMillis;
 import static java.util.UUID.randomUUID;
 
 import net.pterodactylus.sone.database.ReplyBuilder;
 
 import com.google.common.base.Optional;
+import org.apache.commons.lang.StringUtils;
 
 /**
  * Abstract implementation of a {@link ReplyBuilder}.
@@ -36,24 +38,13 @@ import com.google.common.base.Optional;
  */
 public class AbstractReplyBuilder<B extends ReplyBuilder<B>> implements ReplyBuilder<B> {
 
+       protected final String senderId;
        protected Optional<String> id = absent();
-
-       /** The sender of the reply. */
-       protected String senderId;
-
        protected Optional<Long> time = absent();
-
-       /** The text of the reply. */
        protected String text;
 
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       @SuppressWarnings("unchecked")
-       public B withId(String id) {
-               this.id = fromNullable(id);
-               return (B) this;
+       protected AbstractReplyBuilder(String senderId) {
+               this.senderId = senderId;
        }
 
        /**
@@ -61,8 +52,8 @@ public class AbstractReplyBuilder<B extends ReplyBuilder<B>> implements ReplyBui
         */
        @Override
        @SuppressWarnings("unchecked")
-       public B from(String senderId) {
-               this.senderId = senderId;
+       public B withId(String id) {
+               this.id = fromNullable(id);
                return (B) this;
        }
 
@@ -94,4 +85,15 @@ public class AbstractReplyBuilder<B extends ReplyBuilder<B>> implements ReplyBui
                return time.isPresent() ? time.get() : currentTimeMillis();
        }
 
+       /**
+        * Validates the state of this post reply builder.
+        *
+        * @throws IllegalStateException
+        *             if the state is not valid for building a new post reply
+        */
+       protected void validate() throws IllegalStateException {
+               checkState(senderId != null, "sender must not be null");
+               checkState(!StringUtils.isBlank(text), "text must not be empty");
+       }
+
 }
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java
new file mode 100644 (file)
index 0000000..c4dcb62
--- /dev/null
@@ -0,0 +1,182 @@
+/*
+ * Sone - PostImpl.java - Copyright © 2010–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 java.util.UUID;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
+
+import com.google.common.base.Optional;
+
+/**
+ * A post is a short message that a user writes in his Sone to let other users
+ * know what is going on.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultPost implements Post {
+
+       private final Database database;
+
+       /** The GUID of the post. */
+       private final UUID id;
+
+       /** The ID of the owning Sone. */
+       private final String soneId;
+
+       /** The ID of the recipient Sone. */
+       private final String recipientId;
+
+       /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
+       private final long time;
+
+       /** The text of the post. */
+       private final String text;
+
+       /** Whether the post is known. */
+       private volatile boolean known;
+
+       /**
+        * Creates a new post.
+        *
+        * @param database
+        *            The database
+        * @param id
+        *            The ID of the post
+        * @param soneId
+        *            The ID of the Sone this post belongs to
+        * @param recipientId
+        *            The ID of the recipient of the post
+        * @param time
+        *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
+        * @param text
+        *            The text of the post
+        */
+       public DefaultPost(Database database, String id, String soneId, String recipientId, long time, String text) {
+               this.database = database;
+               this.id = UUID.fromString(id);
+               this.soneId = soneId;
+               this.recipientId = recipientId;
+               this.time = time;
+               this.text = text;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getId() {
+               return id.toString();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Sone getSone() {
+               return database.getSone(soneId).get();
+       }
+
+       /**
+        * {@inheritDocs}
+        */
+       @Override
+       public Optional<String> getRecipientId() {
+               return Optional.fromNullable(recipientId);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Optional<Sone> getRecipient() {
+               return database.getSone(recipientId);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public long getTime() {
+               return time;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getText() {
+               return text;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean isKnown() {
+               return known;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public DefaultPost setKnown(boolean known) {
+               this.known = known;
+               return this;
+       }
+
+       //
+       // OBJECT METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public int hashCode() {
+               return id.hashCode();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean equals(Object object) {
+               if (!(object instanceof DefaultPost)) {
+                       return false;
+               }
+               DefaultPost post = (DefaultPost) object;
+               return post.id.equals(id);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String toString() {
+               return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
+       }
+
+}
index e73e293..0d4c71c 100644 (file)
@@ -24,7 +24,7 @@ import net.pterodactylus.sone.database.PostBuilder;
 import com.google.common.base.Optional;
 
 /**
- * {@link PostBuilder} implementation that creates {@link PostImpl} objects.
+ * {@link PostBuilder} implementation that creates {@link DefaultPost} objects.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -43,7 +43,7 @@ public class DefaultPostBuilder extends AbstractPostBuilder {
        @Override
        public Post build(Optional<PostCreated> postCreated) {
                validate();
-               PostImpl post = new PostImpl(database, getId(), senderId, recipientId.orNull(), getTime(), text);
+               DefaultPost post = new DefaultPost(database, getId(), senderId, recipientId.orNull(), getTime(), text);
                if (postCreated.isPresent()) {
                        postCreated.get().postCreated(post);
                }
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java
new file mode 100644 (file)
index 0000000..6c08106
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Sone - PostReplyImpl.java - Copyright © 2010–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.data.Post;
+import net.pterodactylus.sone.data.PostReply;
+import net.pterodactylus.sone.database.Database;
+
+import com.google.common.base.Optional;
+
+/**
+ * Simple {@link PostReply} implementation.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultPostReply extends DefaultReply<PostReply> implements PostReply {
+
+       /** The Post this reply refers to. */
+       private final String postId;
+
+       /**
+        * Creates a new reply.
+        *
+        * @param database
+        *              The database
+        * @param id
+        *              The ID of the reply
+        * @param soneId
+        *              The ID of the Sone of the reply
+        * @param time
+        *              The time of the reply
+        * @param text
+        *              The text of the reply
+        * @param postId
+        */
+       public DefaultPostReply(Database database, String id, String soneId, long time, String text, String postId) {
+               super(database, id, soneId, time, text);
+               this.postId = postId;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /** {@inheritDocs} */
+       @Override
+       public String getPostId() {
+               return postId;
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public Optional<Post> getPost() {
+               return database.getPost(postId);
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilder.java
new file mode 100644 (file)
index 0000000..8213faa
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Sone - PostReplyBuilderImpl.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.data.PostReply;
+import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.database.PostReplyBuilder;
+
+import com.google.common.base.Optional;
+
+/**
+ * {@link PostReplyBuilder} implementation that creates {@link DefaultPostReply}
+ * objects.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultPostReplyBuilder extends AbstractPostReplyBuilder {
+
+       private final Database database;
+
+       public DefaultPostReplyBuilder(Database database, String senderId, String postId) {
+               super(senderId, postId);
+               this.database = database;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
+               validate();
+
+               DefaultPostReply postReply = new DefaultPostReply(database, getId(), senderId, getTime(), text, postId);
+               if (postReplyCreated.isPresent()) {
+                       postReplyCreated.get().postReplyCreated(postReply);
+               }
+               return postReply;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java
deleted file mode 100644 (file)
index 2c303a1..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Sone - DefaultPostReplyBuilderFactory.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.PostReplyBuilder;
-import net.pterodactylus.sone.database.PostReplyBuilderFactory;
-
-import com.google.inject.Inject;
-
-/**
- * {@link PostReplyBuilderFactory} that creates {@link PostReplyBuilderImpl}s.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class DefaultPostReplyBuilderFactory implements PostReplyBuilderFactory {
-
-       private final Database database;
-
-       @Inject
-       public DefaultPostReplyBuilderFactory(Database database) {
-               this.database = database;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public PostReplyBuilder newPostReplyBuilder() {
-               return new PostReplyBuilderImpl(database);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java
new file mode 100644 (file)
index 0000000..13bd5bc
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Sone - ReplyImpl.java - Copyright © 2011–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.data.Reply;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
+
+/**
+ * Abstract base class for all replies.
+ *
+ * @param <T>
+ *             The type of the reply
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public abstract class DefaultReply<T extends Reply<T>> implements Reply<T> {
+
+       protected final Database database;
+
+       /** The ID of the reply. */
+       private final String id;
+
+       /** The Sone that created this reply. */
+       private final String soneId;
+
+       /** The time of the reply. */
+       private final long time;
+
+       /** The text of the reply. */
+       private final String text;
+
+       /** Whether the reply is known. */
+       private volatile boolean known;
+
+       /**
+        * Creates a new reply.
+        *
+        * @param database
+        *              The database
+        * @param id
+        *              The ID of the reply
+        * @param soneId
+        *              The ID of the Sone of the reply
+        * @param time
+        *              The time of the reply
+        * @param text
+        */
+       protected DefaultReply(Database database, String id, String soneId, long time, String text) {
+               this.database = database;
+               this.id = id;
+               this.soneId = soneId;
+               this.time = time;
+               this.text = text;
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public String getId() {
+               return id;
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public Sone getSone() {
+               return database.getSone(soneId).get();
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public long getTime() {
+               return time;
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public String getText() {
+               return text;
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public boolean isKnown() {
+               return known;
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       @SuppressWarnings("unchecked")
+       public T setKnown(boolean known) {
+               this.known = known;
+               return (T) this;
+       }
+
+       //
+       // OBJECT METHODS
+       //
+
+       /** {@inheritDoc} */
+       @Override
+       public int hashCode() {
+               return id.hashCode();
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public boolean equals(Object object) {
+               if (!(object instanceof Reply<?>)) {
+                       return false;
+               }
+               Reply<?> reply = (Reply<?>) object;
+               return reply.getId().equals(id);
+       }
+
+       /** {@inheritDoc} */
+       @Override
+       public String toString() {
+               return String.format("%s[id=%s,sone=%s,time=%d,text=%s]", getClass().getName(), id, soneId, time, text);
+       }
+
+}
index 6f77fb4..cc03d7a 100644 (file)
@@ -39,6 +39,7 @@ 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.database.PostReplyBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.util.logging.Logging;
 
@@ -689,6 +690,18 @@ public class DefaultSone implements Sone {
                };
        }
 
+       @Override
+       public PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException {
+               return new DefaultPostReplyBuilder(database, getId(), postId) {
+                       @Override
+                       public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
+                               PostReply postReply = super.build(postReplyCreated);
+                               database.storePostReply(postReply);
+                               return postReply;
+                       }
+               };
+       }
+
        //
        // FINGERPRINTABLE METHODS
        //
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java
deleted file mode 100644 (file)
index f0f9d35..0000000
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- * Sone - PostImpl.java - Copyright © 2010–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 java.util.UUID;
-
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
-
-import com.google.common.base.Optional;
-
-/**
- * A post is a short message that a user writes in his Sone to let other users
- * know what is going on.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class PostImpl implements Post {
-
-       private final Database database;
-
-       /** The GUID of the post. */
-       private final UUID id;
-
-       /** The ID of the owning Sone. */
-       private final String soneId;
-
-       /** The ID of the recipient Sone. */
-       private final String recipientId;
-
-       /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
-       private final long time;
-
-       /** The text of the post. */
-       private final String text;
-
-       /** Whether the post is known. */
-       private volatile boolean known;
-
-       /**
-        * Creates a new post.
-        *
-        * @param database
-        *            The database
-        * @param id
-        *            The ID of the post
-        * @param soneId
-        *            The ID of the Sone this post belongs to
-        * @param recipientId
-        *            The ID of the recipient of the post
-        * @param time
-        *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
-        * @param text
-        *            The text of the post
-        */
-       public PostImpl(Database database, String id, String soneId, String recipientId, long time, String text) {
-               this.database = database;
-               this.id = UUID.fromString(id);
-               this.soneId = soneId;
-               this.recipientId = recipientId;
-               this.time = time;
-               this.text = text;
-       }
-
-       //
-       // ACCESSORS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String getId() {
-               return id.toString();
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public Sone getSone() {
-               return database.getSone(soneId).get();
-       }
-
-       /**
-        * {@inheritDocs}
-        */
-       @Override
-       public Optional<String> getRecipientId() {
-               return Optional.fromNullable(recipientId);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public Optional<Sone> getRecipient() {
-               return database.getSone(recipientId);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public long getTime() {
-               return time;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String getText() {
-               return text;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isKnown() {
-               return known;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public PostImpl setKnown(boolean known) {
-               this.known = known;
-               return this;
-       }
-
-       //
-       // OBJECT METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public int hashCode() {
-               return id.hashCode();
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean equals(Object object) {
-               if (!(object instanceof PostImpl)) {
-                       return false;
-               }
-               PostImpl post = (PostImpl) object;
-               return post.id.equals(id);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String toString() {
-               return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java
deleted file mode 100644 (file)
index 5c7f494..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Sone - PostReplyBuilderImpl.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.data.PostReply;
-import net.pterodactylus.sone.database.Database;
-import net.pterodactylus.sone.database.PostReplyBuilder;
-
-/**
- * {@link PostReplyBuilder} implementation that creates {@link PostReplyImpl}
- * objects.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class PostReplyBuilderImpl extends AbstractPostReplyBuilder {
-
-       private final Database database;
-
-       public PostReplyBuilderImpl(Database database) {
-               this.database = database;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public PostReply build() {
-               validate();
-
-               /* create new post reply. */
-               return new PostReplyImpl(database, getId(), senderId, getTime(), text, postId);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java
deleted file mode 100644 (file)
index c1a2423..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Sone - PostReplyImpl.java - Copyright © 2010–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.data.Post;
-import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.database.Database;
-
-import com.google.common.base.Optional;
-
-/**
- * Simple {@link PostReply} implementation.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class PostReplyImpl extends ReplyImpl<PostReply> implements PostReply {
-
-       /** The Post this reply refers to. */
-       private final String postId;
-
-       /**
-        * Creates a new reply.
-        *
-        * @param database
-        *              The database
-        * @param id
-        *              The ID of the reply
-        * @param soneId
-        *              The ID of the Sone of the reply
-        * @param time
-        *              The time of the reply
-        * @param text
-        *              The text of the reply
-        * @param postId
-        */
-       public PostReplyImpl(Database database, String id, String soneId, long time, String text, String postId) {
-               super(database, id, soneId, time, text);
-               this.postId = postId;
-       }
-
-       //
-       // ACCESSORS
-       //
-
-       /** {@inheritDocs} */
-       @Override
-       public String getPostId() {
-               return postId;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public Optional<Post> getPost() {
-               return database.getPost(postId);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java
deleted file mode 100644 (file)
index fae5436..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Sone - ReplyImpl.java - Copyright © 2011–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.data.Reply;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
-
-/**
- * Abstract base class for all replies.
- *
- * @param <T>
- *             The type of the reply
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public abstract class ReplyImpl<T extends Reply<T>> implements Reply<T> {
-
-       protected final Database database;
-
-       /** The ID of the reply. */
-       private final String id;
-
-       /** The Sone that created this reply. */
-       private final String soneId;
-
-       /** The time of the reply. */
-       private final long time;
-
-       /** The text of the reply. */
-       private final String text;
-
-       /** Whether the reply is known. */
-       private volatile boolean known;
-
-       /**
-        * Creates a new reply.
-        *
-        * @param database
-        *              The database
-        * @param id
-        *              The ID of the reply
-        * @param soneId
-        *              The ID of the Sone of the reply
-        * @param time
-        *              The time of the reply
-        * @param text
-        */
-       protected ReplyImpl(Database database, String id, String soneId, long time, String text) {
-               this.database = database;
-               this.id = id;
-               this.soneId = soneId;
-               this.time = time;
-               this.text = text;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public String getId() {
-               return id;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public Sone getSone() {
-               return database.getSone(soneId).get();
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public long getTime() {
-               return time;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public String getText() {
-               return text;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public boolean isKnown() {
-               return known;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       @SuppressWarnings("unchecked")
-       public T setKnown(boolean known) {
-               this.known = known;
-               return (T) this;
-       }
-
-       //
-       // OBJECT METHODS
-       //
-
-       /** {@inheritDoc} */
-       @Override
-       public int hashCode() {
-               return id.hashCode();
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public boolean equals(Object object) {
-               if (!(object instanceof Reply<?>)) {
-                       return false;
-               }
-               Reply<?> reply = (Reply<?>) object;
-               return reply.getId().equals(id);
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public String toString() {
-               return String.format("%s[id=%s,sone=%s,time=%d,text=%s]", getClass().getName(), id, soneId, time, text);
-       }
-
-}
index c031443..2dc922a 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.sone.database;
 
 import net.pterodactylus.sone.data.PostReply;
 
+import com.google.common.base.Optional;
+
 /**
  * Builder for a {@link PostReply} object.
  *
@@ -27,16 +29,6 @@ import net.pterodactylus.sone.data.PostReply;
 public interface PostReplyBuilder extends ReplyBuilder<PostReplyBuilder> {
 
        /**
-        * Configures this builder to set the given post as post the created reply
-        * refers to.
-        *
-        * @param postId
-        *            The ID of the post the reply refers to
-        * @return This builder
-        */
-       public PostReplyBuilder to(String postId);
-
-       /**
         * Verifies the configuration of this builder and creates a new post reply.
         * <p>
         * The following conditions must be met in order for the configuration to be
@@ -56,6 +48,12 @@ public interface PostReplyBuilder extends ReplyBuilder<PostReplyBuilder> {
         * @throws IllegalStateException
         *             if this builder’s configuration is not valid
         */
-       public PostReply build() throws IllegalStateException;
+       public PostReply build(Optional<PostReplyCreated> postReplyCreated) throws IllegalStateException;
+
+       interface PostReplyCreated {
+
+               void postReplyCreated(PostReply postReply);
+
+       }
 
 }
index c9a809f..c1e5391 100644 (file)
@@ -23,7 +23,7 @@ package net.pterodactylus.sone.database;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public interface PostReplyDatabase extends PostReplyProvider, PostReplyBuilderFactory, PostReplyStore {
+public interface PostReplyDatabase extends PostReplyProvider, PostReplyStore {
 
        /* nothing here. */
 
index a04a09c..8582c83 100644 (file)
@@ -41,16 +41,6 @@ public interface ReplyBuilder<B extends ReplyBuilder<B>> {
        public B withId(String id);
 
        /**
-        * Configures this builder to use the ID of the given {@link Sone} as sender
-        * of the reply.
-        *
-        * @param senderId
-        *            The ID of the sender of the reply
-        * @return This builder
-        */
-       public B from(String senderId);
-
-       /**
         * Configures this builder to use the given time when creating the reply. If
         * this method is used, {@link #currentTime()} must not be used.
         *
index d1ceee9..00443e4 100644 (file)
@@ -362,16 +362,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        //
-       // POSTREPLYBUILDERFACTORY METHODS
-       //
-
-       /** {@inheritDocs} */
-       @Override
-       public PostReplyBuilder newPostReplyBuilder() {
-               return new MemoryPostReplyBuilder(this, this);
-       }
-
-       //
        // POSTREPLYSTORE METHODS
        //
 
index fe4bee8..2de9bef 100644 (file)
@@ -53,7 +53,7 @@ public class CreateReplyCommand extends AbstractSoneCommand {
                Sone sone = getSone(parameters, "Sone", true);
                Post post = getPost(parameters, "Post");
                String text = getString(parameters, "Text");
-               PostReply reply = getCore().createReply(sone, post, text);
+               PostReply reply = sone.newPostReplyBuilder(post.getId()).withText(text).build(getCore().postReplyCreated());
                return new Response("ReplyCreated", new SimpleFieldSetBuilder().put("Reply", reply.getId()).get());
        }
 
index 4813028..8e24e3c 100644 (file)
@@ -26,9 +26,7 @@ import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.FreenetInterface;
 import net.pterodactylus.sone.core.WebOfTrustUpdater;
 import net.pterodactylus.sone.database.Database;
-import net.pterodactylus.sone.database.PostBuilderFactory;
 import net.pterodactylus.sone.database.PostProvider;
-import net.pterodactylus.sone.database.PostReplyBuilderFactory;
 import net.pterodactylus.sone.database.SoneProvider;
 import net.pterodactylus.sone.database.memory.MemoryDatabase;
 import net.pterodactylus.sone.fcp.FcpInterface;
@@ -226,7 +224,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(PostReplyBuilderFactory.class).to(MemoryDatabase.class);
                                bind(SoneProvider.class).to(Core.class).in(Singleton.class);
                                bind(PostProvider.class).to(MemoryDatabase.class);
                                bindListener(Matchers.any(), new TypeListener() {
index 84231aa..ef83c71 100644 (file)
@@ -73,7 +73,7 @@ public class CreateReplyPage extends SoneTemplatePage {
                                        sender = of(getCurrentSone(request.getToadletContext()));
                                }
                                text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
-                               webInterface.getCore().createReply(sender.get(), post.get(), text);
+                               sender.get().newPostReplyBuilder(post.get().getId()).withText(text).build(webInterface.getCore().postReplyCreated());
                                throw new RedirectException(returnPage);
                        }
                        templateContext.set("errorTextEmpty", true);
index ab5bca3..441caaa 100644 (file)
@@ -66,7 +66,7 @@ public class CreateReplyAjaxPage extends JsonPage {
                        return createErrorJsonObject("invalid-post-id");
                }
                text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
-               PostReply reply = webInterface.getCore().createReply(sender.get(), post.get(), text);
+               PostReply reply = sender.get().newPostReplyBuilder(post.get().getId()).withText(text).build(webInterface.getCore().postReplyCreated());
                return createSuccessJsonObject().put("reply", reply.getId()).put("sone", sender.get().getId());
        }