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