Merge commit '0.3.1-RC3' into message-recipient
[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.Comparator;
21 import java.util.UUID;
22
23 /**
24  * A post is a short message that a user writes in his Sone to let other users
25  * know what is going on.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class Post {
30
31         /** Comparator for posts, sorts descending by time. */
32         public static final Comparator<Post> TIME_COMPARATOR = new Comparator<Post>() {
33
34                 @Override
35                 public int compare(Post leftPost, Post rightPost) {
36                         return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
37                 }
38
39         };
40
41         /** The GUID of the post. */
42         private final UUID id;
43
44         /** The Sone this post belongs to. */
45         private volatile Sone sone;
46
47         /** The Sone of the recipient. */
48         private volatile Sone recipient;
49
50         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
51         private volatile long time;
52
53         /** The text of the post. */
54         private volatile String text;
55
56         /**
57          * Creates a new post.
58          *
59          * @param id
60          *            The ID of the post
61          */
62         public Post(String id) {
63                 this(id, null, 0, null);
64         }
65
66         /**
67          * Creates a new post.
68          *
69          * @param sone
70          *            The Sone this post belongs to
71          * @param text
72          *            The text of the post
73          */
74         public Post(Sone sone, String text) {
75                 this(sone, System.currentTimeMillis(), text);
76         }
77
78         /**
79          * Creates a new post.
80          *
81          * @param sone
82          *            The Sone this post belongs to
83          * @param time
84          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
85          * @param text
86          *            The text of the post
87          */
88         public Post(Sone sone, long time, String text) {
89                 this(UUID.randomUUID().toString(), sone, time, text);
90         }
91
92         /**
93          * Creates a new post.
94          *
95          * @param id
96          *            The ID of the post
97          * @param sone
98          *            The Sone this post belongs to
99          * @param time
100          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
101          * @param text
102          *            The text of the post
103          */
104         public Post(String id, Sone sone, long time, String text) {
105                 this.id = UUID.fromString(id);
106                 this.sone = sone;
107                 this.time = time;
108                 this.text = text;
109         }
110
111         //
112         // ACCESSORS
113         //
114
115         /**
116          * Returns the ID of the post.
117          *
118          * @return The ID of the post
119          */
120         public String getId() {
121                 return id.toString();
122         }
123
124         /**
125          * Returns the Sone this post belongs to.
126          *
127          * @return The Sone of this post
128          */
129         public Sone getSone() {
130                 return sone;
131         }
132
133         /**
134          * Sets the Sone of this post.
135          *
136          * @param sone
137          *            The Sone of this post
138          * @return This post (for method chaining)
139          */
140         public Post setSone(Sone sone) {
141                 this.sone = sone;
142                 return this;
143         }
144
145         /**
146          * Returns the recipient of this post, if any.
147          *
148          * @return The recipient of this post, or {@code null}
149          */
150         public Sone getRecipient() {
151                 return recipient;
152         }
153
154         /**
155          * Sets the recipient of this post.
156          *
157          * @param recipient
158          *            The recipient of this post, or {@code null}
159          * @return This post (for method chaining)
160          */
161         public Post setRecipient(Sone recipient) {
162                 this.recipient = recipient;
163                 return this;
164         }
165
166         /**
167          * Returns the time of the post.
168          *
169          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
170          */
171         public long getTime() {
172                 return time;
173         }
174
175         /**
176          * Sets the time of this post.
177          *
178          * @param time
179          *            The time of this post (in milliseconds since Jan 1, 1970 UTC)
180          * @return This post (for method chaining)
181          */
182         public Post setTime(long time) {
183                 this.time = time;
184                 return this;
185         }
186
187         /**
188          * Returns the text of the post.
189          *
190          * @return The text of the post
191          */
192         public String getText() {
193                 return text;
194         }
195
196         /**
197          * Sets the text of this post.
198          *
199          * @param text
200          *            The text of this post
201          * @return This post (for method chaining)
202          */
203         public Post setText(String text) {
204                 this.text = text;
205                 return this;
206         }
207
208         //
209         // OBJECT METHODS
210         //
211
212         /**
213          * {@inheritDoc}
214          */
215         @Override
216         public int hashCode() {
217                 return id.hashCode();
218         }
219
220         /**
221          * {@inheritDoc}
222          */
223         @Override
224         public boolean equals(Object object) {
225                 if (!(object instanceof Post)) {
226                         return false;
227                 }
228                 Post post = (Post) object;
229                 return post.id.equals(id);
230         }
231
232         /**
233          * {@inheritDoc}
234          */
235         @Override
236         public String toString() {
237                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
238         }
239
240 }