From c50bfe2d891013b8da5539acf2881da5f60da231 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 27 Jan 2013 11:53:12 +0100 Subject: [PATCH] Extract an abstract post builder. --- .../sone/data/impl/AbstractPostBuilder.java | 170 +++++++++++++++++++++ .../sone/data/impl/PostBuilderImpl.java | 116 +------------- 2 files changed, 173 insertions(+), 113 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java new file mode 100644 index 0000000..342ea5c --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostBuilder.java @@ -0,0 +1,170 @@ +/* + * Sone - AbstractPostBuilder.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.data.impl; + +import static com.google.common.base.Preconditions.checkState; + +import org.apache.commons.lang.StringUtils; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.SoneProvider; + +/** + * Abstract {@link PostBuilder} implementation. It stores the state of the new + * post and performs validation, you only need to implement {@link #build()}. + * + * @author David ‘Bombe’ Roden + */ +public abstract class AbstractPostBuilder implements PostBuilder { + + /** The Sone provider for the created posts. */ + protected final SoneProvider soneProvider; + + /** Wether to create a post with a random ID. */ + protected boolean randomId; + + /** The ID of the post. */ + protected String id; + + /** The sender of the post. */ + protected String senderId; + + /** Whether to use the current time when creating the post. */ + protected boolean currentTime; + + /** The time of the post. */ + protected long time; + + /** The text of the post. */ + protected String text; + + /** The (optional) recipient of the post. */ + protected String recipientId; + + /** + * Creates a new abstract post builder. + * + * @param soneProvider + * The Sone provider + */ + public AbstractPostBuilder(SoneProvider soneProvider) { + this.soneProvider = soneProvider; + } + + // + // POSTBUILDER METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder copyPost(Post post) { + this.randomId = false; + this.id = post.getId(); + this.senderId = post.getSone().getId(); + this.currentTime = false; + this.time = post.getTime(); + this.text = post.getText(); + this.recipientId = (post.getRecipient() != null) ? post.getRecipient().getId() : null; + return this; + } + + /** + * {@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 from(String senderId) { + this.senderId = senderId; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder currentTime() { + currentTime = true; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder withTime(long time) { + this.time = time; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder withText(String text) { + this.text = text; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder to(String recipientId) { + this.recipientId = recipientId; + return this; + } + + // + // PROTECTED METHODS + // + + /** + * Validates the state of this post builder. + * + * @throws IllegalStateException + * 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(senderId != null, "sender must not be null"); + 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 == null) || !recipientId.equals(senderId), "sender and recipient must not be the same"); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java index 6713827..88a2d71 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java @@ -17,46 +17,18 @@ package net.pterodactylus.sone.data.impl; -import static com.google.common.base.Preconditions.checkState; - import java.util.UUID; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.SoneProvider; -import org.apache.commons.lang.StringUtils; - /** * {@link PostBuilder} implementation that creates {@link PostImpl} objects. * * @author David ‘Bombe’ Roden */ -public class PostBuilderImpl implements PostBuilder { - - /** The Sone provider for the created posts. */ - private final SoneProvider soneProvider; - - /** Wether to create a post with a random ID. */ - private boolean randomId; - - /** The ID of the post. */ - private String id; - - /** The sender of the post. */ - private String senderId; - - /** Whether to use the current time when creating the post. */ - private boolean currentTime; - - /** The time of the post. */ - private long time; - - /** The text of the post. */ - private String text; - - /** The (optional) recipient of the post. */ - private String recipientId; +public class PostBuilderImpl extends AbstractPostBuilder { /** * Creates a new post builder. @@ -65,85 +37,7 @@ public class PostBuilderImpl implements PostBuilder { * The Sone provider */ public PostBuilderImpl(SoneProvider soneProvider) { - this.soneProvider = soneProvider; - } - - /** - * {@inheritDoc} - */ - @Override - public PostBuilder copyPost(Post post) { - this.randomId = false; - this.id = post.getId(); - this.senderId = post.getSone().getId(); - this.currentTime = false; - this.time = post.getTime(); - this.text = post.getText(); - this.recipientId = (post.getRecipient() != null) ? post.getRecipient().getId() : null; - return this; - } - - /** - * {@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 from(String senderId) { - this.senderId = senderId; - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public PostBuilder currentTime() { - currentTime = true; - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public PostBuilder withTime(long time) { - this.time = time; - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public PostBuilder withText(String text) { - this.text = text; - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public PostBuilder to(String recipientId) { - this.recipientId = recipientId; - return this; + super(soneProvider); } /** @@ -151,11 +45,7 @@ public class PostBuilderImpl implements PostBuilder { */ @Override public Post build() { - checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set"); - checkState(senderId != null, "sender must not be null"); - 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 == null) || !recipientId.equals(senderId), "sender and recipient must not be the same"); + validate(); return new PostImpl(soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text); } -- 2.7.4