X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FAbstractPostBuilder.java;h=650455867802358ea884f3e7140cf288bca9f9f5;hb=f10d40f746f6c7c716f783da11791d28c1117447;hp=3a56dc99850de79a3b4710c9a25ab1595f19aa4f;hpb=6f019de1d4d9742981d851ac3c9097cca8bff58e;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 3a56dc9..6504558 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java @@ -17,13 +17,19 @@ package net.pterodactylus.sone.data.impl; +import static com.google.common.base.Optional.absent; +import static com.google.common.base.Optional.fromNullable; +import static com.google.common.base.Optional.of; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static java.lang.System.currentTimeMillis; +import static java.util.UUID.randomUUID; -import org.apache.commons.lang.StringUtils; - -import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.PostBuilder; -import net.pterodactylus.sone.database.SoneProvider; + +import com.google.common.base.Optional; +import org.apache.commons.lang.StringUtils; /** * Abstract {@link PostBuilder} implementation. It stores the state of the new @@ -33,118 +39,50 @@ import net.pterodactylus.sone.database.SoneProvider; */ 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; + protected final Database database; + protected final String senderId; /** 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; + protected Optional id = absent(); /** The time of the post. */ - protected long time; + protected Optional time = absent(); /** The text of the post. */ protected String text; /** The (optional) recipient of the post. */ - protected String recipientId; + protected Optional recipientId = absent(); - /** - * Creates a new abstract post builder. - * - * @param soneProvider - * The Sone provider - */ - public AbstractPostBuilder(SoneProvider soneProvider) { - this.soneProvider = soneProvider; + 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 // - /** - * {@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; - } - - /** - * {@inheritDoc} - */ @Override public PostBuilder withId(String id) { - this.id = id; - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public PostBuilder from(String senderId) { - this.senderId = senderId; + this.id = fromNullable(id); return this; } - /** - * {@inheritDoc} - */ - @Override - public PostBuilder currentTime() { - currentTime = true; - return this; - } - - /** - * {@inheritDoc} - */ @Override public PostBuilder withTime(long time) { - this.time = time; + this.time = of(time); return this; } - /** - * {@inheritDoc} - */ @Override public PostBuilder withText(String text) { this.text = text; return this; } - /** - * {@inheritDoc} - */ @Override - public PostBuilder to(String recipientId) { + public PostBuilder to(Optional recipientId) { this.recipientId = recipientId; return this; } @@ -153,6 +91,14 @@ public abstract class AbstractPostBuilder implements PostBuilder { // PROTECTED METHODS // + protected String getId() { + return id.isPresent() ? id.get() : randomUUID().toString(); + } + + protected long getTime() { + return time.isPresent() ? time.get() : currentTimeMillis(); + } + /** * Validates the state of this post builder. * @@ -160,11 +106,8 @@ public abstract class AbstractPostBuilder implements PostBuilder { * if the state is not valid for building a new post */ 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"); } }