Fixing Jedis.(get|set)bit to use use booleans as input and output to better match Redis commands

This commit is contained in:
Eric Hauser
2011-02-22 22:15:08 -05:00
committed by Jonathan Leibiusky
parent dad1b8c394
commit ffebfe120b
4 changed files with 14 additions and 12 deletions

View File

@@ -1,5 +1,7 @@
package redis.clients.jedis; package redis.clients.jedis;
import static redis.clients.jedis.Protocol.toByteArray;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@@ -507,8 +509,8 @@ public class Client extends BinaryClient implements Commands {
timeout); timeout);
} }
public void setbit(final String key, final long offset, final String value) { public void setbit(final String key, final long offset, final boolean value) {
setbit(SafeEncoder.encode(key), offset, SafeEncoder.encode(value)); setbit(SafeEncoder.encode(key), offset, toByteArray(value ? 0 : 1));
} }
public void getbit(String key, long offset) { public void getbit(String key, long offset) {

View File

@@ -2600,9 +2600,9 @@ public class Jedis extends BinaryJedis implements JedisCommands {
* @param value * @param value
* @return * @return
*/ */
public Long setbit(String key, long offset, String value) { public boolean setbit(String key, long offset, boolean value) {
client.setbit(key, offset, value); client.setbit(key, offset, value);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
/** /**
@@ -2612,8 +2612,8 @@ public class Jedis extends BinaryJedis implements JedisCommands {
* @param offset * @param offset
* @return * @return
*/ */
public Long getbit(String key, long offset) { public boolean getbit(String key, long offset) {
client.getbit(key, offset); client.getbit(key, offset);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
} }

View File

@@ -403,14 +403,14 @@ public class Transaction extends BinaryTransaction {
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
public String setbit(String key, long offset, String value) { public String setbit(String key, long offset, boolean value) {
client.setbit(key, offset, value); client.setbit(key, offset, value);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
public String getbit(String key, long offset) { public boolean getbit(String key, long offset) {
client.getbit(key, offset); client.getbit(key, offset);
return client.getStatusCodeReply(); return client.getStatusCodeReply() == "1";
} }
public String linsert(final String key, final LIST_POSITION where, public String linsert(final String key, final LIST_POSITION where,

View File

@@ -5,11 +5,11 @@ import org.junit.Test;
public class BitCommandsTest extends JedisCommandTestBase { public class BitCommandsTest extends JedisCommandTestBase {
@Test @Test
public void setAndgetbit() { public void setAndgetbit() {
long bit = jedis.setbit("foo", 0, "1"); boolean bit = jedis.setbit("foo", 0, true);
assertEquals(0, bit); assertEquals(false, bit);
bit = jedis.getbit("foo", 0); bit = jedis.getbit("foo", 0);
assertEquals(1, bit); assertEquals(false, bit);
long bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes()); long bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes());
assertEquals(0, bbit); assertEquals(0, bbit);