1e45609610c3cd9350c3672b204ee08569781ff0
[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.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Optional.of;
23 import static com.google.common.base.Preconditions.checkNotNull;
24 import static com.google.common.base.Preconditions.checkState;
25 import static java.lang.System.currentTimeMillis;
26 import static java.util.UUID.randomUUID;
27
28 import net.pterodactylus.sone.database.Database;
29 import net.pterodactylus.sone.database.PostBuilder;
30
31 import com.google.common.base.Optional;
32 import org.apache.commons.lang.StringUtils;
33
34 /**
35  * Abstract {@link PostBuilder} implementation. It stores the state of the new
36  * post and performs validation, you only need to implement {@link #build()}.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public abstract class AbstractPostBuilder implements PostBuilder {
41
42         protected final Database database;
43         protected final String senderId;
44
45         /** The ID of the post. */
46         protected Optional<String> id = absent();
47
48         /** The time of the post. */
49         protected Optional<Long> time = absent();
50
51         /** The text of the post. */
52         protected String text;
53
54         /** The (optional) recipient of the post. */
55         protected Optional<String> recipientId = absent();
56
57         protected AbstractPostBuilder(Database database, String soneId) {
58                 this.database = checkNotNull(database, "database must not be null");
59                 this.senderId = checkNotNull(soneId, "sender ID must not be null");
60         }
61
62         //
63         // POSTBUILDER METHODS
64         //
65
66         /**
67          * {@inheritDoc}
68          */
69         @Override
70         public PostBuilder withId(String id) {
71                 this.id = fromNullable(id);
72                 return this;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         public PostBuilder withTime(long time) {
80                 this.time = of(time);
81                 return this;
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         public PostBuilder withText(String text) {
89                 this.text = text;
90                 return this;
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         @Override
97         public PostBuilder to(Optional<String> recipientId) {
98                 this.recipientId = recipientId;
99                 return this;
100         }
101
102         //
103         // PROTECTED METHODS
104         //
105
106         protected String getId() {
107                 return id.isPresent() ? id.get() : randomUUID().toString();
108         }
109
110         protected long getTime() {
111                 return time.isPresent() ? time.get() : currentTimeMillis();
112         }
113
114         /**
115          * Validates the state of this post builder.
116          *
117          * @throws IllegalStateException
118          *             if the state is not valid for building a new post
119          */
120         protected void validate() throws IllegalStateException {
121                 checkState(!StringUtils.isBlank(text), "text must not be empty");
122                 checkState(!recipientId.isPresent() || !senderId.equals(recipientId.get()), "sender and recipient must not be the same");
123         }
124
125 }