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 >.
89 lines
2.1 KiB
Java
89 lines
2.1 KiB
Java
package redis.clients.jedis;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import redis.clients.util.SafeEncoder;
|
|
|
|
public class Tuple implements Comparable {
|
|
private byte[] element;
|
|
private Double score;
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
final int prime = 31;
|
|
int result = 1;
|
|
result = prime * result;
|
|
if (null != element) {
|
|
for (final byte b : element) {
|
|
result = prime * result + b;
|
|
}
|
|
}
|
|
long temp;
|
|
temp = Double.doubleToLongBits(score);
|
|
result = prime * result + (int) (temp ^ (temp >>> 32));
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj)
|
|
return true;
|
|
if (obj == null)
|
|
return false;
|
|
if (getClass() != obj.getClass())
|
|
return false;
|
|
Tuple other = (Tuple) obj;
|
|
if (element == null) {
|
|
if (other.element != null)
|
|
return false;
|
|
} else if (!Arrays.equals(element, other.element))
|
|
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);
|
|
this.score = score;
|
|
}
|
|
|
|
public Tuple(byte[] element, Double score) {
|
|
super();
|
|
this.element = element;
|
|
this.score = score;
|
|
}
|
|
|
|
public String getElement() {
|
|
if (null != element) {
|
|
return SafeEncoder.encode(element);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public byte[] getBinaryElement() {
|
|
return element;
|
|
}
|
|
|
|
public double getScore() {
|
|
return score;
|
|
}
|
|
|
|
public String toString() {
|
|
return '[' + Arrays.toString(element) + ',' + score + ']';
|
|
}
|
|
}
|