Only store sender and recipient IDs in a post.
[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.core.SoneProvider;
25 import net.pterodactylus.sone.data.Post;
26 import net.pterodactylus.sone.data.PostBuilder;
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         /** The Sone provider for the created posts. */
38         private final SoneProvider soneProvider;
39
40         /** Wether to create a post with a random ID. */
41         private boolean randomId;
42
43         /** The ID of the post. */
44         private String id;
45
46         /** The sender of the post. */
47         private String senderId;
48
49         /** Whether to use the current time when creating the post. */
50         private boolean currentTime;
51
52         /** The time of the post. */
53         private long time;
54
55         /** The text of the post. */
56         private String text;
57
58         /** The (optional) recipient of the post. */
59         private String recipientId;
60
61         /**
62          * Creates a new post builder.
63          *
64          * @param soneProvider
65          *            The Sone provider
66          */
67         public PostBuilderImpl(SoneProvider soneProvider) {
68                 this.soneProvider = soneProvider;
69         }
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         public PostBuilder copyPost(Post post) {
76                 this.randomId = false;
77                 this.id = post.getId();
78                 this.senderId = post.getSone().getId();
79                 this.currentTime = false;
80                 this.time = post.getTime();
81                 this.text = post.getText();
82                 this.recipientId = (post.getRecipient() != null) ? post.getRecipient().getId() : null;
83                 return this;
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         @Override
90         public PostBuilder randomId() {
91                 randomId = true;
92                 return this;
93         }
94
95         /**
96          * {@inheritDoc}
97          */
98         @Override
99         public PostBuilder withId(String id) {
100                 this.id = id;
101                 return this;
102         }
103
104         /**
105          * {@inheritDoc}
106          */
107         @Override
108         public PostBuilder from(String senderId) {
109                 this.senderId = senderId;
110                 return this;
111         }
112
113         /**
114          * {@inheritDoc}
115          */
116         @Override
117         public PostBuilder currentTime() {
118                 currentTime = true;
119                 return this;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public PostBuilder withTime(long time) {
127                 this.time = time;
128                 return this;
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         @Override
135         public PostBuilder withText(String text) {
136                 this.text = text;
137                 return this;
138         }
139
140         /**
141          * {@inheritDoc}
142          */
143         @Override
144         public PostBuilder to(String recipientId) {
145                 this.recipientId = recipientId;
146                 return this;
147         }
148
149         /**
150          * {@inheritDoc}
151          */
152         @Override
153         public Post build() {
154                 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
155                 checkState(senderId != null, "sender must not be null");
156                 checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
157                 checkState(!StringUtils.isBlank(text), "text must not be empty");
158                 checkState((recipientId == null) || !recipientId.equals(senderId), "sender and recipient must not be the same");
159                 return new PostImpl(soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
160         }
161
162 }