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