From: David ‘Bombe’ Roden Date: Tue, 15 Oct 2013 17:02:32 +0000 (+0200) Subject: Move post creation into Sone, remove memory-based post builder. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=027615fa64c78ef5ebc198e852cfd6d39b18aad0;p=Sone.git Move post creation into Sone, remove memory-based post builder. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index b035d58..14667de 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -445,15 +445,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return trustedIdentities.containsEntry(origin.getIdentity(), target.getIdentity()); } - /** - * Returns a post builder. - * - * @return A new post builder - */ - public PostBuilder postBuilder() { - return database.newPostBuilder(); - } - /** {@inheritDoc} */ @Override public Optional getPost(String postId) { @@ -1072,7 +1063,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Invalid post found, aborting load!"); return; } - PostBuilder postBuilder = postBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText); + PostBuilder postBuilder = sone.newPostBuilder().withId(postId).withTime(postTime).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { postBuilder.to(postRecipientId); } @@ -1292,8 +1283,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone)); return null; } - PostBuilder postBuilder = database.newPostBuilder(); - postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim()); + PostBuilder postBuilder = sone.newPostBuilder(); + postBuilder.randomId().withTime(time).withText(text.trim()); if (recipient.isPresent()) { postBuilder.to(recipient.get().getId()); } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 4be836b..c026a37 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -368,9 +368,9 @@ public class SoneDownloader extends AbstractService { return null; } try { - PostBuilder postBuilder = core.postBuilder(); + PostBuilder postBuilder = sone.newPostBuilder(); /* TODO - parse time correctly. */ - postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText); + postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { postBuilder.to(postRecipientId); } diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 67ecccb..41fdcc1 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -28,8 +28,9 @@ import java.util.List; import java.util.Set; import net.pterodactylus.sone.core.Options; -import net.pterodactylus.sone.database.AlbumBuilder; import net.pterodactylus.sone.database.AlbumBuilderFactory; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.PostBuilderFactory; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.template.SoneAccessor; @@ -45,7 +46,7 @@ import com.google.common.primitives.Ints; * * @author David ‘Bombe’ Roden */ -public interface Sone extends Identified, Fingerprintable, AlbumBuilderFactory, Comparable { +public interface Sone extends Identified, Fingerprintable, PostBuilderFactory, AlbumBuilderFactory, Comparable { /** * Enumeration for the possible states of a {@link Sone}. @@ -548,4 +549,6 @@ public interface Sone extends Identified, Fingerprintable, AlbumBuilderFactory, /* TODO - remove this method again, maybe add an option provider */ void setOptions(Options options); + PostBuilder newPostBuilder(); + } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java index 0bc7a59..ad580ee 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java @@ -17,9 +17,11 @@ package net.pterodactylus.sone.data.impl; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.PostBuilder; import org.apache.commons.lang.StringUtils; @@ -32,15 +34,15 @@ import org.apache.commons.lang.StringUtils; */ public abstract class AbstractPostBuilder implements PostBuilder { + protected final Database database; + protected final String senderId; + /** Wether to create a post with a random ID. */ protected boolean randomId; /** The ID of the post. */ protected String id; - /** The sender of the post. */ - protected String senderId; - /** Whether to use the current time when creating the post. */ protected boolean currentTime; @@ -53,6 +55,11 @@ public abstract class AbstractPostBuilder implements PostBuilder { /** The (optional) recipient of the post. */ protected String recipientId; + protected AbstractPostBuilder(Database database, String soneId) { + this.database = checkNotNull(database, "database must not be null"); + this.senderId = checkNotNull(soneId, "sender ID must not be null"); + } + // // POSTBUILDER METHODS // @@ -64,7 +71,6 @@ public abstract class AbstractPostBuilder implements PostBuilder { public PostBuilder copyPost(Post post) { this.randomId = false; this.id = post.getId(); - this.senderId = post.getSone().getId(); this.currentTime = false; this.time = post.getTime(); this.text = post.getText(); @@ -94,15 +100,6 @@ public abstract class AbstractPostBuilder implements PostBuilder { * {@inheritDoc} */ @Override - public PostBuilder from(String senderId) { - this.senderId = senderId; - return this; - } - - /** - * {@inheritDoc} - */ - @Override public PostBuilder currentTime() { currentTime = true; return this; @@ -147,10 +144,9 @@ public abstract class AbstractPostBuilder implements PostBuilder { */ protected void validate() throws IllegalStateException { checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set"); - 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((recipientId == null) || !recipientId.equals(senderId), "sender and recipient must not be the same"); + checkState(!senderId.equals(recipientId), "sender and recipient must not be the same"); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java index 9ac82c8..3ae2f6e 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java @@ -30,15 +30,13 @@ import net.pterodactylus.sone.database.PostBuilder; */ public class DefaultPostBuilder extends AbstractPostBuilder { - private final Database database; - /** * Creates a new post builder. * * @param database */ - public DefaultPostBuilder(Database database) { - this.database = database; + public DefaultPostBuilder(Database database, String soneId) { + super(database, soneId); } /** {@inheritDoc} */ diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java deleted file mode 100644 index 83548bd..0000000 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Sone - DefaultPostBuilderFactory.java - Copyright © 2013 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.data.impl; - -import net.pterodactylus.sone.database.Database; -import net.pterodactylus.sone.database.PostBuilder; -import net.pterodactylus.sone.database.PostBuilderFactory; - -import com.google.inject.Inject; - -/** - * {@link PostBuilderFactory} implementation that creates {@link - * DefaultPostBuilder}s. - * - * @author David ‘Bombe’ Roden - */ -public class DefaultPostBuilderFactory implements PostBuilderFactory { - - private final Database database; - - /** - * Creates a new default post builder factory. - * - * @param database - * The database - */ - @Inject - public DefaultPostBuilderFactory(Database database) { - this.database = database; - } - - /** {@inheritDoc} */ - @Override - public PostBuilder newPostBuilder() { - return new DefaultPostBuilder(database); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index 87ad6bf..213c266 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -38,6 +38,7 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.AlbumBuilder; import net.pterodactylus.sone.database.Database; +import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.util.logging.Logging; @@ -676,6 +677,10 @@ public class DefaultSone implements Sone { return new DefaultAlbumBuilder(database, this, rootAlbum.getId()); } + public PostBuilder newPostBuilder() { + return new DefaultPostBuilder(database, getId()); + } + // // FINGERPRINTABLE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/database/PostBuilder.java b/src/main/java/net/pterodactylus/sone/database/PostBuilder.java index 449147a..c46bd69 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/PostBuilder.java @@ -54,15 +54,6 @@ public interface PostBuilder { public PostBuilder copyPost(Post post) throws NullPointerException; /** - * Configures this builder to use the given Sone as sender of the new post. - * - * @param senderId - * The ID of the sender of the post - * @return This post builder - */ - public PostBuilder from(String senderId); - - /** * Configures this builder to use a random ID for the new post. If this * method is used, {@link #withId(String)} must not be used. * diff --git a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java index 40e6290..0832807 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java @@ -23,7 +23,7 @@ package net.pterodactylus.sone.database; * * @author David ‘Bombe’ Roden */ -public interface PostDatabase extends PostProvider, PostBuilderFactory, PostStore { +public interface PostDatabase extends PostProvider, PostStore { /* nothing here. */ 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 9a2637d..d1ceee9 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -242,16 +242,6 @@ public class MemoryDatabase extends AbstractService implements Database { } // - // POSTBUILDERFACTORY METHODS - // - - /** {@inheritDocs} */ - @Override - public PostBuilder newPostBuilder() { - return new MemoryPostBuilder(this); - } - - // // POSTSTORE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostBuilder.java deleted file mode 100644 index a7afcdc..0000000 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostBuilder.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Sone - MemoryPostBuilder.java - Copyright © 2013 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.database.memory; - -import java.util.UUID; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.impl.AbstractPostBuilder; -import net.pterodactylus.sone.database.PostBuilder; -import net.pterodactylus.sone.database.SoneProvider; - -/** - * {@link PostBuilder} implementation that creates a {@link MemoryPost}. - * - * @author David ‘Bombe’ Roden - */ -class MemoryPostBuilder extends AbstractPostBuilder { - - /** The database. */ - private final MemoryDatabase database; - - /** - * Creates a new memory post builder. - * - * @param memoryDatabase - * The database - * @param soneProvider - * The Sone provider - */ - public MemoryPostBuilder(MemoryDatabase memoryDatabase) { - database = memoryDatabase; - } - - /** - * {@inheritDocs} - */ - @Override - public Post build() throws IllegalStateException { - validate(); - Post post = new MemoryPost(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text); - post.setKnown(database.isPostKnown(post)); - return post; - } - -} diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index e31bbf5..4813028 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -226,7 +226,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr bind(SonePlugin.class).toInstance(SonePlugin.this); bind(FcpInterface.class).in(Singleton.class); bind(Database.class).to(MemoryDatabase.class); - bind(PostBuilderFactory.class).to(MemoryDatabase.class); bind(PostReplyBuilderFactory.class).to(MemoryDatabase.class); bind(SoneProvider.class).to(Core.class).in(Singleton.class); bind(PostProvider.class).to(MemoryDatabase.class);