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