Added more sorting options
This commit is contained in:
@@ -1,20 +1,42 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SortingParams {
|
||||
private String pattern = null;
|
||||
private List<String> params = new ArrayList<String>();
|
||||
|
||||
public SortingParams by(String pattern) {
|
||||
this.pattern = pattern;
|
||||
params.add("BY");
|
||||
params.add(pattern);
|
||||
return this;
|
||||
params.add("BY");
|
||||
params.add(pattern);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String[] getParams() {
|
||||
return params.toArray(new String[params.size()]);
|
||||
public Collection<String> getParams() {
|
||||
return Collections.unmodifiableCollection(params);
|
||||
}
|
||||
}
|
||||
|
||||
public SortingParams desc() {
|
||||
params.add("DESC");
|
||||
return this;
|
||||
}
|
||||
|
||||
public SortingParams asc() {
|
||||
params.add("ASC");
|
||||
return this;
|
||||
}
|
||||
|
||||
public SortingParams limit(int start, int count) {
|
||||
params.add("LIMIT");
|
||||
params.add(String.valueOf(start));
|
||||
params.add(String.valueOf(count));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SortingParams alpha() {
|
||||
params.add("ALPHA");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ public class SortingCommandsTest extends Assert {
|
||||
public void setUp() throws Exception {
|
||||
jedis = new Jedis("localhost");
|
||||
jedis.connect();
|
||||
jedis.flushDB();
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -45,20 +46,58 @@ public class SortingCommandsTest extends Assert {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortBy() throws JedisException {
|
||||
public void sortDesc() throws JedisException {
|
||||
jedis.lpush("foo", "3");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "1");
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.by("bar_*");
|
||||
sp.desc();
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("foo_1");
|
||||
expected.add("foo_2");
|
||||
expected.add("foo_3");
|
||||
expected.add("3");
|
||||
expected.add("2");
|
||||
expected.add("1");
|
||||
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortLimit() throws JedisException {
|
||||
for (int n = 10; n > 0; n--) {
|
||||
jedis.lpush("foo", String.valueOf(n));
|
||||
}
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.limit(0, 3);
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
expected.add("3");
|
||||
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortAlpha() throws JedisException {
|
||||
jedis.lpush("foo", "1");
|
||||
jedis.lpush("foo", "2");
|
||||
jedis.lpush("foo", "10");
|
||||
|
||||
SortingParams sp = new SortingParams();
|
||||
sp.alpha();
|
||||
|
||||
List<String> result = jedis.sort("foo", sp);
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("1");
|
||||
expected.add("10");
|
||||
expected.add("2");
|
||||
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user