Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AbstractPostBuilder.java
1 /*
2  * Sone - AbstractPostBuilder.java - Copyright © 2013–2015 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.checkState;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.database.PostBuilder;
26 import net.pterodactylus.sone.database.SoneProvider;
27
28 /**
29  * Abstract {@link PostBuilder} implementation. It stores the state of the new
30  * post and performs validation, you only need to implement {@link #build()}.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public abstract class AbstractPostBuilder implements PostBuilder {
35
36         /** The Sone provider for the created posts. */
37         protected final SoneProvider soneProvider;
38
39         /** Wether to create a post with a random ID. */
40         protected boolean randomId;
41
42         /** The ID of the post. */
43         protected String id;
44
45         /** The sender of the post. */
46         protected String senderId;
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 String recipientId;
59
60         /**
61          * Creates a new abstract post builder.
62          *
63          * @param soneProvider
64          *            The Sone provider
65          */
66         public AbstractPostBuilder(SoneProvider soneProvider) {
67                 this.soneProvider = soneProvider;
68         }
69
70         //
71         // POSTBUILDER METHODS
72         //
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         public PostBuilder copyPost(Post post) {
79                 this.randomId = false;
80                 this.id = post.getId();
81                 this.senderId = post.getSone().getId();
82                 this.currentTime = false;
83                 this.time = post.getTime();
84                 this.text = post.getText();
85                 this.recipientId = post.getRecipientId().orNull();
86                 return this;
87         }
88
89         /**
90          * {@inheritDoc}
91          */
92         @Override
93         public PostBuilder randomId() {
94                 randomId = true;
95                 return this;
96         }
97
98         /**
99          * {@inheritDoc}
100          */
101         @Override
102         public PostBuilder withId(String id) {
103                 this.id = id;
104                 return this;
105         }
106
107         /**
108          * {@inheritDoc}
109          */
110         @Override
111         public PostBuilder from(String senderId) {
112                 this.senderId = senderId;
113                 return this;
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         @Override
120         public PostBuilder currentTime() {
121                 currentTime = true;
122                 return this;
123         }
124
125         /**
126          * {@inheritDoc}
127          */
128         @Override
129         public PostBuilder withTime(long time) {
130                 this.time = time;
131                 return this;
132         }
133
134         /**
135          * {@inheritDoc}
136          */
137         @Override
138         public PostBuilder withText(String text) {
139                 this.text = text;
140                 return this;
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public PostBuilder to(String recipientId) {
148                 this.recipientId = recipientId;
149                 return this;
150         }
151
152         //
153         // PROTECTED METHODS
154         //
155
156         /**
157          * Validates the state of this post builder.
158          *
159          * @throws IllegalStateException
160          *             if the state is not valid for building a new post
161          */
162         protected void validate() throws IllegalStateException {
163                 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
164                 checkState(senderId != null, "sender must not be null");
165                 checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
166                 checkState(!StringUtils.isBlank(text), "text must not be empty");
167                 checkState((recipientId == null) || !recipientId.equals(senderId), "sender and recipient must not be the same");
168         }
169
170 }