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