2 * Sone - PostBuilderImpl.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.data.impl;
20 import static com.google.common.base.Preconditions.checkState;
22 import java.util.UUID;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.PostBuilder;
26 import net.pterodactylus.sone.data.Sone;
28 import org.apache.commons.lang.StringUtils;
31 * {@link PostBuilder} implementation that creates {@link PostImpl} objects.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class PostBuilderImpl implements PostBuilder {
37 /** Wether to create a post with a random ID. */
38 private boolean randomId;
40 /** The ID of the post. */
43 /** The sender of the post. */
46 /** Whether to use the current time when creating the post. */
47 private boolean currentTime;
49 /** The time of the post. */
52 /** The text of the post. */
55 /** The (optional) recipient of the post. */
56 private Sone recipient;
62 public PostBuilder copyPost(Post post) {
63 this.randomId = false;
64 this.id = post.getId();
65 this.sender = post.getSone();
66 this.currentTime = false;
67 this.time = post.getTime();
68 this.text = post.getText();
69 this.recipient = post.getRecipient();
77 public PostBuilder randomId() {
86 public PostBuilder withId(String id) {
95 public PostBuilder from(Sone sender) {
104 public PostBuilder currentTime() {
113 public PostBuilder withTime(long time) {
122 public PostBuilder withText(String text) {
131 public PostBuilder to(Sone recipient) {
132 this.recipient = recipient;
140 public Post build() {
141 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
142 checkState(sender != null, "sender must not be null");
143 checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
144 checkState(!StringUtils.isBlank(text), "text must not be empty");
145 checkState((recipient == null) || !recipient.equals(sender), "sender and recipient must not be the same");
146 return new PostImpl(randomId ? UUID.randomUUID().toString() : id, sender, currentTime ? System.currentTimeMillis() : time, text).setRecipient(recipient);