Implement equals and hashCode, move compareTo into MessageSnap class.

This commit is contained in:
Keir 2015-11-26 23:59:17 +00:00
parent 3e06451ac1
commit 0ee042f192
3 changed files with 33 additions and 26 deletions

View File

@ -13,8 +13,6 @@ import com.google.common.cache.CacheBuilder;
*/
public class ChatSnapManager
{
private static final MessageSnapComparator COMPARATOR = new MessageSnapComparator();
// There aren't any List or Set caching implementations
// For an easy work around, we store values as the Key
// For the value we just use some dummy object
@ -43,7 +41,7 @@ public class ChatSnapManager
*/
public Set<MessageSnap> getMessageSnapsInvolving(UUID search)
{
Set<MessageSnap> matches = new TreeSet<>(COMPARATOR);
Set<MessageSnap> matches = new TreeSet<>();
for (MessageSnap messageSnap : _snapshots.asMap().keySet())
{

View File

@ -2,16 +2,19 @@ package mineplex.core.chatsnap;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import org.bukkit.ChatColor;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Represents a message sent by a player.
* @author iKeirNez
*/
public class MessageSnap
public class MessageSnap implements Comparable<MessageSnap>
{
private UUID sender;
private Collection<UUID> recipients;
@ -25,10 +28,10 @@ public class MessageSnap
public MessageSnap(UUID sender, Collection<UUID> recipients, String message, long time)
{
this.sender = sender;
this.recipients = recipients;
this.message = message;
this.time = time;
this.sender = checkNotNull(sender);
this.recipients = checkNotNull(recipients);
this.message = checkNotNull(message);
this.time = checkNotNull(time);
}
public UUID getSender()
@ -51,6 +54,30 @@ public class MessageSnap
return time;
}
@Override
public int compareTo(MessageSnap o)
{
return Long.compare(getTime(), o.getTime());
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MessageSnap that = (MessageSnap) o;
return time == that.time &&
Objects.equals(sender, that.sender) &&
Objects.equals(recipients, that.recipients) &&
Objects.equals(message, that.message);
}
@Override
public int hashCode()
{
return Objects.hash(sender, recipients, message, time);
}
@Override
public String toString()
{

View File

@ -1,18 +0,0 @@
package mineplex.core.chatsnap;
import java.util.Comparator;
/**
* Compares two message snaps based on the time they were sent.
* @author iKeirNez
*/
public class MessageSnapComparator implements Comparator<MessageSnap>
{
@Override
public int compare(MessageSnap first, MessageSnap second)
{
long firstTime = first.getTime();
long secondTime = second.getTime();
return Long.compare(firstTime, secondTime);
}
}