2 * Sone - Post.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 post is a short message that a user writes in his Sone to let other users
27 * know what is going on.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 /** Comparator for posts, sorts descending by time. */
34 public static final Comparator<Post> TIME_COMPARATOR = new Comparator<Post>() {
37 public int compare(Post leftPost, Post rightPost) {
38 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
43 /** Filter for posts with timestamps from the future. */
44 public static final Filter<Post> FUTURE_POSTS_FILTER = new Filter<Post>() {
47 public boolean filterObject(Post post) {
48 return post.getTime() <= System.currentTimeMillis();
53 /** The GUID of the post. */
54 private final UUID id;
56 /** The Sone this post belongs to. */
57 private volatile Sone sone;
59 /** The Sone of the recipient. */
60 private volatile Sone recipient;
62 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
63 private volatile long time;
65 /** The text of the post. */
66 private volatile String text;
68 /** Whether the post is known. */
69 private volatile boolean known;
77 public Post(String id) {
78 this(id, null, 0, null);
85 * The Sone this post belongs to
87 * The text of the post
89 public Post(Sone sone, String text) {
90 this(sone, System.currentTimeMillis(), text);
97 * The Sone this post belongs to
99 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
101 * The text of the post
103 public Post(Sone sone, long time, String text) {
104 this(UUID.randomUUID().toString(), sone, time, text);
108 * Creates a new post.
113 * The Sone this post belongs to
115 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
117 * The text of the post
119 public Post(String id, Sone sone, long time, String text) {
120 this.id = UUID.fromString(id);
131 * Returns the ID of the post.
133 * @return The ID of the post
135 public String getId() {
136 return id.toString();
140 * Returns the Sone this post belongs to.
142 * @return The Sone of this post
144 public Sone getSone() {
149 * Sets the Sone of this post.
152 * The Sone of this post
153 * @return This post (for method chaining)
155 public Post setSone(Sone sone) {
161 * Returns the recipient of this post, if any.
163 * @return The recipient of this post, or {@code null}
165 public Sone getRecipient() {
170 * Sets the recipient of this post.
173 * The recipient of this post, or {@code null}
174 * @return This post (for method chaining)
176 public Post setRecipient(Sone recipient) {
177 if (!sone.equals(recipient)) {
178 this.recipient = recipient;
184 * Returns the time of the post.
186 * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
188 public long getTime() {
193 * Sets the time of this post.
196 * The time of this post (in milliseconds since Jan 1, 1970 UTC)
197 * @return This post (for method chaining)
199 public Post setTime(long time) {
205 * Returns the text of the post.
207 * @return The text of the post
209 public String getText() {
214 * Sets the text of this post.
217 * The text of this post
218 * @return This post (for method chaining)
220 public Post setText(String text) {
226 * Returns whether this post is known.
228 * @return {@code true} if this post is known, {@code false} otherwise
230 public boolean isKnown() {
235 * Sets whether this post is known.
238 * {@code true} if this post is known, {@code false} otherwise
241 public Post setKnown(boolean known) {
254 public int hashCode() {
255 return id.hashCode();
262 public boolean equals(Object object) {
263 if (!(object instanceof Post)) {
266 Post post = (Post) object;
267 return post.id.equals(id);
274 public String toString() {
275 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";