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;
24 * A reply is like a {@link Post} but can never be posted on its own, it always
25 * refers to another {@link Post}.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 /** Comparator that sorts replies ascending by time. */
32 public static final Comparator<Reply> TIME_COMPARATOR = new Comparator<Reply>() {
35 public int compare(Reply leftReply, Reply rightReply) {
36 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
41 /** The ID of the reply. */
42 private final UUID id;
44 /** The Sone that posted this reply. */
45 private volatile Sone sone;
47 /** The Post this reply refers to. */
48 private volatile Post post;
50 /** The time of the reply. */
51 private volatile long time;
53 /** The text of the reply. */
54 private volatile String text;
57 * Creates a new reply.
62 public Reply(String id) {
63 this(id, null, null, 0, null);
67 * Creates a new reply.
70 * The sone that posted the reply
72 * The post to reply to
74 * The text of the reply
76 public Reply(Sone sone, Post post, String text) {
77 this(sone, post, System.currentTimeMillis(), text);
81 * Creates a new reply-
84 * The sone that posted the reply
86 * The post to reply to
88 * The time of the reply
90 * The text of the reply
92 public Reply(Sone sone, Post post, long time, String text) {
93 this(UUID.randomUUID().toString(), sone, post, time, text);
97 * Creates a new reply-
100 * The sone that posted the reply
102 * The ID of the reply
104 * The post to reply to
106 * The time of the reply
108 * The text of the reply
110 public Reply(String id, Sone sone, Post post, long time, String text) {
111 this.id = UUID.fromString(id);
123 * Returns the ID of the reply.
125 * @return The ID of the reply
127 public String getId() {
128 return id.toString();
132 * Returns the Sone that posted this reply.
134 * @return The Sone that posted this reply
136 public Sone getSone() {
141 * Sets the Sone that posted this reply.
144 * The Sone that posted this reply
145 * @return This reply (for method chaining)
147 public Reply setSone(Sone sone) {
153 * Returns the post this reply refers to.
155 * @return The post this reply refers to
157 public Post getPost() {
162 * Sets the post this reply refers to.
165 * The post this reply refers to
166 * @return This reply (for method chaining)
168 public Reply setPost(Post post) {
174 * Returns the time of the reply.
176 * @return The time of the reply (in milliseconds since Jan 1, 1970 UTC)
178 public long getTime() {
183 * Sets the time of this reply.
186 * The time of this reply (in milliseconds since Jan 1, 1970 UTC)
187 * @return This reply (for method chaining)
189 public Reply setTime(long time) {
195 * Returns the text of the reply.
197 * @return The text of the reply
199 public String getText() {
204 * Sets the text of this reply.
207 * The text of this reply
208 * @return This reply (for method chaining)
210 public Reply setText(String text) {
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 + ",post=" + post + ",time=" + time + ",text=" + text + "]";