2 * Sone - Reply.java - Copyright © 2011 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.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;
73 * Creates a new reply with the given ID.
78 protected Reply(String id) {
79 this(id, null, 0, null);
83 * Creates a new reply with a new random ID.
86 * The Sone of the reply
88 * The time of the reply
90 * The text of the reply
92 protected Reply(Sone sone, long time, String text) {
93 this(UUID.randomUUID().toString(), sone, time, text);
97 * Creates a new reply.
100 * The ID of the reply
102 * The Sone of the reply
104 * The time of the reply
106 * The text of the reply
108 protected Reply(String id, Sone sone, long time, String text) {
116 * Returns the ID of the reply.
118 * @return The ID of the reply
120 public String getId() {
125 * Returns the Sone that posted this reply.
127 * @return The Sone that posted this reply
129 public Sone getSone() {
134 * Sets the Sone that posted this reply.
137 * The Sone that posted this reply
138 * @return This reply (for method chaining)
140 @SuppressWarnings("unchecked")
141 public T setSone(Sone sone) {
147 * Returns the time of the reply.
149 * @return The time of the reply (in milliseconds since Jan 1, 1970 UTC)
151 public long getTime() {
156 * Sets the time of this reply.
159 * The time of this reply (in milliseconds since Jan 1, 1970 UTC)
160 * @return This reply (for method chaining)
162 @SuppressWarnings("unchecked")
163 public T setTime(long time) {
169 * Returns the text of the reply.
171 * @return The text of the reply
173 public String getText() {
178 * Sets the text of this reply.
181 * The text of this reply
182 * @return This reply (for method chaining)
184 @SuppressWarnings("unchecked")
185 public T setText(String text) {
198 public int hashCode() {
199 return id.hashCode();
206 public boolean equals(Object object) {
207 if (!(object instanceof Reply<?>)) {
210 Reply<?> reply = (Reply<?>) object;
211 return reply.id.equals(id);
218 public String toString() {
219 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";