import static com.google.common.base.Preconditions.checkState;
-import org.apache.commons.lang.StringUtils;
-
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.database.PostBuilder;
-import net.pterodactylus.sone.database.SoneProvider;
+
+import org.apache.commons.lang.StringUtils;
/**
* Abstract {@link PostBuilder} implementation. It stores the state of the new
*/
public abstract class AbstractPostBuilder implements PostBuilder {
- /** The Sone provider for the created posts. */
- protected final SoneProvider soneProvider;
-
/** Wether to create a post with a random ID. */
protected boolean randomId;
/** The (optional) recipient of the post. */
protected String recipientId;
- /**
- * Creates a new abstract post builder.
- *
- * @param soneProvider
- * The Sone provider
- */
- public AbstractPostBuilder(SoneProvider soneProvider) {
- this.soneProvider = soneProvider;
- }
-
//
// POSTBUILDER METHODS
//
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.SoneProvider;
import com.google.common.base.Optional;
class MemoryPost implements Post {
/** The post database. */
- private final MemoryDatabase postDatabase;
-
- /** The Sone provider. */
- private final SoneProvider soneProvider;
+ private final MemoryDatabase memoryDatabase;
/** The GUID of the post. */
private final UUID id;
/**
* Creates a new post.
*
- * @param postDatabase
- * The post database
- * @param soneProvider
- * The Sone provider
+ * @param memoryDatabase
+ * The database
* @param id
- * The ID of the post
+ * The ID of the post
* @param soneId
- * The ID of the Sone this post belongs to
+ * The ID of the Sone this post belongs to
* @param recipientId
- * The ID of the recipient of the post
+ * The ID of the recipient of the post
* @param time
- * The time of the post (in milliseconds since Jan 1, 1970 UTC)
+ * The time of the post (in milliseconds since Jan 1, 1970 UTC)
* @param text
- * The text of the post
+ * The text of the post
*/
- public MemoryPost(MemoryDatabase postDatabase, SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
- this.postDatabase = postDatabase;
- this.soneProvider = soneProvider;
+ public MemoryPost(MemoryDatabase memoryDatabase, String id, String soneId, String recipientId, long time, String text) {
+ this.memoryDatabase = memoryDatabase;
this.id = UUID.fromString(id);
this.soneId = soneId;
this.recipientId = recipientId;
// ACCESSORS
//
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public String getId() {
return id.toString();
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public Sone getSone() {
- return soneProvider.getSone(soneId).get();
+ return memoryDatabase.getSone(soneId).get();
}
- /**
- * {@inheritDocs}
- */
+ /** {@inheritDocs} */
@Override
public Optional<String> getRecipientId() {
return Optional.fromNullable(recipientId);
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public Optional<Sone> getRecipient() {
- return soneProvider.getSone(recipientId);
+ return memoryDatabase.getSone(recipientId);
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public long getTime() {
return time;
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public String getText() {
return text;
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public boolean isKnown() {
- return postDatabase.isPostKnown(this);
+ return memoryDatabase.isPostKnown(this);
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public MemoryPost setKnown(boolean known) {
- postDatabase.setPostKnown(this, known);
+ memoryDatabase.setPostKnown(this, known);
return this;
}
// OBJECT METHODS
//
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public int hashCode() {
return id.hashCode();
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public boolean equals(Object object) {
if (!(object instanceof MemoryPost)) {
return post.id.equals(id);
}
- /**
- * {@inheritDoc}
- */
+ /** {@inheritDoc} */
@Override
public String toString() {
return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
* @param soneProvider
* The Sone provider
*/
- public MemoryPostBuilder(MemoryDatabase memoryDatabase, SoneProvider soneProvider) {
- super(soneProvider);
+ public MemoryPostBuilder(MemoryDatabase memoryDatabase) {
database = memoryDatabase;
}
@Override
public Post build() throws IllegalStateException {
validate();
- Post post = new MemoryPost(database, soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
+ Post post = new MemoryPost(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
post.setKnown(database.isPostKnown(post));
return post;
}