Filter posts and replies from the future.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Reply.java
index c97d57b..2dacfbe 100644 (file)
 
 package net.pterodactylus.sone.data;
 
+import java.util.Comparator;
 import java.util.UUID;
 
+import net.pterodactylus.util.filter.Filter;
+
 /**
  * A reply is like a {@link Post} but can never be posted on its own, it always
  * refers to another {@link Post}.
@@ -27,20 +30,50 @@ import java.util.UUID;
  */
 public class Reply {
 
+       /** Comparator that sorts replies ascending by time. */
+       public static final Comparator<Reply> TIME_COMPARATOR = new Comparator<Reply>() {
+
+               @Override
+               public int compare(Reply leftReply, Reply rightReply) {
+                       return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
+               }
+
+       };
+
+       /** Filter for replies with timestamps from the future. */
+       public static final Filter<Reply> FUTURE_REPLIES_FILTER = new Filter<Reply>() {
+
+               @Override
+               public boolean filterObject(Reply reply) {
+                       return reply.getTime() <= System.currentTimeMillis();
+               }
+
+       };
+
        /** The ID of the reply. */
        private final UUID id;
 
        /** The Sone that posted this reply. */
-       private Sone sone;
+       private volatile Sone sone;
 
        /** The Post this reply refers to. */
-       private Post post;
+       private volatile Post post;
 
        /** The time of the reply. */
-       private long time;
+       private volatile long time;
 
        /** The text of the reply. */
-       private String text;
+       private volatile String text;
+
+       /**
+        * Creates a new reply.
+        *
+        * @param id
+        *            The ID of the reply
+        */
+       public Reply(String id) {
+               this(id, null, null, 0, null);
+       }
 
        /**
         * Creates a new reply.
@@ -200,7 +233,7 @@ public class Reply {
         */
        @Override
        public int hashCode() {
-               return sone.hashCode() ^ id.hashCode() ^ post.hashCode() ^ (int) (time >> 32) ^ (int) (time & 0xffffffff) ^ text.hashCode();
+               return id.hashCode();
        }
 
        /**
@@ -212,7 +245,7 @@ public class Reply {
                        return false;
                }
                Reply reply = (Reply) object;
-               return reply.sone.equals(sone) && reply.id.equals(id) && reply.post.equals(post) && (reply.time == time) && reply.text.equals(text);
+               return reply.id.equals(id);
        }
 
        /**