Add post comparator that sorts by time.
[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 time of the post (in milliseconds since Jan 1, 1970 UTC). */
48         private volatile long time;
49
50         /** The text of the post. */
51         private volatile String text;
52
53         /**
54          * Creates a new post.
55          *
56          * @param id
57          *            The ID of the post
58          */
59         public Post(String id) {
60                 this(id, null, 0, null);
61         }
62
63         /**
64          * Creates a new post.
65          *
66          * @param sone
67          *            The Sone this post belongs to
68          * @param text
69          *            The text of the post
70          */
71         public Post(Sone sone, String text) {
72                 this(sone, System.currentTimeMillis(), text);
73         }
74
75         /**
76          * Creates a new post.
77          *
78          * @param sone
79          *            The Sone this post belongs to
80          * @param time
81          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
82          * @param text
83          *            The text of the post
84          */
85         public Post(Sone sone, long time, String text) {
86                 this(UUID.randomUUID().toString(), sone, time, text);
87         }
88
89         /**
90          * Creates a new post.
91          *
92          * @param id
93          *            The ID of the post
94          * @param sone
95          *            The Sone this post belongs to
96          * @param time
97          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
98          * @param text
99          *            The text of the post
100          */
101         public Post(String id, Sone sone, long time, String text) {
102                 this.id = UUID.fromString(id);
103                 this.sone = sone;
104                 this.time = time;
105                 this.text = text;
106         }
107
108         //
109         // ACCESSORS
110         //
111
112         /**
113          * Returns the ID of the post.
114          *
115          * @return The ID of the post
116          */
117         public String getId() {
118                 return id.toString();
119         }
120
121         /**
122          * Returns the Sone this post belongs to.
123          *
124          * @return The Sone of this post
125          */
126         public Sone getSone() {
127                 return sone;
128         }
129
130         /**
131          * Sets the Sone of this post.
132          *
133          * @param sone
134          *            The Sone of this post
135          * @return This post (for method chaining)
136          */
137         public Post setSone(Sone sone) {
138                 this.sone = sone;
139                 return this;
140         }
141
142         /**
143          * Returns the time of the post.
144          *
145          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
146          */
147         public long getTime() {
148                 return time;
149         }
150
151         /**
152          * Sets the time of this post.
153          *
154          * @param time
155          *            The time of this post (in milliseconds since Jan 1, 1970 UTC)
156          * @return This post (for method chaining)
157          */
158         public Post setTime(long time) {
159                 this.time = time;
160                 return this;
161         }
162
163         /**
164          * Returns the text of the post.
165          *
166          * @return The text of the post
167          */
168         public String getText() {
169                 return text;
170         }
171
172         /**
173          * Sets the text of this post.
174          *
175          * @param text
176          *            The text of this post
177          * @return This post (for method chaining)
178          */
179         public Post setText(String text) {
180                 this.text = text;
181                 return this;
182         }
183
184         //
185         // OBJECT METHODS
186         //
187
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         public int hashCode() {
193                 return id.hashCode();
194         }
195
196         /**
197          * {@inheritDoc}
198          */
199         @Override
200         public boolean equals(Object object) {
201                 if (!(object instanceof Post)) {
202                         return false;
203                 }
204                 Post post = (Post) object;
205                 return post.id.equals(id);
206         }
207
208         /**
209          * {@inheritDoc}
210          */
211         @Override
212         public String toString() {
213                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
214         }
215
216 }