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 java.lang.System.currentTimeMillis;
+import static java.util.UUID.randomUUID;
+
+import net.pterodactylus.sone.database.ReplyBuilder;
+
+import com.google.common.base.Optional;
/**
* Abstract implementation of a {@link ReplyBuilder}.
*
- * @param <C>
- * The concrete implementation of the builder
* @param <B>
* The interface implemented and exposed by the builder
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class AbstractReplyBuilder<C extends AbstractReplyBuilder<C, B>, B extends ReplyBuilder<B>> implements ReplyBuilder<B> {
-
- /** Whether to use a random ID for the reply. */
- protected boolean randomId;
+public class AbstractReplyBuilder<B extends ReplyBuilder<B>> implements ReplyBuilder<B> {
- /** The ID of the reply. */
- protected String id;
+ protected Optional<String> id = absent();
/** The sender of the reply. */
- protected Sone sender;
+ protected String senderId;
- /** Whether to use the current time when creating the reply. */
- protected boolean currentTime;
-
- /** The time of the reply. */
- protected long time;
+ protected Optional<Long> time = absent();
/** The text of the reply. */
protected String text;
*/
@Override
@SuppressWarnings("unchecked")
- public B randomId() {
- this.randomId = true;
- return (B) this;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- @SuppressWarnings("unchecked")
public B withId(String id) {
- this.id = id;
+ this.id = fromNullable(id);
return (B) this;
}
*/
@Override
@SuppressWarnings("unchecked")
- public B from(Sone sender) {
- this.sender = sender;
- return (B) this;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- @SuppressWarnings("unchecked")
- public B currentTime() {
- this.currentTime = true;
+ public B from(String senderId) {
+ this.senderId = senderId;
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public B withTime(long time) {
- this.time = time;
+ this.time = of(time);
return (B) this;
}
return (B) this;
}
+ protected String getId() {
+ return id.isPresent() ? id.get() : randomUUID().toString();
+ }
+
+ protected long getTime() {
+ return time.isPresent() ? time.get() : currentTimeMillis();
+ }
+
}