ad580eef7b94f3c29557f4926e1c8e7893ea13d9
[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.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
22
23 import net.pterodactylus.sone.data.Post;
24 import net.pterodactylus.sone.database.Database;
25 import net.pterodactylus.sone.database.PostBuilder;
26
27 import org.apache.commons.lang.StringUtils;
28
29 /**
30  * Abstract {@link PostBuilder} implementation. It stores the state of the new
31  * post and performs validation, you only need to implement {@link #build()}.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public abstract class AbstractPostBuilder implements PostBuilder {
36
37         protected final Database database;
38         protected final String senderId;
39
40         /** Wether to create a post with a random ID. */
41         protected boolean randomId;
42
43         /** The ID of the post. */
44         protected String id;
45
46         /** Whether to use the current time when creating the post. */
47         protected boolean currentTime;
48
49         /** The time of the post. */
50         protected long time;
51
52         /** The text of the post. */
53         protected String text;
54
55         /** The (optional) recipient of the post. */
56         protected String recipientId;
57
58         protected AbstractPostBuilder(Database database, String soneId) {
59                 this.database = checkNotNull(database, "database must not be null");
60                 this.senderId = checkNotNull(soneId, "sender ID must not be null");
61         }
62
63         //
64         // POSTBUILDER METHODS
65         //
66
67         /**
68          * {@inheritDoc}
69          */
70         @Override
71         public PostBuilder copyPost(Post post) {
72                 this.randomId = false;
73                 this.id = post.getId();
74                 this.currentTime = false;
75                 this.time = post.getTime();
76                 this.text = post.getText();
77                 this.recipientId = post.getRecipientId().orNull();
78                 return this;
79         }
80
81         /**
82          * {@inheritDoc}
83          */
84         @Override
85         public PostBuilder randomId() {
86                 randomId = true;
87                 return this;
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         @Override
94         public PostBuilder withId(String id) {
95                 this.id = id;
96                 return this;
97         }
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
103         public PostBuilder currentTime() {
104                 currentTime = true;
105                 return this;
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         @Override
112         public PostBuilder withTime(long time) {
113                 this.time = time;
114                 return this;
115         }
116
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         public PostBuilder withText(String text) {
122                 this.text = text;
123                 return this;
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public PostBuilder to(String recipientId) {
131                 this.recipientId = recipientId;
132                 return this;
133         }
134
135         //
136         // PROTECTED METHODS
137         //
138
139         /**
140          * Validates the state of this post builder.
141          *
142          * @throws IllegalStateException
143          *             if the state is not valid for building a new post
144          */
145         protected void validate() throws IllegalStateException {
146                 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
147                 checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
148                 checkState(!StringUtils.isBlank(text), "text must not be empty");
149                 checkState(!senderId.equals(recipientId), "sender and recipient must not be the same");
150         }
151
152 }