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;
74 public Post(String id) {
75 this(id, null, 0, null);
82 * The Sone this post belongs to
84 * The text of the post
86 public Post(Sone sone, String text) {
87 this(sone, System.currentTimeMillis(), text);
94 * The Sone this post belongs to
96 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
98 * The text of the post
100 public Post(Sone sone, long time, String text) {
101 this(UUID.randomUUID().toString(), sone, time, text);
105 * Creates a new post.
110 * The Sone this post belongs to
112 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
114 * The text of the post
116 public Post(String id, Sone sone, long time, String text) {
117 this.id = UUID.fromString(id);
128 * Returns the ID of the post.
130 * @return The ID of the post
132 public String getId() {
133 return id.toString();
137 * Returns the Sone this post belongs to.
139 * @return The Sone of this post
141 public Sone getSone() {
146 * Sets the Sone of this post.
149 * The Sone of this post
150 * @return This post (for method chaining)
152 public Post setSone(Sone sone) {
158 * Returns the recipient of this post, if any.
160 * @return The recipient of this post, or {@code null}
162 public Sone getRecipient() {
167 * Sets the recipient of this post.
170 * The recipient of this post, or {@code null}
171 * @return This post (for method chaining)
173 public Post setRecipient(Sone recipient) {
174 if (!sone.equals(recipient)) {
175 this.recipient = recipient;
181 * Returns the time of the post.
183 * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
185 public long getTime() {
190 * Sets the time of this post.
193 * The time of this post (in milliseconds since Jan 1, 1970 UTC)
194 * @return This post (for method chaining)
196 public Post setTime(long time) {
202 * Returns the text of the post.
204 * @return The text of the post
206 public String getText() {
211 * Sets the text of this post.
214 * The text of this post
215 * @return This post (for method chaining)
217 public Post setText(String text) {
230 public int hashCode() {
231 return id.hashCode();
238 public boolean equals(Object object) {
239 if (!(object instanceof Post)) {
242 Post post = (Post) object;
243 return post.id.equals(id);
250 public String toString() {
251 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";