X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FPostBuilderImpl.java;h=6a072b3c50ecc8c857a5736d3e88b3a93a477cc8;hp=967de9df32a9764c1974cd689cfd68baf229e673;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=8503021c00d63885288c507d9930fefabd5d7678 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 967de9d..6a072b3 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java @@ -1,5 +1,5 @@ /* - * Sone - PostBuilderImpl.java - Copyright © 2013 David Roden + * Sone - PostBuilderImpl.java - Copyright © 2013–2016 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 @@ -17,120 +17,25 @@ 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.data.PostBuilder; -import net.pterodactylus.sone.data.Sone; - -import org.apache.commons.lang.StringUtils; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.SoneProvider; /** * {@link PostBuilder} implementation that creates {@link PostImpl} objects. - * - * @author David ‘Bombe’ Roden */ -public class PostBuilderImpl implements PostBuilder { - - /** 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 Sone sender; - - /** 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 Sone recipient; - - /** - * {@inheritDoc} - */ - @Override - public PostBuilder copyPost(Post post) { - this.randomId = false; - this.id = post.getId(); - this.sender = post.getSone(); - this.currentTime = false; - this.time = post.getTime(); - this.text = post.getText(); - this.recipient = post.getRecipient(); - 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(Sone sender) { - this.sender = sender; - return this; - } +public class PostBuilderImpl extends AbstractPostBuilder { /** - * {@inheritDoc} - */ - @Override - public PostBuilder currentTime() { - currentTime = true; - return this; - } - - /** - * {@inheritDoc} + * Creates a new post builder. + * + * @param soneProvider + * The Sone provider */ - @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(Sone recipient) { - this.recipient = recipient; - return this; + public PostBuilderImpl(SoneProvider soneProvider) { + super(soneProvider); } /** @@ -138,12 +43,8 @@ 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(sender != 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((recipient == null) || !recipient.equals(sender), "sender and recipient must not be the same"); - return new PostImpl(randomId ? UUID.randomUUID().toString() : id, sender, currentTime ? System.currentTimeMillis() : time, text).setRecipient(recipient); + validate(); + return new PostImpl(soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text); } }