Make posts (more or less) immutable.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / PostBuilderImpl.java
1 /*
2  * Sone - PostBuilderImpl.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 java.util.UUID;
23
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.PostBuilder;
26 import net.pterodactylus.sone.data.Sone;
27
28 import org.apache.commons.lang.StringUtils;
29
30 /**
31  * {@link PostBuilder} implementation that creates {@link PostImpl} objects.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class PostBuilderImpl implements PostBuilder {
36
37         /** Wether to create a post with a random ID. */
38         private boolean randomId;
39
40         /** The ID of the post. */
41         private String id;
42
43         /** The sender of the post. */
44         private Sone sender;
45
46         /** Whether to use the current time when creating the post. */
47         private boolean currentTime;
48
49         /** The time of the post. */
50         private long time;
51
52         /** The text of the post. */
53         private String text;
54
55         /** The (optional) recipient of the post. */
56         private Sone recipient;
57
58         /**
59          * {@inheritDoc}
60          */
61         @Override
62         public PostBuilder copyPost(Post post) {
63                 this.randomId = false;
64                 this.id = post.getId();
65                 this.sender = post.getSone();
66                 this.currentTime = false;
67                 this.time = post.getTime();
68                 this.text = post.getText();
69                 this.recipient = post.getRecipient();
70                 return this;
71         }
72
73         /**
74          * {@inheritDoc}
75          */
76         @Override
77         public PostBuilder randomId() {
78                 randomId = true;
79                 return this;
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         public PostBuilder withId(String id) {
87                 this.id = id;
88                 return this;
89         }
90
91         /**
92          * {@inheritDoc}
93          */
94         @Override
95         public PostBuilder from(Sone sender) {
96                 this.sender = sender;
97                 return this;
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         @Override
104         public PostBuilder currentTime() {
105                 currentTime = true;
106                 return this;
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public PostBuilder withTime(long time) {
114                 this.time = time;
115                 return this;
116         }
117
118         /**
119          * {@inheritDoc}
120          */
121         @Override
122         public PostBuilder withText(String text) {
123                 this.text = text;
124                 return this;
125         }
126
127         /**
128          * {@inheritDoc}
129          */
130         @Override
131         public PostBuilder to(Sone recipient) {
132                 this.recipient = recipient;
133                 return this;
134         }
135
136         /**
137          * {@inheritDoc}
138          */
139         @Override
140         public Post build() {
141                 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
142                 checkState(sender != null, "sender must not be null");
143                 checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
144                 checkState(!StringUtils.isBlank(text), "text must not be empty");
145                 checkState((recipient == null) || !recipient.equals(sender), "sender and recipient must not be the same");
146                 return new PostImpl(randomId ? UUID.randomUUID().toString() : id, sender, recipient, currentTime ? System.currentTimeMillis() : time, text);
147         }
148
149 }