Store identities in database.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / PostBuilder.java
1 /*
2  * Sone - PostBuilder.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.database;
19
20 import net.pterodactylus.sone.data.Post;
21 import net.pterodactylus.sone.data.Sone;
22
23 import com.google.common.base.Optional;
24
25 /**
26  * Builder for {@link Post} objects.
27  * <p>
28  * A {@link Post} consists of the following elements:
29  * <ul>
30  * <li>an ID,</li>
31  * <li>a {@link Sone sender},</li>
32  * <li>an optional {@link Sone recipient},</li>
33  * <li>a time,</li>
34  * <li>and a text.</li>
35  * </ul>
36  * Except for the recipient, all this elements have to be configured on this
37  * builder. For the ID you have the possibility to configure either a random ID
38  * (which should be used for new posts) or a custom ID you specify (for creating
39  * an existing post). For the time you can use the current time (again, for
40  * creating new posts) or the given time (for loading posts). It is an error to
41  * specify both ways for either the ID or the time.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public interface PostBuilder {
46
47         /**
48          * Configures this builder to use the given ID as ID for the new post. If
49          * this method is used, {@link #randomId()} must not be used.
50          *
51          * @param id
52          *            The ID to use for the post
53          * @return This post builder
54          */
55         public PostBuilder withId(String id);
56
57         /**
58          * Configures the builder to use the given time as time for the new post. If
59          * this method is used, {@link #currentTime()} must not be used.
60          *
61          * @param time
62          *            The time to use for the post
63          * @return This post builder
64          */
65         public PostBuilder withTime(long time);
66
67         /**
68          * Configures the builder to use the given text for the new post.
69          *
70          * @param text
71          *            The text to use for the post
72          * @return This post builder
73          */
74         public PostBuilder withText(String text);
75
76         /**
77          * Configures the builder to use the given {@link Sone} as recipient for the
78          * post.
79          *
80          * @param recipientId
81          *            The ID of the recipient of the post
82          * @return This post builder
83          */
84         public PostBuilder to(Optional<String> recipientId);
85
86         /**
87          * Verifies this builder’s configuration and creates a new post.
88          * <p>
89          * The following conditions must be met in order for this builder to be
90          * configured correctly:
91          * <ul>
92          * <li>Exactly one of {@link #randomId()} or {@link #withId(String)} must
93          * have been called.</li>
94          * <li>The {@link #from(String) sender} must not be {@code null}.</li>
95          * <li>Exactly one of {@link #currentTime()} or {@link #withTime(long)} must
96          * have been called.</li>
97          * <li>The {@link #withText(String) text} must not be {@code null} and must
98          * contain something other than whitespace.</li>
99          * <li>The {@link #to(String) recipient} must either not have been set, or
100          * it must have been set to a {@link Sone} other than {@link #from(String)
101          * the sender}.</li>
102          * </ul>
103          *
104          * @return A new post
105          * @throws IllegalStateException
106          *             if this builder’s configuration is not valid
107          */
108         public Post build(Optional<PostCreated> postCreated) throws IllegalStateException;
109
110         interface PostCreated {
111
112                 void postCreated(Post post);
113
114         }
115
116 }