X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FAbstractPostBuilder.java;h=65dd1bd72722c32ab1b8c11d4c9eb81f0077456a;hb=cf032630b167166215f45d8ae2e5147e1934c223;hp=0bc7a59fbe7ac7b49d3d6bd0f20d7a89e7e68629;hpb=ed19f10e7931fb0b09c042c6070e37dffb23ceac;p=Sone.git 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..65dd1bd 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java @@ -17,11 +17,15 @@ package net.pterodactylus.sone.data.impl; +import static com.google.common.base.Optional.absent; +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 com.google.common.base.Optional; import org.apache.commons.lang.StringUtils; /** @@ -32,15 +36,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; @@ -51,7 +55,12 @@ public abstract class AbstractPostBuilder implements PostBuilder { protected String text; /** The (optional) recipient of the post. */ - protected String recipientId; + protected Optional recipientId = absent(); + + 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 @@ -61,21 +70,6 @@ public abstract class AbstractPostBuilder implements PostBuilder { * {@inheritDoc} */ @Override - 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(); - this.recipientId = post.getRecipientId().orNull(); - return this; - } - - /** - * {@inheritDoc} - */ - @Override public PostBuilder randomId() { randomId = true; return this; @@ -94,15 +88,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; @@ -130,7 +115,7 @@ public abstract class AbstractPostBuilder implements PostBuilder { * {@inheritDoc} */ @Override - public PostBuilder to(String recipientId) { + public PostBuilder to(Optional recipientId) { this.recipientId = recipientId; return this; } @@ -147,10 +132,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(!recipientId.isPresent() || !senderId.equals(recipientId.get()), "sender and recipient must not be the same"); } }