From fcabe38e9b3abacc0d580bf0513600858aee2eca Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 22 Jan 2013 09:38:18 +0100 Subject: [PATCH] Only store the post ID in the reply. --- .../java/net/pterodactylus/sone/core/Core.java | 4 +- .../pterodactylus/sone/core/SoneDownloader.java | 2 +- .../net/pterodactylus/sone/data/PostReply.java | 6 +- .../pterodactylus/sone/data/PostReplyBuilder.java | 8 +-- .../data/impl/DefaultPostReplyBuilderFactory.java | 19 +++++- .../sone/data/impl/PostReplyBuilderImpl.java | 29 ++++++--- .../sone/data/impl/PostReplyImpl.java | 76 +++++----------------- .../net/pterodactylus/sone/main/SonePlugin.java | 2 + 8 files changed, 67 insertions(+), 79 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index ca4e16b..8172b45 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1330,7 +1330,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return; } PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder(); - postReplyBuilder.withId(replyId).from(sone).to(getPost(postId)).withTime(replyTime).withText(replyText); + postReplyBuilder.withId(replyId).from(sone).to(postId).withTime(replyTime).withText(replyText); replies.add(postReplyBuilder.build()); } @@ -1663,7 +1663,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return null; } PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder(); - postReplyBuilder.randomId().from(sone).to(post).currentTime().withText(text.trim()); + postReplyBuilder.randomId().from(sone).to(post.getId()).currentTime().withText(text.trim()); final PostReply reply = postReplyBuilder.build(); synchronized (replies) { replies.put(reply.getId(), reply); diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 51bc6fd..cf85d03 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -409,7 +409,7 @@ public class SoneDownloader extends AbstractService { try { PostReplyBuilder postReplyBuilder = core.postReplyBuilder(); /* TODO - parse time correctly. */ - postReplyBuilder.withId(replyId).from(sone).to(core.getPost(replyPostId)).withTime(Long.parseLong(replyTime)).withText(replyText); + postReplyBuilder.withId(replyId).from(sone).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText); replies.add(postReplyBuilder.build()); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ diff --git a/src/main/java/net/pterodactylus/sone/data/PostReply.java b/src/main/java/net/pterodactylus/sone/data/PostReply.java index a4d9ef3..4452e2e 100644 --- a/src/main/java/net/pterodactylus/sone/data/PostReply.java +++ b/src/main/java/net/pterodactylus/sone/data/PostReply.java @@ -35,10 +35,10 @@ public interface PostReply extends Reply { /** * Sets the post this reply refers to. * - * @param post - * The post this reply refers to + * @param postId + * The ID of the post to reply to * @return This reply */ - public PostReply setPost(Post post); + public PostReply setPost(String postId); } diff --git a/src/main/java/net/pterodactylus/sone/data/PostReplyBuilder.java b/src/main/java/net/pterodactylus/sone/data/PostReplyBuilder.java index 6c3e236..1ba7fd5 100644 --- a/src/main/java/net/pterodactylus/sone/data/PostReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/PostReplyBuilder.java @@ -28,11 +28,11 @@ public interface PostReplyBuilder extends ReplyBuilder { * Configures this builder to set the given post as post the created reply * refers to. * - * @param post - * The post the reply refers to + * @param postId + * The ID of the post the reply refers to * @return This builder */ - public PostReplyBuilder to(Post post); + public PostReplyBuilder to(String postId); /** * Verifies the configuration of this builder and creates a new post reply. @@ -47,7 +47,7 @@ public interface PostReplyBuilder extends ReplyBuilder { * have been called. *
  • The {@link #withText(String) text} must not be {@code null} and must * contain something other than whitespace.
  • - *
  • The {@link #to(Post) post} have been set.
  • + *
  • The {@link #to(String) post} have been set.
  • * * * @return The created post reply diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java index efe3c30..80bc3c7 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java @@ -17,9 +17,12 @@ package net.pterodactylus.sone.data.impl; +import net.pterodactylus.sone.core.PostProvider; import net.pterodactylus.sone.data.PostReplyBuilder; import net.pterodactylus.sone.data.PostReplyBuilderFactory; +import com.google.inject.Inject; + /** * {@link PostReplyBuilderFactory} that creates {@link PostReplyBuilderImpl}s. * @@ -27,12 +30,26 @@ import net.pterodactylus.sone.data.PostReplyBuilderFactory; */ public class DefaultPostReplyBuilderFactory implements PostReplyBuilderFactory { + /** The post provider. */ + private final PostProvider postProvider; + + /** + * Creates a new default post reply builder factory. + * + * @param postProvider + * The post provider + */ + @Inject + public DefaultPostReplyBuilderFactory(PostProvider postProvider) { + this.postProvider = postProvider; + } + /** * {@inheritDoc} */ @Override public PostReplyBuilder newPostReplyBuilder() { - return new PostReplyBuilderImpl(); + return new PostReplyBuilderImpl(postProvider); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java index 35b87e8..18ce200 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java @@ -21,7 +21,7 @@ import static com.google.common.base.Preconditions.checkState; import java.util.UUID; -import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.core.PostProvider; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.PostReplyBuilder; @@ -35,15 +35,28 @@ import org.apache.commons.lang.StringUtils; */ public class PostReplyBuilderImpl extends AbstractReplyBuilder implements PostReplyBuilder { - /** The post the created reply refers to. */ - private Post post; + /** The post builder. */ + private final PostProvider postProvider; + + /** The ID of the post the created reply refers to. */ + private String postId; + + /** + * Creates a new post reply builder. + * + * @param postProvider + * The post provider + */ + public PostReplyBuilderImpl(PostProvider postProvider) { + this.postProvider = postProvider; + } /** * {@inheritDoc} */ @Override - public PostReplyBuilder to(Post post) { - this.post = post; + public PostReplyBuilder to(String postId) { + this.postId = postId; return this; } @@ -56,12 +69,12 @@ public class PostReplyBuilderImpl extends AbstractReplyBuilder checkState(sender != null, "sender must not be null"); checkState((currentTime && (time == 0)) || (!currentTime && (time >= 0)), "either current time or custom time must be set"); checkState(!StringUtils.isBlank(text), "text must not be empty"); - checkState(post != null, "post must not be null"); + checkState(postId != null, "post must not be null"); /* create new post reply. */ - PostReplyImpl postReplyImpl = new PostReplyImpl(randomId ? UUID.randomUUID().toString() : id); + PostReplyImpl postReplyImpl = new PostReplyImpl(postProvider, randomId ? UUID.randomUUID().toString() : id); postReplyImpl.setSone(sender); - postReplyImpl.setPost(post); + postReplyImpl.setPost(postId); postReplyImpl.setTime(currentTime ? System.currentTimeMillis() : time); postReplyImpl.setText(text); return postReplyImpl; diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java index 0e66a8f..361a2e9 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java @@ -17,11 +17,9 @@ package net.pterodactylus.sone.data.impl; -import java.util.UUID; - +import net.pterodactylus.sone.core.PostProvider; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; /** * Simple {@link PostReply} implementation. @@ -30,66 +28,24 @@ import net.pterodactylus.sone.data.Sone; */ public class PostReplyImpl extends ReplyImpl implements PostReply { - /** The Post this reply refers to. */ - private volatile Post post; + /** The post provider. */ + private final PostProvider postProvider; - /** - * Creates a new reply. - * - * @param id - * The ID of the reply - */ - public PostReplyImpl(String id) { - this(id, null, null, 0, null); - } + /** The Post this reply refers to. */ + private volatile String postId; /** * Creates a new reply. * - * @param sone - * The sone that posted the reply - * @param post - * The post to reply to - * @param text - * The text of the reply - */ - public PostReplyImpl(Sone sone, Post post, String text) { - this(sone, post, System.currentTimeMillis(), text); - } - - /** - * Creates a new reply- - * - * @param sone - * The sone that posted the reply - * @param post - * The post to reply to - * @param time - * The time of the reply - * @param text - * The text of the reply - */ - public PostReplyImpl(Sone sone, Post post, long time, String text) { - this(UUID.randomUUID().toString(), sone, post, time, text); - } - - /** - * Creates a new reply- - * - * @param sone - * The sone that posted the reply + * @param postProvider + * The post provider * @param id * The ID of the reply - * @param post - * The post to reply to - * @param time - * The time of the reply - * @param text - * The text of the reply */ - public PostReplyImpl(String id, Sone sone, Post post, long time, String text) { - super(id, sone, time, text); - this.post = post; + public PostReplyImpl(PostProvider postProvider, String id) { + super(id); + this.postProvider = postProvider; + this.postId = postId; } // @@ -101,19 +57,19 @@ public class PostReplyImpl extends ReplyImpl implements PostReply { */ @Override public Post getPost() { - return post; + return postProvider.getPost(postId); } /** * Sets the post this reply refers to. * - * @param post - * The post this reply refers to + * @param postId + * The ID of the post to reply to * @return This reply (for method chaining) */ @Override - public PostReply setPost(Post post) { - this.post = post; + public PostReply setPost(String postId) { + this.postId = postId; return this; } diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 152a70f..1114c42 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -24,6 +24,7 @@ import java.util.logging.Logger; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.core.FreenetInterface; +import net.pterodactylus.sone.core.PostProvider; import net.pterodactylus.sone.core.WebOfTrustUpdater; import net.pterodactylus.sone.data.PostBuilderFactory; import net.pterodactylus.sone.data.PostReplyBuilderFactory; @@ -223,6 +224,7 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr bind(FcpInterface.class).in(Singleton.class); bind(PostBuilderFactory.class).to(DefaultPostBuilderFactory.class).in(Singleton.class); bind(PostReplyBuilderFactory.class).to(DefaultPostReplyBuilderFactory.class).in(Singleton.class); + bind(PostProvider.class).to(Core.class).in(Singleton.class); bindListener(Matchers.any(), new TypeListener() { @Override -- 2.7.4