no need to instantiate the Protocol class that can be static

This commit is contained in:
KARASZI István
2011-09-26 15:37:15 +02:00
parent b7ee1fe29a
commit 9d4d11ef46
3 changed files with 33 additions and 36 deletions

View File

@@ -19,7 +19,6 @@ public class Connection {
private String host; private String host;
private int port = Protocol.DEFAULT_PORT; private int port = Protocol.DEFAULT_PORT;
private Socket socket; private Socket socket;
private Protocol protocol = new Protocol();
private RedisOutputStream outputStream; private RedisOutputStream outputStream;
private RedisInputStream inputStream; private RedisInputStream inputStream;
private int pipelinedCommands = 0; private int pipelinedCommands = 0;
@@ -78,14 +77,14 @@ public class Connection {
protected Connection sendCommand(final Command cmd, final byte[]... args) { protected Connection sendCommand(final Command cmd, final byte[]... args) {
connect(); connect();
protocol.sendCommand(outputStream, cmd, args); Protocol.sendCommand(outputStream, cmd, args);
pipelinedCommands++; pipelinedCommands++;
return this; return this;
} }
protected Connection sendCommand(final Command cmd) { protected Connection sendCommand(final Command cmd) {
connect(); connect();
protocol.sendCommand(outputStream, cmd, new byte[0][]); Protocol.sendCommand(outputStream, cmd, new byte[0][]);
pipelinedCommands++; pipelinedCommands++;
return this; return this;
} }
@@ -159,7 +158,7 @@ public class Connection {
protected String getStatusCodeReply() { protected String getStatusCodeReply() {
flush(); flush();
pipelinedCommands--; pipelinedCommands--;
final byte[] resp = (byte[]) protocol.read(inputStream); final byte[] resp = (byte[]) Protocol.read(inputStream);
if (null == resp) { if (null == resp) {
return null; return null;
} else { } else {
@@ -179,13 +178,13 @@ public class Connection {
public byte[] getBinaryBulkReply() { public byte[] getBinaryBulkReply() {
flush(); flush();
pipelinedCommands--; pipelinedCommands--;
return (byte[]) protocol.read(inputStream); return (byte[]) Protocol.read(inputStream);
} }
public Long getIntegerReply() { public Long getIntegerReply() {
flush(); flush();
pipelinedCommands--; pipelinedCommands--;
return (Long) protocol.read(inputStream); return (Long) Protocol.read(inputStream);
} }
public List<String> getMultiBulkReply() { public List<String> getMultiBulkReply() {
@@ -196,14 +195,14 @@ public class Connection {
public List<byte[]> getBinaryMultiBulkReply() { public List<byte[]> getBinaryMultiBulkReply() {
flush(); flush();
pipelinedCommands--; pipelinedCommands--;
return (List<byte[]>) protocol.read(inputStream); return (List<byte[]>) Protocol.read(inputStream);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<Object> getObjectMultiBulkReply() { public List<Object> getObjectMultiBulkReply() {
flush(); flush();
pipelinedCommands--; pipelinedCommands--;
return (List<Object>) protocol.read(inputStream); return (List<Object>) Protocol.read(inputStream);
} }
public List<Object> getAll() { public List<Object> getAll() {
@@ -215,7 +214,7 @@ public class Connection {
flush(); flush();
while (pipelinedCommands > except) { while (pipelinedCommands > except) {
try{ try{
all.add(protocol.read(inputStream)); all.add(Protocol.read(inputStream));
}catch(JedisDataException e){ }catch(JedisDataException e){
all.add(e); all.add(e);
} }
@@ -227,6 +226,6 @@ public class Connection {
public Object getOne() { public Object getOne() {
flush(); flush();
pipelinedCommands--; pipelinedCommands--;
return protocol.read(inputStream); return Protocol.read(inputStream);
} }
} }

View File

@@ -24,12 +24,18 @@ public final class Protocol {
public static final byte MINUS_BYTE = '-'; public static final byte MINUS_BYTE = '-';
public static final byte COLON_BYTE = ':'; public static final byte COLON_BYTE = ':';
public void sendCommand(final RedisOutputStream os, final Command command, private Protocol() {
// this prevent the class from instantiation
}
public static void sendCommand(final RedisOutputStream os,
final Command command,
final byte[]... args) { final byte[]... args) {
sendCommand(os, command.raw, args); sendCommand(os, command.raw, args);
} }
private void sendCommand(final RedisOutputStream os, final byte[] command, private static void sendCommand(final RedisOutputStream os,
final byte[] command,
final byte[]... args) { final byte[]... args) {
try { try {
os.write(ASTERISK_BYTE); os.write(ASTERISK_BYTE);
@@ -50,12 +56,12 @@ public final class Protocol {
} }
} }
private void processError(final RedisInputStream is) { private static void processError(final RedisInputStream is) {
String message = is.readLine(); String message = is.readLine();
throw new JedisDataException(message); throw new JedisDataException(message);
} }
private Object process(final RedisInputStream is) { private static Object process(final RedisInputStream is) {
try { try {
byte b = is.readByte(); byte b = is.readByte();
if (b == MINUS_BYTE) { if (b == MINUS_BYTE) {
@@ -77,11 +83,11 @@ public final class Protocol {
return null; return null;
} }
private byte[] processStatusCodeReply(final RedisInputStream is) { private static byte[] processStatusCodeReply(final RedisInputStream is) {
return SafeEncoder.encode(is.readLine()); return SafeEncoder.encode(is.readLine());
} }
private byte[] processBulkReply(final RedisInputStream is) { private static byte[] processBulkReply(final RedisInputStream is) {
int len = Integer.parseInt(is.readLine()); int len = Integer.parseInt(is.readLine());
if (len == -1) { if (len == -1) {
return null; return null;
@@ -102,12 +108,12 @@ public final class Protocol {
return read; return read;
} }
private Long processInteger(final RedisInputStream is) { private static Long processInteger(final RedisInputStream is) {
String num = is.readLine(); String num = is.readLine();
return Long.valueOf(num); return Long.valueOf(num);
} }
private List<Object> processMultiBulkReply(final RedisInputStream is) { private static List<Object> processMultiBulkReply(final RedisInputStream is) {
int num = Integer.parseInt(is.readLine()); int num = Integer.parseInt(is.readLine());
if (num == -1) { if (num == -1) {
return null; return null;
@@ -123,7 +129,7 @@ public final class Protocol {
return ret; return ret;
} }
public Object read(final RedisInputStream is) { public static Object read(final RedisInputStream is) {
return process(is); return process(is);
} }

View File

@@ -24,8 +24,7 @@ public class ProtocolTest extends JedisTestBase {
PipedOutputStream pos = new PipedOutputStream(pis); PipedOutputStream pos = new PipedOutputStream(pis);
RedisOutputStream ros = new RedisOutputStream(pos); RedisOutputStream ros = new RedisOutputStream(pos);
Protocol protocol = new Protocol(); Protocol.sendCommand(ros, Protocol.Command.GET,
protocol.sendCommand(ros, Protocol.Command.GET,
"SOMEKEY".getBytes(Protocol.CHARSET)); "SOMEKEY".getBytes(Protocol.CHARSET));
ros.flush(); ros.flush();
pos.close(); pos.close();
@@ -43,8 +42,7 @@ public class ProtocolTest extends JedisTestBase {
@Test @Test
public void bulkReply() { public void bulkReply() {
InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes()); InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes());
Protocol protocol = new Protocol(); byte[] response = (byte[]) Protocol.read(new RedisInputStream(is));
byte[] response = (byte[]) protocol.read(new RedisInputStream(is));
assertArrayEquals(SafeEncoder.encode("foobar"), response); assertArrayEquals(SafeEncoder.encode("foobar"), response);
} }
@@ -52,8 +50,7 @@ public class ProtocolTest extends JedisTestBase {
public void fragmentedBulkReply() { public void fragmentedBulkReply() {
FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream( FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream(
"$30\r\n012345678901234567890123456789\r\n".getBytes()); "$30\r\n012345678901234567890123456789\r\n".getBytes());
Protocol protocol = new Protocol(); byte[] response = (byte[]) Protocol.read(new RedisInputStream(fis));
byte[] response = (byte[]) protocol.read(new RedisInputStream(fis));
assertArrayEquals(SafeEncoder.encode("012345678901234567890123456789"), assertArrayEquals(SafeEncoder.encode("012345678901234567890123456789"),
response); response);
} }
@@ -61,24 +58,21 @@ public class ProtocolTest extends JedisTestBase {
@Test @Test
public void nullBulkReply() { public void nullBulkReply() {
InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes()); InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes());
Protocol protocol = new Protocol(); String response = (String) Protocol.read(new RedisInputStream(is));
String response = (String) protocol.read(new RedisInputStream(is));
assertEquals(null, response); assertEquals(null, response);
} }
@Test @Test
public void singleLineReply() { public void singleLineReply() {
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes()); InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
Protocol protocol = new Protocol(); byte[] response = (byte[]) Protocol.read(new RedisInputStream(is));
byte[] response = (byte[]) protocol.read(new RedisInputStream(is));
assertArrayEquals(SafeEncoder.encode("OK"), response); assertArrayEquals(SafeEncoder.encode("OK"), response);
} }
@Test @Test
public void integerReply() { public void integerReply() {
InputStream is = new ByteArrayInputStream(":123\r\n".getBytes()); InputStream is = new ByteArrayInputStream(":123\r\n".getBytes());
Protocol protocol = new Protocol(); long response = (Long) Protocol.read(new RedisInputStream(is));
long response = (Long) protocol.read(new RedisInputStream(is));
assertEquals(123, response); assertEquals(123, response);
} }
@@ -88,8 +82,7 @@ public class ProtocolTest extends JedisTestBase {
InputStream is = new ByteArrayInputStream( InputStream is = new ByteArrayInputStream(
"*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n" "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n"
.getBytes()); .getBytes());
Protocol protocol = new Protocol(); List<byte[]> response = (List<byte[]>) Protocol
List<byte[]> response = (List<byte[]>) protocol
.read(new RedisInputStream(is)); .read(new RedisInputStream(is));
List<byte[]> expected = new ArrayList<byte[]>(); List<byte[]> expected = new ArrayList<byte[]>();
expected.add(SafeEncoder.encode("foo")); expected.add(SafeEncoder.encode("foo"));
@@ -104,8 +97,7 @@ public class ProtocolTest extends JedisTestBase {
@Test @Test
public void nullMultiBulkReply() { public void nullMultiBulkReply() {
InputStream is = new ByteArrayInputStream("*-1\r\n".getBytes()); InputStream is = new ByteArrayInputStream("*-1\r\n".getBytes());
Protocol protocol = new Protocol(); List<String> response = (List<String>) Protocol
List<String> response = (List<String>) protocol
.read(new RedisInputStream(is)); .read(new RedisInputStream(is));
assertNull(response); assertNull(response);
} }