}
SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity());
- if (originalSone.isLocal()) {
- soneBuilder = soneBuilder.local();
- }
SimpleXML soneXml;
try {
}
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");
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>();
}
@Override
- public SoneBuilder local() {
- this.local = true;
- return this;
- }
-
- @Override
public SoneBuilder lastUpdated(long lastUpdated) {
this.lastUpdated = lastUpdated;
return this;
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");
+ }
+
}
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;
public interface SoneBuilder {
SoneBuilder from(Identity identity);
- SoneBuilder local();
SoneBuilder lastUpdated(long lastUpdated);
SoneBuilder using(Client client);
SoneBuilder withPostReplies(Collection<PostReply> postReplies);
Sone build() throws IllegalStateException;
+ LocalSone buildLocal() throws IllegalStateException;
}
}
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)))
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.
@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);
}
}
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;
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
@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)
@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)