65dd1bd72722c32ab1b8c11d4c9eb81f0077456a
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AbstractPostBuilder.java
1 /*
2  * Sone - AbstractPostBuilder.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data.impl;
19
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;
23
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.database.Database;
26 import net.pterodactylus.sone.database.PostBuilder;
27
28 import com.google.common.base.Optional;
29 import org.apache.commons.lang.StringUtils;
30
31 /**
32  * Abstract {@link PostBuilder} implementation. It stores the state of the new
33  * post and performs validation, you only need to implement {@link #build()}.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public abstract class AbstractPostBuilder implements PostBuilder {
38
39         protected final Database database;
40         protected final String senderId;
41
42         /** Wether to create a post with a random ID. */
43         protected boolean randomId;
44
45         /** The ID of the post. */
46         protected String id;
47
48         /** Whether to use the current time when creating the post. */
49         protected boolean currentTime;
50
51         /** The time of the post. */
52         protected long time;
53
54         /** The text of the post. */
55         protected String text;
56
57         /** The (optional) recipient of the post. */
58         protected Optional<String> recipientId = absent();
59
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");
63         }
64
65         //
66         // POSTBUILDER METHODS
67         //
68
69         /**
70          * {@inheritDoc}
71          */
72         @Override
73         public PostBuilder randomId() {
74                 randomId = true;
75                 return this;
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         @Override
82         public PostBuilder withId(String id) {
83                 this.id = id;
84                 return this;
85         }
86
87         /**
88          * {@inheritDoc}
89          */
90         @Override
91         public PostBuilder currentTime() {
92                 currentTime = true;
93                 return this;
94         }
95
96         /**
97          * {@inheritDoc}
98          */
99         @Override
100         public PostBuilder withTime(long time) {
101                 this.time = time;
102                 return this;
103         }
104
105         /**
106          * {@inheritDoc}
107          */
108         @Override
109         public PostBuilder withText(String text) {
110                 this.text = text;
111                 return this;
112         }
113
114         /**
115          * {@inheritDoc}
116          */
117         @Override
118         public PostBuilder to(Optional<String> recipientId) {
119                 this.recipientId = recipientId;
120                 return this;
121         }
122
123         //
124         // PROTECTED METHODS
125         //
126
127         /**
128          * Validates the state of this post builder.
129          *
130          * @throws IllegalStateException
131          *             if the state is not valid for building a new post
132          */
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");
138         }
139
140 }