From 9dd1cff428b0b5c5ea1b2bf73533be820c3619d9 Mon Sep 17 00:00:00 2001 From: Jon Bracy Date: Tue, 8 Mar 2011 16:54:52 -0500 Subject: [PATCH] Compare Tuples based on the element, not the element & score Set uses equals to compare if the set is already in the set. Redis Sorted Sets are unique on the value, not the value with the score. The Tuple.equals should return true when the elements are equal regardless of what the score is. The score should be used when comparing to another element in the set to see if it is < or >. --- src/main/java/redis/clients/jedis/Tuple.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Tuple.java b/src/main/java/redis/clients/jedis/Tuple.java index 6df12d2f38..dd698e0a36 100644 --- a/src/main/java/redis/clients/jedis/Tuple.java +++ b/src/main/java/redis/clients/jedis/Tuple.java @@ -4,7 +4,7 @@ import redis.clients.util.SafeEncoder; -public class Tuple { +public class Tuple implements Comparable { private byte[] element; private Double score; @@ -38,12 +38,22 @@ public boolean equals(Object obj) { return false; } else if (!Arrays.equals(element, other.element)) return false; - if (Double.doubleToLongBits(score) != Double - .doubleToLongBits(other.score)) - return false; return true; } + public int compareTo(Tuple other) { + if (Arrays.equals(this.element, other.element)) + return 0; + else + return this.score < other.getScore() ? -1 : 1; + } + public int compareTo(Object obj) { + if (getClass() != obj.getClass()) + throw new ClassCastException(); + return compareTo((Tuple) obj); + } + + public Tuple(String element, Double score) { super(); this.element = SafeEncoder.encode(element);