Skip to content

Commit 4e4a296

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAREDIS-294 - TypedTuple should allow sorting using java sort().
Updated Comparable type information to allow usage with standard java sort.
1 parent 6d7c5ce commit 4e4a296

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

src/main/java/org/springframework/data/redis/core/DefaultTypedTuple.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ public boolean equals(Object obj) {
6363
return false;
6464
if (!(obj instanceof DefaultTypedTuple))
6565
return false;
66-
DefaultTypedTuple other = (DefaultTypedTuple) obj;
66+
DefaultTypedTuple<?> other = (DefaultTypedTuple<?>) obj;
6767
if (score == null) {
6868
if (other.score != null)
6969
return false;
@@ -83,8 +83,20 @@ public boolean equals(Object obj) {
8383
}
8484

8585
public int compareTo(Double o) {
86-
Double d = (score == null ? Double.valueOf(0) : score);
87-
Double a = (o == null ? Double.valueOf(0) : o);
88-
return d.compareTo(a);
86+
87+
double thisScore = (score == null ? 0.0 : score);
88+
double otherScore = (o == null ? 0.0 : o);
89+
90+
return Double.compare(thisScore, otherScore);
91+
}
92+
93+
@Override
94+
public int compareTo(TypedTuple<V> o) {
95+
96+
if (o == null) {
97+
return compareTo(Double.valueOf(0));
98+
}
99+
100+
return compareTo(o.getScore());
89101
}
90102
}

src/main/java/org/springframework/data/redis/core/ZSetOperations.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,14 @@
2323
* Redis ZSet/sorted set specific operations.
2424
*
2525
* @author Costin Leau
26+
* @author Christoph Strobl
2627
*/
2728
public interface ZSetOperations<K, V> {
2829

2930
/**
3031
* Typed ZSet tuple.
3132
*/
32-
public interface TypedTuple<V> extends Comparable<Double> {
33+
public interface TypedTuple<V> extends Comparable<TypedTuple<V>> {
3334
V getValue();
3435

3536
Double getScore();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.core;
17+
18+
import static org.hamcrest.core.IsEqual.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
23+
24+
/**
25+
* @author Christoph Strobl
26+
*/
27+
public class DefaultTypedTupleUnitTests {
28+
29+
private static final TypedTuple<String> WITH_SCORE_1 = new DefaultTypedTuple<String>("foo", 1D);
30+
private static final TypedTuple<String> ANOTHER_ONE_WITH_SCORE_1 = new DefaultTypedTuple<String>("another", 1D);
31+
private static final TypedTuple<String> WITH_SCORE_2 = new DefaultTypedTuple<String>("bar", 2D);
32+
private static final TypedTuple<String> WITH_SCORE_NULL = new DefaultTypedTuple<String>("foo", null);
33+
34+
/**
35+
* @see DATAREDIS-294
36+
*/
37+
@Test
38+
public void compareToShouldUseScore() {
39+
40+
assertThat(WITH_SCORE_1.compareTo(WITH_SCORE_2), equalTo(-1));
41+
assertThat(WITH_SCORE_2.compareTo(WITH_SCORE_1), equalTo(1));
42+
assertThat(WITH_SCORE_1.compareTo(ANOTHER_ONE_WITH_SCORE_1), equalTo(0));
43+
}
44+
45+
/**
46+
* @see DATAREDIS-294
47+
*/
48+
@Test
49+
public void compareToShouldConsiderGivenNullAsZeroScore() {
50+
51+
assertThat(WITH_SCORE_1.compareTo(null), equalTo(1));
52+
assertThat(WITH_SCORE_NULL.compareTo(null), equalTo(0));
53+
}
54+
55+
/**
56+
* @see DATAREDIS-294
57+
*/
58+
@Test
59+
public void compareToShouldConsiderNullScoreAsZeroScore() {
60+
61+
assertThat(WITH_SCORE_1.compareTo(WITH_SCORE_NULL), equalTo(1));
62+
assertThat(WITH_SCORE_NULL.compareTo(WITH_SCORE_1), equalTo(-1));
63+
}
64+
}

0 commit comments

Comments
 (0)