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