2 * Sone - AbstractPostBuilder.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.Optional.absent;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.database.Database;
26 import net.pterodactylus.sone.database.PostBuilder;
28 import com.google.common.base.Optional;
29 import org.apache.commons.lang.StringUtils;
32 * Abstract {@link PostBuilder} implementation. It stores the state of the new
33 * post and performs validation, you only need to implement {@link #build()}.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public abstract class AbstractPostBuilder implements PostBuilder {
39 protected final Database database;
40 protected final String senderId;
42 /** Wether to create a post with a random ID. */
43 protected boolean randomId;
45 /** The ID of the post. */
48 /** Whether to use the current time when creating the post. */
49 protected boolean currentTime;
51 /** The time of the post. */
54 /** The text of the post. */
55 protected String text;
57 /** The (optional) recipient of the post. */
58 protected Optional<String> recipientId = absent();
60 protected AbstractPostBuilder(Database database, String soneId) {
61 this.database = checkNotNull(database, "database must not be null");
62 this.senderId = checkNotNull(soneId, "sender ID must not be null");
66 // POSTBUILDER METHODS
73 public PostBuilder randomId() {
82 public PostBuilder withId(String id) {
91 public PostBuilder currentTime() {
100 public PostBuilder withTime(long time) {
109 public PostBuilder withText(String text) {
118 public PostBuilder to(Optional<String> recipientId) {
119 this.recipientId = recipientId;
128 * Validates the state of this post builder.
130 * @throws IllegalStateException
131 * if the state is not valid for building a new post
133 protected void validate() throws IllegalStateException {
134 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
135 checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
136 checkState(!StringUtils.isBlank(text), "text must not be empty");
137 checkState(!recipientId.isPresent() || !senderId.equals(recipientId.get()), "sender and recipient must not be the same");