Fix ALL the logging!
[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         /** Whether the post is known. */
69         private volatile boolean known;
70
71         /**
72          * Creates a new post.
73          *
74          * @param id
75          *            The ID of the post
76          */
77         public Post(String id) {
78                 this(id, null, 0, null);
79         }
80
81         /**
82          * Creates a new post.
83          *
84          * @param sone
85          *            The Sone this post belongs to
86          * @param text
87          *            The text of the post
88          */
89         public Post(Sone sone, String text) {
90                 this(sone, System.currentTimeMillis(), text);
91         }
92
93         /**
94          * Creates a new post.
95          *
96          * @param sone
97          *            The Sone this post belongs to
98          * @param time
99          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
100          * @param text
101          *            The text of the post
102          */
103         public Post(Sone sone, long time, String text) {
104                 this(UUID.randomUUID().toString(), sone, time, text);
105         }
106
107         /**
108          * Creates a new post.
109          *
110          * @param id
111          *            The ID of the post
112          * @param sone
113          *            The Sone this post belongs to
114          * @param time
115          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
116          * @param text
117          *            The text of the post
118          */
119         public Post(String id, Sone sone, long time, String text) {
120                 this.id = UUID.fromString(id);
121                 this.sone = sone;
122                 this.time = time;
123                 this.text = text;
124         }
125
126         //
127         // ACCESSORS
128         //
129
130         /**
131          * Returns the ID of the post.
132          *
133          * @return The ID of the post
134          */
135         public String getId() {
136                 return id.toString();
137         }
138
139         /**
140          * Returns the Sone this post belongs to.
141          *
142          * @return The Sone of this post
143          */
144         public Sone getSone() {
145                 return sone;
146         }
147
148         /**
149          * Sets the Sone of this post.
150          *
151          * @param sone
152          *            The Sone of this post
153          * @return This post (for method chaining)
154          */
155         public Post setSone(Sone sone) {
156                 this.sone = sone;
157                 return this;
158         }
159
160         /**
161          * Returns the recipient of this post, if any.
162          *
163          * @return The recipient of this post, or {@code null}
164          */
165         public Sone getRecipient() {
166                 return recipient;
167         }
168
169         /**
170          * Sets the recipient of this post.
171          *
172          * @param recipient
173          *            The recipient of this post, or {@code null}
174          * @return This post (for method chaining)
175          */
176         public Post setRecipient(Sone recipient) {
177                 if (!sone.equals(recipient)) {
178                         this.recipient = recipient;
179                 }
180                 return this;
181         }
182
183         /**
184          * Returns the time of the post.
185          *
186          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
187          */
188         public long getTime() {
189                 return time;
190         }
191
192         /**
193          * Sets the time of this post.
194          *
195          * @param time
196          *            The time of this post (in milliseconds since Jan 1, 1970 UTC)
197          * @return This post (for method chaining)
198          */
199         public Post setTime(long time) {
200                 this.time = time;
201                 return this;
202         }
203
204         /**
205          * Returns the text of the post.
206          *
207          * @return The text of the post
208          */
209         public String getText() {
210                 return text;
211         }
212
213         /**
214          * Sets the text of this post.
215          *
216          * @param text
217          *            The text of this post
218          * @return This post (for method chaining)
219          */
220         public Post setText(String text) {
221                 this.text = text;
222                 return this;
223         }
224
225         /**
226          * Returns whether this post is known.
227          *
228          * @return {@code true} if this post is known, {@code false} otherwise
229          */
230         public boolean isKnown() {
231                 return known;
232         }
233
234         /**
235          * Sets whether this post is known.
236          *
237          * @param known
238          *            {@code true} if this post is known, {@code false} otherwise
239          * @return This post
240          */
241         public Post setKnown(boolean known) {
242                 this.known = known;
243                 return this;
244         }
245
246         //
247         // OBJECT METHODS
248         //
249
250         /**
251          * {@inheritDoc}
252          */
253         @Override
254         public int hashCode() {
255                 return id.hashCode();
256         }
257
258         /**
259          * {@inheritDoc}
260          */
261         @Override
262         public boolean equals(Object object) {
263                 if (!(object instanceof Post)) {
264                         return false;
265                 }
266                 Post post = (Post) object;
267                 return post.id.equals(id);
268         }
269
270         /**
271          * {@inheritDoc}
272          */
273         @Override
274         public String toString() {
275                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
276         }
277
278 }