From f8672b1385173a103d7f085d8e9cd43bc5762d71 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 7 Dec 2014 19:27:20 +0100 Subject: [PATCH] Use different method to create a local Sone. --- .../java/net/pterodactylus/sone/core/SoneParser.java | 5 +---- .../sone/data/impl/AbstractSoneBuilder.java | 15 ++++++--------- .../net/pterodactylus/sone/database/SoneBuilder.java | 3 ++- .../sone/database/memory/MemoryDatabase.java | 4 ++-- .../sone/database/memory/MemorySoneBuilder.java | 14 ++++++++++++-- .../sone/data/impl/AbstractSoneBuilderTest.java | 17 ++++++++++++----- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 6056a7f..218a368 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -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"); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java index a2f1333..5ec8ec4 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java @@ -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 posts = new HashSet(); @@ -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"); + } + } diff --git a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java index 5fb5fed..35a49a4 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java @@ -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 postReplies); Sone build() throws IllegalStateException; + LocalSone buildLocal() throws IllegalStateException; } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 330f588..8815abc 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -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))) diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java index 3cdb120..e07999d 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java @@ -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); } } diff --git a/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java b/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java index 8c9e1a9..927148f 100644 --- a/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java +++ b/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java @@ -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) -- 2.7.4