Use different method to create a local Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 7 Dec 2014 18:27:20 +0000 (19:27 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 7 Dec 2014 18:27:20 +0000 (19:27 +0100)
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java
src/main/java/net/pterodactylus/sone/database/SoneBuilder.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java
src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java

index 6056a7f..218a368 100644 (file)
@@ -59,9 +59,6 @@ public class SoneParser {
                }
 
                SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity());
-               if (originalSone.isLocal()) {
-                       soneBuilder = soneBuilder.local();
-               }
 
                SimpleXML soneXml;
                try {
@@ -222,7 +219,7 @@ public class SoneParser {
                        }
                        soneBuilder.withPostReplies(replies);
                }
-               Sone sone = soneBuilder.build();
+               Sone sone = originalSone.isLocal() ? soneBuilder.buildLocal() : soneBuilder.build();
 
                /* parse liked post IDs. */
                SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
index a2f1333..5ec8ec4 100644 (file)
@@ -20,7 +20,6 @@ import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 public abstract class AbstractSoneBuilder implements SoneBuilder {
 
        protected Identity identity;
-       protected boolean local;
        protected long lastUpdated;
        protected Client client;
        protected final Collection<Post> posts = new HashSet<Post>();
@@ -33,12 +32,6 @@ public abstract class AbstractSoneBuilder implements SoneBuilder {
        }
 
        @Override
-       public SoneBuilder local() {
-               this.local = true;
-               return this;
-       }
-
-       @Override
        public SoneBuilder lastUpdated(long lastUpdated) {
                this.lastUpdated = lastUpdated;
                return this;
@@ -66,10 +59,14 @@ public abstract class AbstractSoneBuilder implements SoneBuilder {
 
        protected void validate() throws IllegalStateException {
                checkState(identity != null, "identity must not be null");
-               checkState(!local || (identity instanceof OwnIdentity),
-                               "can not create local Sone from remote identity");
                checkState(lastUpdated > 0, "last update time must be set");
                checkState(client != null, "client must not be null");
        }
 
+       protected void validateLocal() throws IllegalStateException {
+               validate();
+               checkState(identity instanceof OwnIdentity,
+                               "identity must be an own identity for a local Sone");
+       }
+
 }
index 5fb5fed..35a49a4 100644 (file)
@@ -3,6 +3,7 @@ package net.pterodactylus.sone.database;
 import java.util.Collection;
 
 import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.LocalSone;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
@@ -16,7 +17,6 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 public interface SoneBuilder {
 
        SoneBuilder from(Identity identity);
-       SoneBuilder local();
 
        SoneBuilder lastUpdated(long lastUpdated);
        SoneBuilder using(Client client);
@@ -25,5 +25,6 @@ public interface SoneBuilder {
        SoneBuilder withPostReplies(Collection<PostReply> postReplies);
 
        Sone build() throws IllegalStateException;
+       LocalSone buildLocal() throws IllegalStateException;
 
 }
index 330f588..8815abc 100644 (file)
@@ -182,8 +182,8 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        private LocalSone loadLocalSone(OwnIdentity ownIdentity) {
-               LocalSone localSone = (LocalSone) newSoneBuilder().local().from(ownIdentity).using(
-                               new Client("Sone", SonePlugin.VERSION.toString())).build();
+               LocalSone localSone = newSoneBuilder().from(ownIdentity).using(
+                               new Client("Sone", SonePlugin.VERSION.toString())).buildLocal();
                localSone.setLatestEdition(
                                Optional.fromNullable(
                                                Longs.tryParse(ownIdentity.getProperty(LATEST_EDITION_PROPERTY)))
index 3cdb120..e07999d 100644 (file)
@@ -1,9 +1,13 @@
 package net.pterodactylus.sone.database.memory;
 
+import net.pterodactylus.sone.data.LocalSone;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.impl.SoneImpl;
 import net.pterodactylus.sone.data.impl.AbstractSoneBuilder;
+import net.pterodactylus.sone.data.impl.SoneImpl;
 import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+
+import com.google.common.base.Preconditions;
 
 /**
  * Memory-based {@link AbstractSoneBuilder} implementation.
@@ -21,7 +25,13 @@ public class MemorySoneBuilder extends AbstractSoneBuilder {
        @Override
        public Sone build() throws IllegalStateException {
                validate();
-               return new SoneImpl(database, identity, local, lastUpdated, client, posts, postReplies);
+               return new SoneImpl(database, identity, false, lastUpdated, client, posts, postReplies);
+       }
+
+       @Override
+       public LocalSone buildLocal() throws IllegalStateException {
+               validateLocal();
+               return new SoneImpl(database, identity, true, lastUpdated, client, posts, postReplies);
        }
 
 }
index 8c9e1a9..927148f 100644 (file)
@@ -3,6 +3,7 @@ package net.pterodactylus.sone.data.impl;
 import static org.mockito.Mockito.mock;
 
 import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.LocalSone;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
@@ -22,24 +23,30 @@ public class AbstractSoneBuilderTest {
                        validate();
                        return null;
                }
+
+               @Override
+               public LocalSone buildLocal() throws IllegalStateException {
+                       validateLocal();
+                       return null;
+               }
        };
        private final Client client = new Client("Test Client", "1.0");
 
        @Test
        public void localSoneIsValidated() {
                Identity ownIdentity = mock(OwnIdentity.class);
-               soneBuilder.local().from(ownIdentity).lastUpdated(1).using(client).build();
+               soneBuilder.from(ownIdentity).lastUpdated(1).using(client).buildLocal();
        }
 
        @Test(expected = IllegalStateException.class)
        public void localSoneIsNotValidatedIfIdentityIsNotAnOwnIdentity() {
                Identity identity = mock(Identity.class);
-               soneBuilder.local().from(identity).lastUpdated(1).using(client).build();
+               soneBuilder.from(identity).lastUpdated(1).using(client).buildLocal();
        }
 
        @Test(expected = IllegalStateException.class)
        public void localSoneIsNotValidatedIfIdentityIsNull() {
-               soneBuilder.local().lastUpdated(1).using(client).build();
+               soneBuilder.lastUpdated(1).using(client).buildLocal();
        }
 
        @Test
@@ -56,7 +63,7 @@ public class AbstractSoneBuilderTest {
        @Test(expected = IllegalStateException.class)
        public void localSoneIsNotValidatedWithoutUpdateTime() {
                Identity identity = mock(OwnIdentity.class);
-               soneBuilder.from(identity).local().using(client).build();
+               soneBuilder.from(identity).using(client).buildLocal();
        }
 
        @Test(expected = IllegalStateException.class)
@@ -68,7 +75,7 @@ public class AbstractSoneBuilderTest {
        @Test(expected = IllegalStateException.class)
        public void localSoneIsNotValidatedWithoutClient() {
                Identity identity = mock(OwnIdentity.class);
-               soneBuilder.from(identity).local().lastUpdated(1L).build();
+               soneBuilder.from(identity).lastUpdated(1L).buildLocal();
        }
 
        @Test(expected = IllegalStateException.class)