2 * Sone - Reply.java - Copyright © 2011–2012 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
20 import java.util.Comparator;
21 import java.util.UUID;
23 import net.pterodactylus.util.collection.filter.Filter;
26 * Abstract base class for all replies.
29 * The type of the reply
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public abstract class Reply<T extends Reply<T>> {
34 /** Comparator that sorts replies ascending by time. */
35 public static final Comparator<Reply<?>> TIME_COMPARATOR = new Comparator<Reply<?>>() {
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()));
47 /** Filter for replies with timestamps from the future. */
48 public static final Filter<Reply<?>> FUTURE_REPLY_FILTER = new Filter<Reply<?>>() {
54 public boolean filterObject(Reply<?> reply) {
55 return reply.getTime() <= System.currentTimeMillis();
60 /** The ID of the reply. */
61 private final String id;
63 /** The Sone that created this reply. */
64 private volatile Sone sone;
66 /** The time of the reply. */
67 private volatile long time;
69 /** The text of the reply. */
70 private volatile String text;
72 /** Whether the reply is known. */
73 private volatile boolean known;
76 * Creates a new reply with the given ID.
81 protected Reply(String id) {
82 this(id, null, 0, null);
86 * Creates a new reply with a new random ID.
89 * The Sone of the reply
91 * The time of the reply
93 * The text of the reply
95 protected Reply(Sone sone, long time, String text) {
96 this(UUID.randomUUID().toString(), sone, time, text);
100 * Creates a new reply.
103 * The ID of the reply
105 * The Sone of the reply
107 * The time of the reply
109 * The text of the reply
111 protected Reply(String id, Sone sone, long time, String text) {
119 * Returns the ID of the reply.
121 * @return The ID of the reply
123 public String getId() {
128 * Returns the Sone that posted this reply.
130 * @return The Sone that posted this reply
132 public Sone getSone() {
137 * Sets the Sone that posted this reply.
140 * The Sone that posted this reply
141 * @return This reply (for method chaining)
143 @SuppressWarnings("unchecked")
144 public T setSone(Sone sone) {
150 * Returns the time of the reply.
152 * @return The time of the reply (in milliseconds since Jan 1, 1970 UTC)
154 public long getTime() {
159 * Sets the time of this reply.
162 * The time of this reply (in milliseconds since Jan 1, 1970 UTC)
163 * @return This reply (for method chaining)
165 @SuppressWarnings("unchecked")
166 public T setTime(long time) {
172 * Returns the text of the reply.
174 * @return The text of the reply
176 public String getText() {
181 * Sets the text of this reply.
184 * The text of this reply
185 * @return This reply (for method chaining)
187 @SuppressWarnings("unchecked")
188 public T setText(String text) {
194 * Returns whether this reply is known.
196 * @return {@code true} if this reply is known, {@code false} otherwise
198 public boolean isKnown() {
203 * Sets whether this reply is known.
206 * {@code true} if this reply is known, {@code false} otherwise
209 @SuppressWarnings("unchecked")
210 public T setKnown(boolean known) {
223 public int hashCode() {
224 return id.hashCode();
231 public boolean equals(Object object) {
232 if (!(object instanceof Reply<?>)) {
235 Reply<?> reply = (Reply<?>) object;
236 return reply.id.equals(id);
243 public String toString() {
244 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";