From 550219212ea7809a6575b9d6bbe81030cb6f8618 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 15 Oct 2013 22:21:07 +0200 Subject: [PATCH] Remove randomId() and currentTime() methods from ReplyBuilder. --- .../java/net/pterodactylus/sone/core/Core.java | 2 +- .../sone/data/impl/AbstractPostReplyBuilder.java | 2 - .../sone/data/impl/AbstractReplyBuilder.java | 52 +++++++---------- .../sone/data/impl/PostReplyBuilderImpl.java | 14 +---- .../pterodactylus/sone/database/ReplyBuilder.java | 16 ------ .../database/memory/MemoryPostReplyBuilder.java | 66 ---------------------- 6 files changed, 23 insertions(+), 129 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReplyBuilder.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 8c9bc77..e550652 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1316,7 +1316,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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)); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java index 952a94e..05e5ccc 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java @@ -55,9 +55,7 @@ public abstract class AbstractPostReplyBuilder extends AbstractReplyBuilder= 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"); } 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 2a68a13..5c1b976 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,16 @@ 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}. * @@ -28,20 +36,12 @@ import net.pterodactylus.sone.database.ReplyBuilder; */ 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; + protected Optional 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 time = absent(); /** The text of the reply. */ protected String text; @@ -51,18 +51,8 @@ public class AbstractReplyBuilder> implements ReplyBui */ @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; } @@ -81,18 +71,8 @@ public class AbstractReplyBuilder> implements ReplyBui */ @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; } @@ -106,4 +86,12 @@ public class AbstractReplyBuilder> implements ReplyBui return (B) this; } + protected String getId() { + return id.isPresent() ? id.get() : randomUUID().toString(); + } + + protected long getTime() { + return time.isPresent() ? time.get() : currentTimeMillis(); + } + } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java index 4b43aa7..5c7f494 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java @@ -17,16 +17,10 @@ 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. @@ -46,14 +40,10 @@ public class PostReplyBuilderImpl extends AbstractPostReplyBuilder { */ @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); } } diff --git a/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java b/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java index d83e7ce..a04a09c 100644 --- a/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java @@ -31,14 +31,6 @@ import net.pterodactylus.sone.data.Sone; public interface ReplyBuilder> { /** - * 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. * @@ -59,14 +51,6 @@ public interface ReplyBuilder> { 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. * diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReplyBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReplyBuilder.java deleted file mode 100644 index 32ab1e0..0000000 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReplyBuilder.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -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; - } - -} -- 2.7.4