/*
- * FreenetSone - StatusUpdate.java - Copyright © 2010 David Roden
+ * Sone - Post.java - Copyright © 2010–2013 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
package net.pterodactylus.sone.data;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.UUID;
+import java.util.Comparator;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
/**
* A post is a short message that a user writes in his Sone to let other users
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class Post {
-
- /** The GUID of the post. */
- private final UUID id;
+public interface Post {
- /** The Sone this post belongs to. */
- private final Sone sone;
+ /** Comparator for posts, sorts descending by time. */
+ public static final Comparator<Post> TIME_COMPARATOR = new Comparator<Post>() {
- /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
- private final long time;
-
- /** The text of the post. */
- private final String text;
+ @Override
+ public int compare(Post leftPost, Post rightPost) {
+ return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
+ }
- /** The replies that have been loaded for this post. */
- private final Set<Reply> replies = new HashSet<Reply>();
+ };
- /**
- * Creates a new post.
- *
- * @param sone
- * The Sone this post belongs to
- * @param text
- * The text of the post
- */
- public Post(Sone sone, String text) {
- this(sone, System.currentTimeMillis(), text);
- }
+ /** Filter for posts with timestamps from the future. */
+ public static final Predicate<Post> FUTURE_POSTS_FILTER = new Predicate<Post>() {
- /**
- * Creates a new post.
- *
- * @param sone
- * The Sone this post belongs to
- * @param time
- * The time of the post (in milliseconds since Jan 1, 1970 UTC)
- * @param text
- * The text of the post
- */
- public Post(Sone sone, long time, String text) {
- this(UUID.randomUUID(), sone, time, text);
- }
+ @Override
+ public boolean apply(Post post) {
+ return post.getTime() <= System.currentTimeMillis();
+ }
- /**
- * Creates a new post.
- *
- * @param id
- * The ID of the post
- * @param sone
- * The Sone this post belongs to
- * @param time
- * The time of the post (in milliseconds since Jan 1, 1970 UTC)
- * @param text
- * The text of the post
- */
- public Post(UUID id, Sone sone, long time, String text) {
- this.id = id;
- this.sone = sone;
- this.time = time;
- this.text = text;
- }
+ };
//
// ACCESSORS
*
* @return The ID of the post
*/
- public String getId() {
- return id.toString();
- }
+ public String getId();
/**
* Returns the Sone this post belongs to.
*
* @return The Sone of this post
*/
- public Sone getSone() {
- return sone;
- }
+ public Sone getSone();
/**
- * Returns the time of the post.
+ * Returns the ID of the recipient {@link Sone}, or
+ * {@link Optional#absent()} if this post does not have a recipient.
*
- * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
+ * @return The ID of the recipient, or {@link Optional#absent()}
*/
- public long getTime() {
- return time;
- }
+ public Optional<String> getRecipientId();
/**
- * Returns the text of the post.
+ * Returns the recipient of this post, if any. As this method can return
+ * {@link Optional#absent()} if the post has a recipient which has not yet
+ * been loaded, it is recommended to use {@link #hasRecipient()} to check
+ * for the presence of a recipient.
*
- * @return The text of the post
+ * @return The recipient of this post, or {@link Optional#absent()} if there
+ * is no recipient
*/
- public String getText() {
- return text;
- }
+ public Optional<Sone> getRecipient();
/**
- * Returns all replies to this post in unspecified order.
+ * Returns the time of the post.
*
- * @return All replies to this post
+ * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
*/
- public Collection<Reply> getReplies() {
- return Collections.unmodifiableSet(replies);
- }
+ public long getTime();
/**
- * Adds a reply to this post. The reply will not be added if its
- * {@link Reply#getPost() post} is not equal to this post.
+ * Returns the text of the post.
*
- * @param reply
- * The reply to add
+ * @return The text of the post
*/
- public void addReply(Reply reply) {
- if (reply.getPost().equals(this)) {
- replies.add(reply);
- }
- }
+ public String getText();
/**
- * Removes a reply from this post.
+ * Returns whether this post is known.
*
- * @param reply
- * The reply to remove
- */
- public void removeReply(Reply reply) {
- if (reply.getPost().equals(this)) {
- replies.remove(reply);
- }
- }
-
- //
- // OBJECT METHODS
- //
-
- /**
- * {@inheritDoc}
+ * @return {@code true} if this post is known, {@code false} otherwise
*/
- @Override
- public int hashCode() {
- return id.hashCode();
- }
+ public boolean isKnown();
/**
- * {@inheritDoc}
+ * Sets whether this post is known.
+ *
+ * @param known
+ * {@code true} if this post is known, {@code false} otherwise
+ * @return This post
*/
- @Override
- public boolean equals(Object object) {
- if (!(object instanceof Post)) {
- return false;
- }
- return ((Post) object).id.equals(id);
- }
+ public Post setKnown(boolean known);
}