2 * Sone - Reply.java - Copyright © 2010 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 * A reply is like a {@link Post} but can never be posted on its own, it always
27 * refers to another {@link Post}.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 /** Comparator that sorts replies ascending by time. */
34 public static final Comparator<Reply> TIME_COMPARATOR = new Comparator<Reply>() {
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()));
43 /** Filter for replies with timestamps from the future. */
44 public static final Filter<Reply> FUTURE_REPLIES_FILTER = new Filter<Reply>() {
47 public boolean filterObject(Reply reply) {
48 return reply.getTime() <= System.currentTimeMillis();
53 /** The ID of the reply. */
54 private final UUID id;
56 /** The Sone that posted this reply. */
57 private volatile Sone sone;
59 /** The Post this reply refers to. */
60 private volatile Post post;
62 /** The time of the reply. */
63 private volatile long time;
65 /** The text of the reply. */
66 private volatile String text;
69 * Creates a new reply.
74 public Reply(String id) {
75 this(id, null, null, 0, null);
79 * Creates a new reply.
82 * The sone that posted the reply
84 * The post to reply to
86 * The text of the reply
88 public Reply(Sone sone, Post post, String text) {
89 this(sone, post, System.currentTimeMillis(), text);
93 * Creates a new reply-
96 * The sone that posted the reply
98 * The post to reply to
100 * The time of the reply
102 * The text of the reply
104 public Reply(Sone sone, Post post, long time, String text) {
105 this(UUID.randomUUID().toString(), sone, post, time, text);
109 * Creates a new reply-
112 * The sone that posted the reply
114 * The ID of the reply
116 * The post to reply to
118 * The time of the reply
120 * The text of the reply
122 public Reply(String id, Sone sone, Post post, long time, String text) {
123 this.id = UUID.fromString(id);
135 * Returns the ID of the reply.
137 * @return The ID of the reply
139 public String getId() {
140 return id.toString();
144 * Returns the Sone that posted this reply.
146 * @return The Sone that posted this reply
148 public Sone getSone() {
153 * Sets the Sone that posted this reply.
156 * The Sone that posted this reply
157 * @return This reply (for method chaining)
159 public Reply setSone(Sone sone) {
165 * Returns the post this reply refers to.
167 * @return The post this reply refers to
169 public Post getPost() {
174 * Sets the post this reply refers to.
177 * The post this reply refers to
178 * @return This reply (for method chaining)
180 public Reply setPost(Post post) {
186 * Returns the time of the reply.
188 * @return The time of the reply (in milliseconds since Jan 1, 1970 UTC)
190 public long getTime() {
195 * Sets the time of this reply.
198 * The time of this reply (in milliseconds since Jan 1, 1970 UTC)
199 * @return This reply (for method chaining)
201 public Reply setTime(long time) {
207 * Returns the text of the reply.
209 * @return The text of the reply
211 public String getText() {
216 * Sets the text of this reply.
219 * The text of this reply
220 * @return This reply (for method chaining)
222 public Reply setText(String text) {
235 public int hashCode() {
236 return id.hashCode();
243 public boolean equals(Object object) {
244 if (!(object instanceof Reply)) {
247 Reply reply = (Reply) object;
248 return reply.id.equals(id);
255 public String toString() {
256 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",post=" + post + ",time=" + time + ",text=" + text + "]";