Add post database interface.
[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 /**
24  * Builder for {@link Post} objects.
25  * <p>
26  * A {@link Post} consists of the following elements:
27  * <ul>
28  * <li>an ID,</li>
29  * <li>a {@link Sone sender},</li>
30  * <li>an optional {@link Sone recipient},</li>
31  * <li>a time,</li>
32  * <li>and a text.</li>
33  * </ul>
34  * Except for the recipient, all this elements have to be configured on this
35  * builder. For the ID you have the possibility to configure either a random ID
36  * (which should be used for new posts) or a custom ID you specify (for creating
37  * an existing post). For the time you can use the current time (again, for
38  * creating new posts) or the given time (for loading posts). It is an error to
39  * specify both ways for either the ID or the time.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public interface PostBuilder {
44
45         /**
46          * Copies all attributes of the given post to this post builder.
47          *
48          * @param post
49          *            The post whose attributes to copy into this builder
50          * @return This builder
51          * @throws NullPointerException
52          *             if {@code post} is {@code null}
53          */
54         public PostBuilder copyPost(Post post) throws NullPointerException;
55
56         /**
57          * Configures this builder to use the given Sone as sender of the new post.
58          *
59          * @param senderId
60          *            The ID of the sender of the post
61          * @return This post builder
62          */
63         public PostBuilder from(String senderId);
64
65         /**
66          * Configures this builder to use a random ID for the new post. If this
67          * method is used, {@link #withId(String)} must not be used.
68          *
69          * @return This post builder
70          */
71         public PostBuilder randomId();
72
73         /**
74          * Configures this builder to use the given ID as ID for the new post. If
75          * this method is used, {@link #randomId()} must not be used.
76          *
77          * @param id
78          *            The ID to use for the post
79          * @return This post builder
80          */
81         public PostBuilder withId(String id);
82
83         /**
84          * Configures this builder to use the current time when creating the post.
85          * If this method is used, {@link #withTime(long)} must not be used.
86          *
87          * @return This post builder
88          */
89         public PostBuilder currentTime();
90
91         /**
92          * Configures the builder to use the given time as time for the new post. If
93          * this method is used, {@link #currentTime()} must not be used.
94          *
95          * @param time
96          *            The time to use for the post
97          * @return This post builder
98          */
99         public PostBuilder withTime(long time);
100
101         /**
102          * Configures the builder to use the given text for the new post.
103          *
104          * @param text
105          *            The text to use for the post
106          * @return This post builder
107          */
108         public PostBuilder withText(String text);
109
110         /**
111          * Configures the builder to use the given {@link Sone} as recipient for the
112          * post.
113          *
114          * @param recipientId
115          *            The ID of the recipient of the post
116          * @return This post builder
117          */
118         public PostBuilder to(String recipientId);
119
120         /**
121          * Verifies this builder’s configuration and creates a new post.
122          * <p>
123          * The following conditions must be met in order for this builder to be
124          * configured correctly:
125          * <ul>
126          * <li>Exactly one of {@link #randomId()} or {@link #withId(String)} must
127          * have been called.</li>
128          * <li>The {@link #from(String) sender} must not be {@code null}.</li>
129          * <li>Exactly one of {@link #currentTime()} or {@link #withTime(long)} must
130          * have been called.</li>
131          * <li>The {@link #withText(String) text} must not be {@code null} and must
132          * contain something other than whitespace.</li>
133          * <li>The {@link #to(String) recipient} must either not have been set, or
134          * it must have been set to a {@link Sone} other than {@link #from(String)
135          * the sender}.</li>
136          * </ul>
137          *
138          * @return A new post
139          * @throws IllegalStateException
140          *             if this builder’s configuration is not valid
141          */
142         public Post build() throws IllegalStateException;
143
144 }