c1377cae44073d1b7ac4b04e0732398f395d92ac
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / database / PostBuilder.kt
1 /*
2  * Sone - PostBuilder.java - Copyright © 2013–2016 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 [Post] objects.
25  *
26  *
27  * A [Post] consists of the following elements:
28  *
29  *  * an ID,
30  *  * a [sender][Sone],
31  *  * an optional [recipient][Sone],
32  *  * a time,
33  *  * and a text.
34  *
35  * Except for the recipient, all this elements have to be configured on this
36  * builder. For the ID you have the possibility to configure either a random ID
37  * (which should be used for new posts) or a custom ID you specify (for creating
38  * an existing post). For the time you can use the current time (again, for
39  * creating new posts) or the given time (for loading posts). It is an error to
40  * specify both ways for either the ID or the time.
41  */
42 interface PostBuilder {
43
44         fun copyPost(post: Post): PostBuilder
45
46         fun from(senderId: String): PostBuilder
47
48         fun randomId(): PostBuilder
49         fun withId(id: String): PostBuilder
50
51         fun currentTime(): PostBuilder
52         fun withTime(time: Long): PostBuilder
53
54         fun withText(text: String): PostBuilder
55
56         fun to(recipientId: String): PostBuilder
57
58         /**
59          * Verifies this builder’s configuration and creates a new post.
60          *
61          * The following conditions must be met in order for this builder to be
62          * configured correctly:
63          *
64          *  * Exactly one of [randomId] or [withId] must have been called.
65          *  * The [sender][from] must not be `null`.
66          *  * Exactly one of [currentTime] or [withTime] must have been called.
67          *  * The [text][withText] must not be `null` and must contain something other than whitespace.
68          *  * The [recipient][to] must either not have been set, or it must have been set to a [Sone] other than [the sender][from].
69          *
70          * @return A new post
71          * @throws IllegalStateException if this builder’s configuration is not valid
72          */
73         fun build(): Post
74
75 }