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