2 * FreenetSone - StatusUpdate.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.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.HashSet;
24 import java.util.List;
26 import java.util.UUID;
29 * A post is a short message that a user writes in his Sone to let other users
30 * know what is going on.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 /** The GUID of the post. */
37 private final UUID id;
39 /** The Sone this post belongs to. */
42 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
45 /** The text of the post. */
48 /** The replies that have been loaded for this post. */
49 private final Set<Reply> replies = new HashSet<Reply>();
57 public Post(String id) {
58 this(id, null, 0, null);
65 * The Sone this post belongs to
67 * The text of the post
69 public Post(Sone sone, String text) {
70 this(sone, System.currentTimeMillis(), text);
77 * The Sone this post belongs to
79 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
81 * The text of the post
83 public Post(Sone sone, long time, String text) {
84 this(UUID.randomUUID().toString(), sone, time, text);
93 * The Sone this post belongs to
95 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
97 * The text of the post
99 public Post(String id, Sone sone, long time, String text) {
100 this.id = UUID.fromString(id);
111 * Returns the ID of the post.
113 * @return The ID of the post
115 public String getId() {
116 return id.toString();
120 * Returns the Sone this post belongs to.
122 * @return The Sone of this post
124 public Sone getSone() {
129 * Sets the Sone of this post.
132 * The Sone of this post
133 * @return This post (for method chaining)
135 public Post setSone(Sone sone) {
141 * Returns the time of the post.
143 * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
145 public long getTime() {
150 * Sets the time of this post.
153 * The time of this post (in milliseconds since Jan 1, 1970 UTC)
154 * @return This post (for method chaining)
156 public Post setTime(long time) {
162 * Returns the text of the post.
164 * @return The text of the post
166 public String getText() {
171 * Sets the text of this post.
174 * The text of this post
175 * @return This post (for method chaining)
177 public Post setText(String text) {
183 * Returns all replies to this post in unspecified order.
185 * @return All replies to this post
187 public List<Reply> getReplies() {
188 List<Reply> sortedReplies = new ArrayList<Reply>(replies);
189 Collections.sort(sortedReplies, new Comparator<Reply>() {
192 public int compare(Reply leftReply, Reply rightReply) {
193 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
197 return sortedReplies;
201 * Adds a reply to this post. The reply will not be added if its
202 * {@link Reply#getPost() post} is not equal to this post.
207 public void addReply(Reply reply) {
208 if (reply.getPost().equals(this)) {
214 * Removes a reply from this post.
217 * The reply to remove
219 public void removeReply(Reply reply) {
220 if (reply.getPost().equals(this)) {
221 replies.remove(reply);
233 public int hashCode() {
234 return id.hashCode() ^ sone.hashCode() ^ (int) (time >> 32) ^ (int) (time & 0xffffffff) ^ text.hashCode();
241 public boolean equals(Object object) {
242 if (!(object instanceof Post)) {
245 Post post = (Post) object;
246 return post.id.equals(id) && post.sone.equals(sone) && (post.time == time) && post.text.equals(text);
253 public String toString() {
254 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + ",replies(" + replies.size() + ")]";