return null;
}
PostReplyBuilder postReplyBuilder = postReplyBuilder();
- postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
+ postReplyBuilder.from(sone.getId()).to(post.getId()).withText(text.trim());
final PostReply reply = postReplyBuilder.build();
database.storePostReply(reply);
eventBus.post(new NewPostReplyFoundEvent(reply));
* if the state is not valid for building a new post reply
*/
protected void validate() throws IllegalStateException {
- checkState((randomId && (id == null)) || (!randomId && (id != null)), "either random ID nor custom ID must be set");
checkState(senderId != null, "sender must not be null");
- checkState((currentTime && (time == 0)) || (!currentTime && (time >= 0)), "either current time or custom time must be set");
checkState(!StringUtils.isBlank(text), "text must not be empty");
checkState(postId != null, "post must not be null");
}
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 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}.
*
*/
public class AbstractReplyBuilder<B extends ReplyBuilder<B>> implements ReplyBuilder<B> {
- /** Whether to use a random ID for the reply. */
- protected boolean randomId;
-
- /** The ID of the reply. */
- protected String id;
+ protected Optional<String> id = absent();
/** The sender of the reply. */
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 currentTime() {
- this.currentTime = true;
- return (B) this;
- }
-
- /**
- * {@inheritDoc}
- */
- @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();
+ }
+
}
package net.pterodactylus.sone.data.impl;
-import static com.google.common.base.Preconditions.checkState;
-
-import java.util.UUID;
-
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostReplyBuilder;
-import org.apache.commons.lang.StringUtils;
-
/**
* {@link PostReplyBuilder} implementation that creates {@link PostReplyImpl}
* objects.
*/
@Override
public PostReply build() {
- checkState((randomId && (id == null)) || (!randomId && (id != null)), "either random ID nor custom ID must be set");
- checkState(senderId != null, "sender must not be null");
- checkState((currentTime && (time == 0)) || (!currentTime && (time >= 0)), "either current time or custom time must be set");
- checkState(!StringUtils.isBlank(text), "text must not be empty");
- checkState(postId != null, "post must not be null");
+ validate();
/* create new post reply. */
- return new PostReplyImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, currentTime ? System.currentTimeMillis() : time, text, postId);
+ return new PostReplyImpl(database, getId(), senderId, getTime(), text, postId);
}
}
public interface ReplyBuilder<B extends ReplyBuilder<B>> {
/**
- * Configures this builder to use a random ID when creating the reply. If
- * this method is used, {@link #withId(String)} must not be used.
- *
- * @return This builder
- */
- public B randomId();
-
- /**
* Configures this builder to use the given ID when creating the reply. If
* this method is used, {@link #randomId()} must not be used.
*
public B from(String senderId);
/**
- * Configures this builder to use the current time when creating the reply.
- * If this method is used, {@link #withTime(long)} must not be used.
- *
- * @return This builder
- */
- public B currentTime();
-
- /**
* Configures this builder to use the given time when creating the reply. If
* this method is used, {@link #currentTime()} must not be used.
*
+++ /dev/null
-/*
- * Sone - MemoryPostReplyBuilder.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.database.memory;
-
-import java.util.UUID;
-
-import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.data.impl.AbstractPostReplyBuilder;
-import net.pterodactylus.sone.database.PostReplyBuilder;
-import net.pterodactylus.sone.database.SoneProvider;
-
-/**
- * {@link PostReplyBuilder} implementation that creates {@link MemoryPostReply}
- * objects.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-class MemoryPostReplyBuilder extends AbstractPostReplyBuilder {
-
- /** The database. */
- private final MemoryDatabase database;
-
- /** The Sone provider. */
- private final SoneProvider soneProvider;
-
- /**
- * Creates a new {@link MemoryPostReply} builder.
- *
- * @param database
- * The database
- * @param soneProvider
- * The Sone provider
- */
- public MemoryPostReplyBuilder(MemoryDatabase database, SoneProvider soneProvider) {
- this.database = database;
- this.soneProvider = soneProvider;
- }
-
- /**
- * {@inheritDocs}
- */
- @Override
- public PostReply build() throws IllegalStateException {
- validate();
-
- PostReply postReply = new MemoryPostReply(database, soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, currentTime ? System.currentTimeMillis() : time, text, postId);
- postReply.setKnown(database.isPostReplyKnown(postReply));
- return postReply;
- }
-
-}