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