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<Post> getPost(String postId) {
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);
}
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());
}
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);
}
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;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface Sone extends Identified, Fingerprintable, AlbumBuilderFactory, Comparable<Sone> {
+public interface Sone extends Identified, Fingerprintable, PostBuilderFactory, AlbumBuilderFactory, Comparable<Sone> {
/**
* Enumeration for the possible states of a {@link Sone}.
/* TODO - remove this method again, maybe add an option provider */
void setOptions(Options options);
+ PostBuilder newPostBuilder();
+
}
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;
*/
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;
/** 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
//
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();
* {@inheritDoc}
*/
@Override
- public PostBuilder from(String senderId) {
- this.senderId = senderId;
- return this;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
public PostBuilder currentTime() {
currentTime = true;
return this;
*/
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");
}
}
*/
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} */
+++ /dev/null
-/*
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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);
- }
-
-}
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;
return new DefaultAlbumBuilder(database, this, rootAlbum.getId());
}
+ public PostBuilder newPostBuilder() {
+ return new DefaultPostBuilder(database, getId());
+ }
+
//
// FINGERPRINTABLE METHODS
//
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.
*
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface PostDatabase extends PostProvider, PostBuilderFactory, PostStore {
+public interface PostDatabase extends PostProvider, PostStore {
/* nothing here. */
}
//
- // POSTBUILDERFACTORY METHODS
- //
-
- /** {@inheritDocs} */
- @Override
- public PostBuilder newPostBuilder() {
- return new MemoryPostBuilder(this);
- }
-
- //
// POSTSTORE METHODS
//
+++ /dev/null
-/*
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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;
- }
-
-}
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);