java.lang.Object
org.klomp.snark.comments.Comment
All Implemented Interfaces:
Comparable<Comment>

public class Commentextends Object implements Comparable<Comment>
Store a single comment and/or rating. Unmodifiable except for marking as hidden. Stores a one-second timestamp but designed so identical comments within a certain time frame (bucket) are equal. Don't store in a plain set - see equals(Object).
  • Field Summary

    Modifier and Type
    Field
    Description
    private static final AtomicInteger
    Sequence number of the created comments.
    private static final int
    Width of the comment time buckets.
    private final boolean
    True if we wrote the comment.
    private boolean
    True to keep the comment out of the display.
    private final int
    This comment's position in that sequence.
    static final int
    Maximum length of a commenter name.
    private static final int
    Largest clock skew accepted in a comment.
    private static final int
    Longest comment text accepted, the IRC limit more or less.
    private final String
    The name of the commenter.
    private final byte
    The rating the commenter gave.
    private final String
    The comment text.
    private final int
    When the comment was written, seconds since 1/1/2005.
    private static final long
    Epoch of the comment times, 1/1/2005.
    private static final long
    Factor shrinking the bucket times.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Comment(String text, String name, int rating)
    My comment, now.
    Comment(String text, String name, int rating, long time, boolean isMine)
    Create a comment with the specified text, name, rating, time, and ownership.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Compares comments in reverse chronological order (newest first).
    private static boolean
    eq(String lhs, String rhs)
    Compares two possibly null strings for equality.
    boolean
    Comments within 10 minutes (not necessarily in same bucket) and otherwise equal are considered equal.
    boolean
    Checks equality ignoring the timestamp, comparing only rating, text, and name.
    static Comment
    Parses a comment from its persistent string representation.
    int
    Returns a unique ID for this comment, usable with CommentSet.remove(Object)int).
    Returns the commenter's name.
    int
    Returns the rating (0-5).
    Returns the comment text.
    long
    Returns the comment timestamp in java time (milliseconds since epoch).
    int
    Returns the bucket number for hash-based grouping.
    boolean
    Returns whether this comment is hidden (deleted by the user).
    boolean
    Returns whether this comment was posted by the local user.
    (package private) void
    Mark this comment as hidden.
    Converts this comment to a persistent string representation.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • text

      private final String text
      The comment text.
    • name

      private final String name
      The name of the commenter.
    • time

      private final int time
      When the comment was written, seconds since 1/1/2005.
    • rating

      private final byte rating
      The rating the commenter gave.
    • byMe

      private final boolean byMe
      True if we wrote the comment.
    • hidden

      private boolean hidden
      True to keep the comment out of the display.
    • _id

      private static final AtomicInteger _id
      Sequence number of the created comments.
    • id

      private final int id
      This comment's position in that sequence.
    • MAX_NAME_LEN

      public static final int MAX_NAME_LEN
      Maximum length of a commenter name.
      See Also:
    • MAX_TEXT_LEN

      private static final int MAX_TEXT_LEN
      Longest comment text accepted, the IRC limit more or less.
      See Also:
    • BUCKET_SIZE

      private static final int BUCKET_SIZE
      Width of the comment time buckets.
      See Also:
    • TIME_SHRINK

      private static final long TIME_SHRINK
      Factor shrinking the bucket times.
      See Also:
    • MAX_SKEW

      private static final int MAX_SKEW
      Largest clock skew accepted in a comment.
      See Also:
    • TIME_OFFSET

      private static final long TIME_OFFSET
      Epoch of the comment times, 1/1/2005.
      See Also:
  • Constructor Details

    • Comment

      public Comment(String text, String name, int rating)
      My comment, now.
      Parameters:
      text - may be null, will be truncated to max length, newlines replaced with spaces
      name - may be null, will be truncated to max length, newlines and commas removed
      rating - 0-5
    • Comment

      public Comment(String text, String name, int rating, long time, boolean isMine)
      Create a comment with the specified text, name, rating, time, and ownership.
      Parameters:
      text - may be null, will be truncated to max length, newlines replaced with spaces
      name - may be null, will be truncated to max length, newlines and commas removed
      rating - 0-5
      time - java time (ms)
      isMine - whether this comment is from the local user
  • Method Details

    • getText

      public String getText()
      Returns the comment text.
      Returns:
      the text, or null if no text
    • getName

      public String getName()
      Returns the commenter's name.
      Returns:
      the name, or null if anonymous
    • getRating

      public int getRating()
      Returns the rating (0-5).
      Returns:
      the rating value
    • getTime

      public long getTime()
      Returns the comment timestamp in java time (milliseconds since epoch).
      Returns:
      the scaled offset timestamp, or 0 if the stored value is below the base offset
    • isMine

      public boolean isMine()
      Returns whether this comment was posted by the local user.
      Returns:
      true if the comment is from the local user
    • isHidden

      public boolean isHidden()
      Returns whether this comment is hidden (deleted by the user).
      Returns:
      true if hidden
    • setHidden

      void setHidden()
      Mark this comment as hidden.
    • getID

      public int getID()
      Returns a unique ID for this comment, usable with CommentSet.remove(Object)int). Not persisted across restarts.
      Returns:
      the unique ID
    • compareTo

      public int compareTo(Comment c)
      Compares comments in reverse chronological order (newest first). Falls back to rating, then name, then text for ties.
      Specified by:
      compareTo in interface Comparable<Comment>
      Parameters:
      c - the other Comment to compare to
      Returns:
      negative if this comment is newer
    • toPersistentString

      public String toPersistentString()
      Converts this comment to a persistent string representation.
      Returns:
      comma-separated string of time,rating,mine,hidden,name,text
    • fromPersistentString

      public static Comment fromPersistentString(String s)
      Parses a comment from its persistent string representation.
      Parameters:
      s - the persistent string
      Returns:
      the parsed Comment, or null if parsing fails
    • hashCode

      public int hashCode()
      Returns the bucket number for hash-based grouping.
      Overrides:
      hashCode in class Object
      Returns:
      the bucket number
    • equals

      public boolean equals(Object o)
      Comments within 10 minutes (not necessarily in same bucket) and otherwise equal are considered equal. Violates contract, as equal objects may have different hashcodes and be in adjacent buckets.
      Overrides:
      equals in class Object
    • equalsIgnoreTimestamp

      public boolean equalsIgnoreTimestamp(Comment c)
      Checks equality ignoring the timestamp, comparing only rating, text, and name.
      Parameters:
      c - the other Comment to compare to
      Returns:
      true if rating, text, and name match
    • eq

      private static boolean eq(String lhs, String rhs)
      Compares two possibly null strings for equality.
      Parameters:
      lhs - the first string, null allowed
      rhs - the second string, null allowed
      Returns:
      true if both are null or equal