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