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.Comparator;
21 import java.util.UUID;
24 * A post is a short message that a user writes in his Sone to let other users
25 * know what is going on.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 /** Comparator for posts, sorts descending by time. */
32 public static final Comparator<Post> TIME_COMPARATOR = new Comparator<Post>() {
35 public int compare(Post leftPost, Post rightPost) {
36 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
41 /** The GUID of the post. */
42 private final UUID id;
44 /** The Sone this post belongs to. */
45 private volatile Sone sone;
47 /** The Sone of the recipient. */
48 private volatile Sone recipient;
50 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
51 private volatile long time;
53 /** The text of the post. */
54 private volatile String text;
62 public Post(String id) {
63 this(id, null, 0, null);
70 * The Sone this post belongs to
72 * The text of the post
74 public Post(Sone sone, String text) {
75 this(sone, System.currentTimeMillis(), text);
82 * The Sone this post belongs to
84 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
86 * The text of the post
88 public Post(Sone sone, long time, String text) {
89 this(UUID.randomUUID().toString(), sone, time, text);
98 * The Sone this post belongs to
100 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
102 * The text of the post
104 public Post(String id, Sone sone, long time, String text) {
105 this.id = UUID.fromString(id);
116 * Returns the ID of the post.
118 * @return The ID of the post
120 public String getId() {
121 return id.toString();
125 * Returns the Sone this post belongs to.
127 * @return The Sone of this post
129 public Sone getSone() {
134 * Sets the Sone of this post.
137 * The Sone of this post
138 * @return This post (for method chaining)
140 public Post setSone(Sone sone) {
146 * Returns the recipient of this post, if any.
148 * @return The recipient of this post, or {@code null}
150 public Sone getRecipient() {
155 * Sets the recipient of this post.
158 * The recipient of this post, or {@code null}
159 * @return This post (for method chaining)
161 public Post setRecipient(Sone recipient) {
162 if (!sone.equals(recipient)) {
163 this.recipient = recipient;
169 * Returns the time of the post.
171 * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
173 public long getTime() {
178 * Sets the time of this post.
181 * The time of this post (in milliseconds since Jan 1, 1970 UTC)
182 * @return This post (for method chaining)
184 public Post setTime(long time) {
190 * Returns the text of the post.
192 * @return The text of the post
194 public String getText() {
199 * Sets the text of this post.
202 * The text of this post
203 * @return This post (for method chaining)
205 public Post setText(String text) {
218 public int hashCode() {
219 return id.hashCode();
226 public boolean equals(Object object) {
227 if (!(object instanceof Post)) {
230 Post post = (Post) object;
231 return post.id.equals(id);
238 public String toString() {
239 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";