X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FAbstractReplyBuilder.java;h=10a0a5826b74434b4ee3450fe5c6b3832b910e3c;hb=0a4b6fc252003c71f4bdef09560e87982838d9c8;hp=e04e85455b4984d2f61f9f0068f1b1f628de3847;hpb=015c9b8f78af4f1dbcaa97ec3cb77e3c4cb09c84;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java index e04e854..10a0a58 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java @@ -17,8 +17,17 @@ package net.pterodactylus.sone.data.impl; -import net.pterodactylus.sone.data.ReplyBuilder; -import net.pterodactylus.sone.data.Sone; +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.checkState; +import static java.lang.System.currentTimeMillis; +import static java.util.UUID.randomUUID; + +import net.pterodactylus.sone.database.ReplyBuilder; + +import com.google.common.base.Optional; +import org.apache.commons.lang.StringUtils; /** * Abstract implementation of a {@link ReplyBuilder}. @@ -27,84 +36,55 @@ import net.pterodactylus.sone.data.Sone; * The interface implemented and exposed by the builder * @author David ‘Bombe’ Roden */ -public class AbstractReplyBuilder> implements ReplyBuilder { - - /** Whether to use a random ID for the reply. */ - protected boolean randomId; - - /** The ID of the reply. */ - protected String id; - - /** The sender of the reply. */ - protected Sone sender; +public abstract class AbstractReplyBuilder> implements ReplyBuilder { - /** Whether to use the current time when creating the reply. */ - protected boolean currentTime; - - /** The time of the reply. */ - protected long time; - - /** The text of the reply. */ + protected final String senderId; + protected Optional id = absent(); + protected Optional time = absent(); protected String text; - /** - * {@inheritDoc} - */ - @Override - @SuppressWarnings("unchecked") - public B randomId() { - this.randomId = true; - return (B) this; + protected AbstractReplyBuilder(String senderId) { + this.senderId = senderId; } - /** - * {@inheritDoc} - */ @Override @SuppressWarnings("unchecked") public B withId(String id) { - this.id = id; + this.id = fromNullable(id); return (B) this; } - /** - * {@inheritDoc} - */ @Override @SuppressWarnings("unchecked") - public B from(Sone sender) { - this.sender = sender; + public B withTime(long time) { + this.time = of(time); return (B) this; } - /** - * {@inheritDoc} - */ @Override @SuppressWarnings("unchecked") - public B currentTime() { - this.currentTime = true; + public B withText(String text) { + this.text = text; return (B) this; } - /** - * {@inheritDoc} - */ - @Override - @SuppressWarnings("unchecked") - public B withTime(long time) { - this.time = time; - return (B) this; + protected String getId() { + return id.isPresent() ? id.get() : randomUUID().toString(); + } + + protected long getTime() { + return time.isPresent() ? time.get() : currentTimeMillis(); } /** - * {@inheritDoc} + * Validates the state of this post reply builder. + * + * @throws IllegalStateException + * if the state is not valid for building a new post reply */ - @Override - @SuppressWarnings("unchecked") - public B withText(String text) { - this.text = text; - return (B) this; + protected void validate() throws IllegalStateException { + checkState(senderId != null, "sender must not be null"); + checkState(!StringUtils.isBlank(text), "text must not be empty"); } }