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 Summary
Modifier and TypeFieldDescriptionprivate longWhen the newest comment was written.The comments, by time bucket.static final intMaximum number of comments allowed.private static final intTotal text length allowed across the comments, so the set does not hold 256 comments of 512 characters each.private booleanTrue if the set changed since the last save.private intThe rating we gave the torrent.private intThe ratings counted in the sum.private intThe comments including the hidden ones.private intThe comments not hidden.private intThe sum of all the ratings.private intTotal length of the comment texts.Constructor Summary
ModifierConstructorDescriptionprivateBuilds an empty comment set.CommentSet(File file) Creates a CommentSet by reading from a gzipped file.CommentSet(Collection<Comment> coll) Creates a CommentSet from a collection of comments.Method Summary
Modifier and TypeMethodDescriptionbooleanAdds a comment to this set.private voidUpdates statistics when adding a comment.voidclear()Clears all comments including hidden ones.doubleReturns the average rating from all non-hidden, non-local comments.longReturns the timestamp of the most recent non-hidden comment.intReturns the local user's rating, or 0 if none.intReturns the number of ratings making up the average rating.booleanReturns whether this set has been modified since instantiation or last save.iterator()Returns an iterator over non-hidden comments in reverse-sort order (newest first).booleanremove(int id) Hides a comment by its unique ID (as returned byComment.getID()).booleanHides a comment (does not actually remove it from the underlying set).private voidremoveMyOldRatings(int exceptID) Hides all of my ratings with empty text, except the specified ID.private voidUpdates statistics when removing a comment.voidSaves this CommentSet to a gzipped file.intsize()Returns the number of non-hidden comments.Methods inherited from class AbstractSet
equals, hashCode, removeAllMethods inherited from class AbstractCollection
addAll, contains, containsAll, isEmpty, retainAll, toArray, toArray, toStringMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface Set
addAll, contains, containsAll, isEmpty, retainAll, spliterator, toArray, toArray
Field Details
map
size
private int sizeThe comments not hidden.realSize
private int realSizeThe comments including the hidden ones.myRating
private int myRatingThe rating we gave the torrent.totalRating
private int totalRatingThe sum of all the ratings.ratingSize
private int ratingSizeThe ratings counted in the sum.totalTextSize
private int totalTextSizeTotal length of the comment texts.latestCommentTime
private long latestCommentTimeWhen the newest comment was written.modified
private boolean modifiedTrue if the set changed since the last save.MAX_SIZE
MAX_TOTAL_TEXT_LEN
private static final int MAX_TOTAL_TEXT_LENTotal 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
Creates a CommentSet from a collection of comments.- Parameters:
coll- the initial collection of comments
CommentSet
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
Saves this CommentSet to a gzipped file. Not sorted, includes hidden comments. SetsisModified()to false on success.- Parameters:
file- the file to write to- Throws:
IOException- if an I/O error occurs
add
Adds a comment to this set. Enforces max size and max total text length. Checks for duplicates based on approximate time range.- Specified by:
addin interfaceCollection<Comment>- Specified by:
addin interfaceSet<Comment>- Overrides:
addin classAbstractCollection<Comment>- Parameters:
c- the comment to add- Returns:
- true if added, false if duplicate or at capacity
remove
Hides a comment (does not actually remove it from the underlying set).- Specified by:
removein interfaceCollection<Comment>- Specified by:
removein interfaceSet<Comment>- Overrides:
removein classAbstractCollection<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 byComment.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
Updates statistics when adding a comment. The comment may be hidden.- Parameters:
c- the comment to add stats for
removeStats
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:
clearin interfaceCollection<Comment>- Specified by:
clearin interfaceSet<Comment>- Overrides:
clearin classAbstractCollection<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:
sizein interfaceCollection<Comment>- Specified by:
sizein interfaceSet<Comment>- Specified by:
sizein classAbstractCollection<Comment>- Returns:
- the non-hidden count
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.