Add ID-only constructors.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Post.java
1 /*
2  * FreenetSone - StatusUpdate.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
28 /**
29  * A post is a short message that a user writes in his Sone to let other users
30  * know what is going on.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class Post {
35
36         /** The GUID of the post. */
37         private final UUID id;
38
39         /** The Sone this post belongs to. */
40         private Sone sone;
41
42         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
43         private long time;
44
45         /** The text of the post. */
46         private String text;
47
48         /** The replies that have been loaded for this post. */
49         private final Set<Reply> replies = new HashSet<Reply>();
50
51         /**
52          * Creates a new post.
53          *
54          * @param id
55          *            The ID of the post
56          */
57         public Post(String id) {
58                 this(id, null, 0, null);
59         }
60
61         /**
62          * Creates a new post.
63          *
64          * @param sone
65          *            The Sone this post belongs to
66          * @param text
67          *            The text of the post
68          */
69         public Post(Sone sone, String text) {
70                 this(sone, System.currentTimeMillis(), text);
71         }
72
73         /**
74          * Creates a new post.
75          *
76          * @param sone
77          *            The Sone this post belongs to
78          * @param time
79          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
80          * @param text
81          *            The text of the post
82          */
83         public Post(Sone sone, long time, String text) {
84                 this(UUID.randomUUID().toString(), sone, time, text);
85         }
86
87         /**
88          * Creates a new post.
89          *
90          * @param id
91          *            The ID of the post
92          * @param sone
93          *            The Sone this post belongs to
94          * @param time
95          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
96          * @param text
97          *            The text of the post
98          */
99         public Post(String id, Sone sone, long time, String text) {
100                 this.id = UUID.fromString(id);
101                 this.sone = sone;
102                 this.time = time;
103                 this.text = text;
104         }
105
106         //
107         // ACCESSORS
108         //
109
110         /**
111          * Returns the ID of the post.
112          *
113          * @return The ID of the post
114          */
115         public String getId() {
116                 return id.toString();
117         }
118
119         /**
120          * Returns the Sone this post belongs to.
121          *
122          * @return The Sone of this post
123          */
124         public Sone getSone() {
125                 return sone;
126         }
127
128         /**
129          * Sets the Sone of this post.
130          *
131          * @param sone
132          *            The Sone of this post
133          * @return This post (for method chaining)
134          */
135         public Post setSone(Sone sone) {
136                 this.sone = sone;
137                 return this;
138         }
139
140         /**
141          * Returns the time of the post.
142          *
143          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
144          */
145         public long getTime() {
146                 return time;
147         }
148
149         /**
150          * Sets the time of this post.
151          *
152          * @param time
153          *            The time of this post (in milliseconds since Jan 1, 1970 UTC)
154          * @return This post (for method chaining)
155          */
156         public Post setTime(long time) {
157                 this.time = time;
158                 return this;
159         }
160
161         /**
162          * Returns the text of the post.
163          *
164          * @return The text of the post
165          */
166         public String getText() {
167                 return text;
168         }
169
170         /**
171          * Sets the text of this post.
172          *
173          * @param text
174          *            The text of this post
175          * @return This post (for method chaining)
176          */
177         public Post setText(String text) {
178                 this.text = text;
179                 return this;
180         }
181
182         /**
183          * Returns all replies to this post in unspecified order.
184          *
185          * @return All replies to this post
186          */
187         public List<Reply> getReplies() {
188                 List<Reply> sortedReplies = new ArrayList<Reply>(replies);
189                 Collections.sort(sortedReplies, new Comparator<Reply>() {
190
191                         @Override
192                         public int compare(Reply leftReply, Reply rightReply) {
193                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
194                         }
195
196                 });
197                 return sortedReplies;
198         }
199
200         /**
201          * Adds a reply to this post. The reply will not be added if its
202          * {@link Reply#getPost() post} is not equal to this post.
203          *
204          * @param reply
205          *            The reply to add
206          */
207         public void addReply(Reply reply) {
208                 if (reply.getPost().equals(this)) {
209                         replies.add(reply);
210                 }
211         }
212
213         /**
214          * Removes a reply from this post.
215          *
216          * @param reply
217          *            The reply to remove
218          */
219         public void removeReply(Reply reply) {
220                 if (reply.getPost().equals(this)) {
221                         replies.remove(reply);
222                 }
223         }
224
225         //
226         // OBJECT METHODS
227         //
228
229         /**
230          * {@inheritDoc}
231          */
232         @Override
233         public int hashCode() {
234                 return id.hashCode() ^ sone.hashCode() ^ (int) (time >> 32) ^ (int) (time & 0xffffffff) ^ text.hashCode();
235         }
236
237         /**
238          * {@inheritDoc}
239          */
240         @Override
241         public boolean equals(Object object) {
242                 if (!(object instanceof Post)) {
243                         return false;
244                 }
245                 Post post = (Post) object;
246                 return post.id.equals(id) && post.sone.equals(sone) && (post.time == time) && post.text.equals(text);
247         }
248
249         /**
250          * {@inheritDoc}
251          */
252         @Override
253         public String toString() {
254                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + ",replies(" + replies.size() + ")]";
255         }
256
257 }