Only store sender and recipient IDs in a post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 22 Jan 2013 09:22:29 +0000 (10:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 22 Jan 2013 09:22:29 +0000 (10:22 +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/PostBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java
src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java
src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java

index 5e8bc26..8faad72 100644 (file)
@@ -1104,7 +1104,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                synchronized (knownPosts) {
                                        for (Post post : sone.getPosts()) {
                                                PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
-                                               postBuilder.copyPost(post).from(storedSone);
+                                               postBuilder.copyPost(post).from(storedSone.getId());
                                                Post newPost = postBuilder.build().setKnown(knownPosts.contains(post.getId()));
                                                if (!storedPosts.contains(newPost)) {
                                                        if (newPost.getTime() < getSoneFollowingTime(sone)) {
@@ -1307,9 +1307,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid post found, aborting load!");
                                return;
                        }
-                       PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone).withTime(postTime).withText(postText);
+                       PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
-                               postBuilder.to(getSone(postRecipientId));
+                               postBuilder.to(postRecipientId);
                        }
                        posts.add(postBuilder.build());
                }
@@ -1536,9 +1536,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
-               postBuilder.from(sone).randomId().withTime(time).withText(text.trim());
+               postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
                if (recipient != null) {
-                       postBuilder.to(recipient);
+                       postBuilder.to(recipient.getId());
                }
                final Post post = postBuilder.build();
                synchronized (posts) {
index cf85d03..6c61fd8 100644 (file)
@@ -376,9 +376,9 @@ public class SoneDownloader extends AbstractService {
                                try {
                                        PostBuilder postBuilder = core.postBuilder();
                                        /* TODO - parse time correctly. */
-                                       postBuilder.withId(postId).from(sone).withTime(Long.parseLong(postTime)).withText(postText);
+                                       postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText);
                                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
-                                               postBuilder.to(core.getSone(postRecipientId));
+                                               postBuilder.to(postRecipientId);
                                        }
                                        posts.add(postBuilder.build());
                                } catch (NumberFormatException nfe1) {
index 00f7354..87d02ee 100644 (file)
@@ -53,11 +53,11 @@ public interface PostBuilder {
        /**
         * Configures this builder to use the given Sone as sender of the new post.
         *
-        * @param sender
-        *            The sender of the post
+        * @param senderId
+        *            The ID of the sender of the post
         * @return This post builder
         */
-       public PostBuilder from(Sone sender);
+       public PostBuilder from(String senderId);
 
        /**
         * Configures this builder to use a random ID for the new post. If this
@@ -108,11 +108,11 @@ public interface PostBuilder {
         * Configures the builder to use the given {@link Sone} as recipient for the
         * post.
         *
-        * @param recipient
-        *            The recipient of the post
+        * @param recipientId
+        *            The ID of the recipient of the post
         * @return This post builder
         */
-       public PostBuilder to(Sone recipient);
+       public PostBuilder to(String recipientId);
 
        /**
         * Verifies this builder’s configuration and creates a new post.
@@ -122,14 +122,14 @@ public interface PostBuilder {
         * <ul>
         * <li>Exactly one of {@link #randomId()} or {@link #withId(String)} must
         * have been called.</li>
-        * <li>The {@link #from(Sone) sender} must not be {@code null}.</li>
+        * <li>The {@link #from(String) sender} must not be {@code null}.</li>
         * <li>Exactly one of {@link #currentTime()} or {@link #withTime(long)} must
         * have been called.</li>
         * <li>The {@link #withText(String) text} must not be {@code null} and must
         * contain something other than whitespace.</li>
-        * <li>The {@link #to(Sone) recipient} must either not have been set, or it
-        * must have been set to a {@link Sone} other than {@link #from(Sone) the
-        * sender}.</li>
+        * <li>The {@link #to(String) recipient} must either not have been set, or
+        * it must have been set to a {@link Sone} other than {@link #from(String)
+        * the sender}.</li>
         * </ul>
         *
         * @return A new post
index 402b99a..25f6a86 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.data.impl;
 
+import net.pterodactylus.sone.core.SoneProvider;
 import net.pterodactylus.sone.data.PostBuilder;
 import net.pterodactylus.sone.data.PostBuilderFactory;
 
@@ -28,12 +29,25 @@ import net.pterodactylus.sone.data.PostBuilderFactory;
  */
 public class DefaultPostBuilderFactory implements PostBuilderFactory {
 
+       /** The Sone provider. */
+       private final SoneProvider soneProvider;
+
+       /**
+        * Creates a new default post builder factory.
+        *
+        * @param soneProvider
+        *            The Sone provider
+        */
+       public DefaultPostBuilderFactory(SoneProvider soneProvider) {
+               this.soneProvider = soneProvider;
+       }
+
        /**
         * {@inheritDoc}
         */
        @Override
        public PostBuilder newPostBuilder() {
-               return new PostBuilderImpl();
+               return new PostBuilderImpl(soneProvider);
        }
 
 }
index 92b80c5..929f315 100644 (file)
@@ -21,9 +21,9 @@ import static com.google.common.base.Preconditions.checkState;
 
 import java.util.UUID;
 
+import net.pterodactylus.sone.core.SoneProvider;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostBuilder;
-import net.pterodactylus.sone.data.Sone;
 
 import org.apache.commons.lang.StringUtils;
 
@@ -34,6 +34,9 @@ import org.apache.commons.lang.StringUtils;
  */
 public class PostBuilderImpl implements PostBuilder {
 
+       /** The Sone provider for the created posts. */
+       private final SoneProvider soneProvider;
+
        /** Wether to create a post with a random ID. */
        private boolean randomId;
 
@@ -41,7 +44,7 @@ public class PostBuilderImpl implements PostBuilder {
        private String id;
 
        /** The sender of the post. */
-       private Sone sender;
+       private String senderId;
 
        /** Whether to use the current time when creating the post. */
        private boolean currentTime;
@@ -53,7 +56,17 @@ public class PostBuilderImpl implements PostBuilder {
        private String text;
 
        /** The (optional) recipient of the post. */
-       private Sone recipient;
+       private String recipientId;
+
+       /**
+        * Creates a new post builder.
+        *
+        * @param soneProvider
+        *            The Sone provider
+        */
+       public PostBuilderImpl(SoneProvider soneProvider) {
+               this.soneProvider = soneProvider;
+       }
 
        /**
         * {@inheritDoc}
@@ -62,11 +75,11 @@ public class PostBuilderImpl implements PostBuilder {
        public PostBuilder copyPost(Post post) {
                this.randomId = false;
                this.id = post.getId();
-               this.sender = post.getSone();
+               this.senderId = post.getSone().getId();
                this.currentTime = false;
                this.time = post.getTime();
                this.text = post.getText();
-               this.recipient = post.getRecipient();
+               this.recipientId = (post.getRecipient() != null) ? post.getRecipient().getId() : null;
                return this;
        }
 
@@ -92,8 +105,8 @@ public class PostBuilderImpl implements PostBuilder {
         * {@inheritDoc}
         */
        @Override
-       public PostBuilder from(Sone sender) {
-               this.sender = sender;
+       public PostBuilder from(String senderId) {
+               this.senderId = senderId;
                return this;
        }
 
@@ -128,8 +141,8 @@ public class PostBuilderImpl implements PostBuilder {
         * {@inheritDoc}
         */
        @Override
-       public PostBuilder to(Sone recipient) {
-               this.recipient = recipient;
+       public PostBuilder to(String recipientId) {
+               this.recipientId = recipientId;
                return this;
        }
 
@@ -139,11 +152,11 @@ public class PostBuilderImpl implements PostBuilder {
        @Override
        public Post build() {
                checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
-               checkState(sender != null, "sender must not be null");
+               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((recipient == null) || !recipient.equals(sender), "sender and recipient must not be the same");
-               return new PostImpl(randomId ? UUID.randomUUID().toString() : id, sender, recipient, currentTime ? System.currentTimeMillis() : time, text);
+               checkState((recipientId == null) || !recipientId.equals(senderId), "sender and recipient must not be the same");
+               return new PostImpl(soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
        }
 
 }
index 43d7d79..bb8a4df 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.data.impl;
 
 import java.util.UUID;
 
+import net.pterodactylus.sone.core.SoneProvider;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 
@@ -30,14 +31,17 @@ import net.pterodactylus.sone.data.Sone;
  */
 public class PostImpl implements Post {
 
+       /** The Sone provider. */
+       private final SoneProvider soneProvider;
+
        /** The GUID of the post. */
        private final UUID id;
 
-       /** The Sone this post belongs to. */
-       private final Sone sone;
+       /** The ID of the owning Sone. */
+       private final String soneId;
 
-       /** The Sone of the recipient. */
-       private final Sone recipient;
+       /** 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;
@@ -51,21 +55,24 @@ public class PostImpl implements Post {
        /**
         * Creates a new post.
         *
+        * @param soneProvider
+        *            The Sone provider
         * @param id
         *            The ID of the post
-        * @param sone
-        *            The Sone this post belongs to
-        * @param recipient
-        *            The recipient 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(String id, Sone sone, Sone recipient, long time, String text) {
+       public PostImpl(SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
+               this.soneProvider = soneProvider;
                this.id = UUID.fromString(id);
-               this.sone = sone;
-               this.recipient = recipient;
+               this.soneId = soneId;
+               this.recipientId = recipientId;
                this.time = time;
                this.text = text;
        }
@@ -87,7 +94,7 @@ public class PostImpl implements Post {
         */
        @Override
        public Sone getSone() {
-               return sone;
+               return soneProvider.getSone(soneId, false);
        }
 
        /**
@@ -95,7 +102,7 @@ public class PostImpl implements Post {
         */
        @Override
        public Sone getRecipient() {
-               return recipient;
+               return soneProvider.getSone(recipientId, false);
        }
 
        /**
@@ -160,7 +167,7 @@ public class PostImpl implements Post {
         */
        @Override
        public String toString() {
-               return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
+               return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
        }
 
 }