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.core.SoneProvider;
25 import net.pterodactylus.sone.data.Post;
26 import net.pterodactylus.sone.data.PostBuilder;
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 /** The Sone provider for the created posts. */
38 private final SoneProvider soneProvider;
40 /** Wether to create a post with a random ID. */
41 private boolean randomId;
43 /** The ID of the post. */
46 /** The sender of the post. */
47 private String senderId;
49 /** Whether to use the current time when creating the post. */
50 private boolean currentTime;
52 /** The time of the post. */
55 /** The text of the post. */
58 /** The (optional) recipient of the post. */
59 private String recipientId;
62 * Creates a new post builder.
67 public PostBuilderImpl(SoneProvider soneProvider) {
68 this.soneProvider = soneProvider;
75 public PostBuilder copyPost(Post post) {
76 this.randomId = false;
77 this.id = post.getId();
78 this.senderId = post.getSone().getId();
79 this.currentTime = false;
80 this.time = post.getTime();
81 this.text = post.getText();
82 this.recipientId = (post.getRecipient() != null) ? post.getRecipient().getId() : null;
90 public PostBuilder randomId() {
99 public PostBuilder withId(String id) {
108 public PostBuilder from(String senderId) {
109 this.senderId = senderId;
117 public PostBuilder currentTime() {
126 public PostBuilder withTime(long time) {
135 public PostBuilder withText(String text) {
144 public PostBuilder to(String recipientId) {
145 this.recipientId = recipientId;
153 public Post build() {
154 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
155 checkState(senderId != null, "sender must not be null");
156 checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
157 checkState(!StringUtils.isBlank(text), "text must not be empty");
158 checkState((recipientId == null) || !recipientId.equals(senderId), "sender and recipient must not be the same");
159 return new PostImpl(soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);