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