All Implemented Interfaces:
Iterable<Comment>, Collection<Comment>, Set<Comment>

public class CommentSetextends AbstractSet<Comment>
Store comments.

Optimized for fast checking of duplicates, and retrieval of ratings. Removes are not really removed, only marked as hidden, so they don't reappear. Duplicates are detected based on an approximate time range. Max size of both elements and total text length is enforced.

Supports persistence via save(File) and File constructor.

NOT THREAD SAFE except for iterating AFTER the iterator() call.

  • Field Details

    • map

      private final HashMap<Integer, List<Comment>> map
      The comments, by time bucket.
    • size

      private int size
      The comments not hidden.
    • realSize

      private int realSize
      The comments including the hidden ones.
    • myRating

      private int myRating
      The rating we gave the torrent.
    • totalRating

      private int totalRating
      The sum of all the ratings.
    • ratingSize

      private int ratingSize
      The ratings counted in the sum.
    • totalTextSize

      private int totalTextSize
      Total length of the comment texts.
    • latestCommentTime

      private long latestCommentTime
      When the newest comment was written.
    • modified

      private boolean modified
      True if the set changed since the last save.
    • MAX_SIZE

      public static final int MAX_SIZE
      Maximum number of comments allowed.
      See Also:
    • MAX_TOTAL_TEXT_LEN

      private static final int MAX_TOTAL_TEXT_LEN
      Total text length allowed across the comments, so the set does not hold 256 comments of 512 characters each.
      See Also:
  • Constructor Details

    • CommentSet

      private CommentSet()
      Builds an empty comment set.
    • CommentSet

      public CommentSet(Collection<Comment> coll)
      Creates a CommentSet from a collection of comments.
      Parameters:
      coll - the initial collection of comments
    • CommentSet

      public CommentSet(File file) throws IOException
      Creates a CommentSet by reading from a gzipped file.
      Parameters:
      file - the gzipped file to read from
      Throws:
      IOException - if an I/O error occurs
  • Method Details

    • save

      public void save(File file) throws IOException
      Saves this CommentSet to a gzipped file. Not sorted, includes hidden comments. Sets isModified() to false on success.
      Parameters:
      file - the file to write to
      Throws:
      IOException - if an I/O error occurs
    • add

      public boolean add(Comment c)
      Adds a comment to this set. Enforces max size and max total text length. Checks for duplicates based on approximate time range.
      Specified by:
      add in interface Collection<Comment>
      Specified by:
      add in interface Set<Comment>
      Overrides:
      add in class AbstractCollection<Comment>
      Parameters:
      c - the comment to add
      Returns:
      true if added, false if duplicate or at capacity
    • remove

      public boolean remove(Object o)
      Hides a comment (does not actually remove it from the underlying set).
      Specified by:
      remove in interface Collection<Comment>
      Specified by:
      remove in interface Set<Comment>
      Overrides:
      remove in class AbstractCollection<Comment>
      Parameters:
      o - the Comment to hide
      Returns:
      true if present and not previously hidden
    • remove

      public boolean remove(int id)
      Hides a comment by its unique ID (as returned by Comment.getID()).
      Parameters:
      id - the comment ID to hide
      Returns:
      true if present and not previously hidden
    • removeMyOldRatings

      private void removeMyOldRatings(int exceptID)
      Hides all of my ratings with empty text, except the specified ID.
      Parameters:
      exceptID - the comment ID to preserve
    • addStats

      private void addStats(Comment c)
      Updates statistics when adding a comment. The comment may be hidden.
      Parameters:
      c - the comment to add stats for
    • removeStats

      private void removeStats(Comment c)
      Updates statistics when removing a comment. Call before setting hidden.
      Parameters:
      c - the comment to remove stats for
    • getLatestCommentTime

      public long getLatestCommentTime()
      Returns the timestamp of the most recent non-hidden comment. Not adjusted when the latest comment is subsequently hidden.
      Returns:
      the timestamp in milliseconds
    • isModified

      public boolean isModified()
      Returns whether this set has been modified since instantiation or last save.
      Returns:
      true if modified
    • getMyRating

      public int getMyRating()
      Returns the local user's rating, or 0 if none.
      Returns:
      0 if none, or 1-5
    • getRatingCount

      public int getRatingCount()
      Returns the number of ratings making up the average rating.
      Returns:
      the count of ratings
    • getAverageRating

      public double getAverageRating()
      Returns the average rating from all non-hidden, non-local comments.
      Returns:
      0 if none, or 1-5
    • clear

      public void clear()
      Clears all comments including hidden ones. Resets all statistics to zero.
      Specified by:
      clear in interface Collection<Comment>
      Specified by:
      clear in interface Set<Comment>
      Overrides:
      clear in class AbstractCollection<Comment>
    • size

      public int size()
      Returns the number of non-hidden comments. May be more than what iterator() returns due to additional deduping.
      Specified by:
      size in interface Collection<Comment>
      Specified by:
      size in interface Set<Comment>
      Specified by:
      size in class AbstractCollection<Comment>
      Returns:
      the non-hidden count
    • iterator

      public Iterator<Comment> iterator()
      Returns an iterator over non-hidden comments in reverse-sort order (newest first). Thread-safe after this call. Changes after this call are not reflected. iter.remove(Object) has no effect on the underlying set.

      Returned values may be fewer than size() due to additional deduping.

      Specified by:
      iterator in interface Collection<Comment>
      Specified by:
      iterator in interface Iterable<Comment>
      Specified by:
      iterator in interface Set<Comment>
      Specified by:
      iterator in class AbstractCollection<Comment>
      Returns:
      an iterator over non-hidden comments