4a4b797e257afeec432e3a5a274d51ebd62c9586
[Sone.git] / src / main / java / net / pterodactylus / sone / data / PostShell.java
1 /*
2  * Sone - PostShell.java - Copyright © 2010 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;
19
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.UUID;
24
25 /**
26  * {@link Shell} around a {@link Post} that has not yet been retrieved from
27  * Freenet.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class PostShell extends Post implements Shell<Post> {
32
33         /** The shell creator. */
34         public static final ShellCreator<Post> creator = new ShellCreator<Post>() {
35
36                 @Override
37                 public Shell<Post> createShell(String id) {
38                         return new PostShell().setId(UUID.fromString(id));
39                 }
40         };
41
42         /** The GUID of the post. */
43         private UUID id;
44
45         /** The Sone this post belongs to. */
46         private Sone sone;
47
48         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
49         private Long time;
50
51         /** The text of the post. */
52         private String text;
53
54         /** The replies that have been loaded for this post. */
55         private final Set<Reply> replies = new HashSet<Reply>();
56
57         /**
58          * Creates a new post shell.
59          */
60         public PostShell() {
61                 super(null, null);
62         }
63
64         //
65         // ACCESSORS
66         //
67
68         /**
69          * Returns the ID of the post.
70          *
71          * @return The ID of the post
72          */
73         @Override
74         public String getId() {
75                 return id.toString();
76         }
77
78         /**
79          * Sets the ID of the post.
80          *
81          * @param id
82          *            The ID of the post
83          * @return This post shell (for method chaining)
84          */
85         public PostShell setId(UUID id) {
86                 this.id = id;
87                 return this;
88         }
89
90         /**
91          * Returns the Sone this post belongs to.
92          *
93          * @return The Sone of this post
94          */
95         @Override
96         public Sone getSone() {
97                 return sone;
98         }
99
100         /**
101          * Sets the Sone the post belongs to.
102          *
103          * @param sone
104          *            The Sone the post belongs to
105          * @return This post shell (for method chaining)
106          */
107         public PostShell setSone(Sone sone) {
108                 this.sone = sone;
109                 return this;
110         }
111
112         /**
113          * Returns the time of the post.
114          *
115          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
116          */
117         @Override
118         public long getTime() {
119                 return time;
120         }
121
122         /**
123          * Sets the time of the post.
124          *
125          * @param time
126          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
127          * @return This post shell (for method chaining)
128          */
129         public PostShell setTime(long time) {
130                 this.time = time;
131                 return this;
132         }
133
134         /**
135          * Returns the text of the post.
136          *
137          * @return The text of the post
138          */
139         @Override
140         public String getText() {
141                 return text;
142         }
143
144         /**
145          * Sets the text of the post.
146          *
147          * @param text
148          *            The text of the post.
149          * @return This post shell (for method chaining)
150          */
151         public PostShell setText(String text) {
152                 this.text = text;
153                 return this;
154         }
155
156         /**
157          * Returns all replies to this post in unspecified order.
158          *
159          * @return All replies to this post
160          */
161         @Override
162         public Set<Reply> getReplies() {
163                 return Collections.unmodifiableSet(replies);
164         }
165
166         /**
167          * Adds a reply to this post. The reply will not be added if its
168          * {@link Reply#getPost() post} is not equal to this post.
169          *
170          * @param reply
171          *            The reply to add
172          */
173         @Override
174         public void addReply(Reply reply) {
175                 if (reply.getPost().equals(this)) {
176                         replies.add(reply);
177                 }
178         }
179
180         /**
181          * Removes a reply from this post.
182          *
183          * @param reply
184          *            The reply to remove
185          */
186         @Override
187         public void removeReply(Reply reply) {
188                 if (reply.getPost().equals(this)) {
189                         replies.remove(reply);
190                 }
191         }
192
193         //
194         // INTERFACE Shell
195         //
196
197         /**
198          * {@inheritDoc}
199          */
200         @Override
201         public boolean canUnshell() {
202                 return (id != null) && (sone != null) && (!(sone instanceof Shell<?>)) && (time != null) && (text != null);
203         }
204
205         /**
206          * {@inheritDoc}
207          */
208         @Override
209         public Post getShelled() {
210                 if (canUnshell()) {
211                         Post post = new Post(id, sone, time, text);
212                         for (Reply reply : replies) {
213                                 post.addReply(reply);
214                         }
215                 }
216                 return this;
217         }
218
219 }