15d8287859886be37230018772c7498ae259fdbd
[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         /** Whether the reply is known. */
73         private volatile boolean known;
74
75         /**
76          * Creates a new reply with the given ID.
77          *
78          * @param id
79          *            The ID of the reply
80          */
81         protected Reply(String id) {
82                 this(id, null, 0, null);
83         }
84
85         /**
86          * Creates a new reply with a new random ID.
87          *
88          * @param sone
89          *            The Sone of the reply
90          * @param time
91          *            The time of the reply
92          * @param text
93          *            The text of the reply
94          */
95         protected Reply(Sone sone, long time, String text) {
96                 this(UUID.randomUUID().toString(), sone, time, text);
97         }
98
99         /**
100          * Creates a new reply.
101          *
102          * @param id
103          *            The ID of the reply
104          * @param sone
105          *            The Sone of the reply
106          * @param time
107          *            The time of the reply
108          * @param text
109          *            The text of the reply
110          */
111         protected Reply(String id, Sone sone, long time, String text) {
112                 this.id = id;
113                 this.sone = sone;
114                 this.time = time;
115                 this.text = text;
116         }
117
118         /**
119          * Returns the ID of the reply.
120          *
121          * @return The ID of the reply
122          */
123         public String getId() {
124                 return id;
125         }
126
127         /**
128          * Returns the Sone that posted this reply.
129          *
130          * @return The Sone that posted this reply
131          */
132         public Sone getSone() {
133                 return sone;
134         }
135
136         /**
137          * Sets the Sone that posted this reply.
138          *
139          * @param sone
140          *            The Sone that posted this reply
141          * @return This reply (for method chaining)
142          */
143         @SuppressWarnings("unchecked")
144         public T setSone(Sone sone) {
145                 this.sone = sone;
146                 return (T) this;
147         }
148
149         /**
150          * Returns the time of the reply.
151          *
152          * @return The time of the reply (in milliseconds since Jan 1, 1970 UTC)
153          */
154         public long getTime() {
155                 return time;
156         }
157
158         /**
159          * Sets the time of this reply.
160          *
161          * @param time
162          *            The time of this reply (in milliseconds since Jan 1, 1970 UTC)
163          * @return This reply (for method chaining)
164          */
165         @SuppressWarnings("unchecked")
166         public T setTime(long time) {
167                 this.time = time;
168                 return (T) this;
169         }
170
171         /**
172          * Returns the text of the reply.
173          *
174          * @return The text of the reply
175          */
176         public String getText() {
177                 return text;
178         }
179
180         /**
181          * Sets the text of this reply.
182          *
183          * @param text
184          *            The text of this reply
185          * @return This reply (for method chaining)
186          */
187         @SuppressWarnings("unchecked")
188         public T setText(String text) {
189                 this.text = text;
190                 return (T) this;
191         }
192
193         /**
194          * Returns whether this reply is known.
195          *
196          * @return {@code true} if this reply is known, {@code false} otherwise
197          */
198         public boolean isKnown() {
199                 return known;
200         }
201
202         /**
203          * Sets whether this reply is known.
204          *
205          * @param known
206          *            {@code true} if this reply is known, {@code false} otherwise
207          * @return This reply
208          */
209         @SuppressWarnings("unchecked")
210         public T setKnown(boolean known) {
211                 this.known = known;
212                 return (T) 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 + ",time=" + time + ",text=" + text + "]";
245         }
246
247 }