From 8d49fb185ee26853262a12f6d419ef2e39c1116c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 15 Oct 2013 21:58:22 +0200 Subject: [PATCH] =?utf8?q?Remove=20randomId()=20and=20currentTime()=20from?= =?utf8?q?=20post=20builder,=20it=E2=80=99s=20the=20default.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/data/impl/AbstractPostBuilder.java | 47 ++++++++-------------- .../sone/data/impl/DefaultPostBuilder.java | 4 +- .../pterodactylus/sone/database/PostBuilder.java | 16 -------- .../pterodactylus/sone/fcp/CreatePostCommand.java | 2 +- .../net/pterodactylus/sone/web/CreatePostPage.java | 2 +- .../sone/web/ajax/CreatePostAjaxPage.java | 2 +- 6 files changed, 20 insertions(+), 53 deletions(-) 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 65dd1bd..1e45609 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java @@ -18,10 +18,13 @@ 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 net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.PostBuilder; @@ -39,17 +42,11 @@ public abstract class AbstractPostBuilder implements PostBuilder { protected final Database database; protected final String senderId; - /** Wether to create a post with a random ID. */ - protected boolean randomId; - /** The ID of the post. */ - protected String id; - - /** 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; @@ -70,26 +67,8 @@ public abstract class AbstractPostBuilder implements PostBuilder { * {@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 currentTime() { - currentTime = true; + this.id = fromNullable(id); return this; } @@ -98,7 +77,7 @@ public abstract class AbstractPostBuilder implements PostBuilder { */ @Override public PostBuilder withTime(long time) { - this.time = time; + this.time = of(time); return this; } @@ -124,6 +103,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. * @@ -131,8 +118,6 @@ 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((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.isPresent() || !senderId.equals(recipientId.get()), "sender and recipient must not be the same"); } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java index a07f144..e73e293 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java @@ -17,8 +17,6 @@ package net.pterodactylus.sone.data.impl; -import java.util.UUID; - import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.PostBuilder; @@ -45,7 +43,7 @@ public class DefaultPostBuilder extends AbstractPostBuilder { @Override public Post build(Optional postCreated) { validate(); - PostImpl post = new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId.orNull(), currentTime ? System.currentTimeMillis() : time, text); + PostImpl post = new PostImpl(database, getId(), senderId, recipientId.orNull(), getTime(), text); if (postCreated.isPresent()) { postCreated.get().postCreated(post); } diff --git a/src/main/java/net/pterodactylus/sone/database/PostBuilder.java b/src/main/java/net/pterodactylus/sone/database/PostBuilder.java index 5564a93..775378c 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/PostBuilder.java @@ -45,14 +45,6 @@ import com.google.common.base.Optional; public interface PostBuilder { /** - * Configures this builder to use a random ID for the new post. If this - * method is used, {@link #withId(String)} must not be used. - * - * @return This post builder - */ - public PostBuilder randomId(); - - /** * Configures this builder to use the given ID as ID for the new post. If * this method is used, {@link #randomId()} must not be used. * @@ -63,14 +55,6 @@ public interface PostBuilder { public PostBuilder withId(String id); /** - * Configures this builder to use the current time when creating the post. - * If this method is used, {@link #withTime(long)} must not be used. - * - * @return This post builder - */ - public PostBuilder currentTime(); - - /** * Configures the builder to use the given time as time for the new post. If * this method is used, {@link #currentTime()} must not be used. * diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java index 81dff2c..01b52aa 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java @@ -61,7 +61,7 @@ public class CreatePostCommand extends AbstractSoneCommand { if (sone.equals(recipient)) { return new ErrorResponse("Sone and Recipient must not be the same."); } - Post post = sone.newPostBuilder().randomId().currentTime().to(fromNullable(recipient).transform(GET_ID)).withText(text).build(of(getCore().postCreated())); + Post post = sone.newPostBuilder().to(fromNullable(recipient).transform(GET_ID)).withText(text).build(of(getCore().postCreated())); return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get()); } diff --git a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java index 647b3ae..73cd097 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java @@ -72,7 +72,7 @@ public class CreatePostPage extends SoneTemplatePage { } Optional recipient = webInterface.getCore().getSone(recipientId); text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(Identified.GET_ID)).withText(text).build(of(webInterface.getCore().postCreated())); + sender.get().newPostBuilder().to(recipient.transform(Identified.GET_ID)).withText(text).build(of(webInterface.getCore().postCreated())); throw new RedirectException(returnPage); } templateContext.set("errorTextEmpty", true); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java index 7941a53..536d4fb 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java @@ -66,7 +66,7 @@ public class CreatePostAjaxPage extends JsonPage { return createErrorJsonObject("text-required"); } text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - Post newPost = sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(GET_ID)).withText(text).build(of(webInterface.getCore().postCreated())); + Post newPost = sender.get().newPostBuilder().to(recipient.transform(GET_ID)).withText(text).build(of(webInterface.getCore().postCreated())); return createSuccessJsonObject().put("postId", newPost.getId()).put("sone", sender.get().getId()).put("recipient", newPost.getRecipientId().orNull()); } -- 2.7.4