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 int port = Protocol.DEFAULT_PORT;
private Socket socket;
private Protocol protocol = new Protocol();
private RedisOutputStream outputStream;
private RedisInputStream inputStream;
private int pipelinedCommands = 0;
@@ -78,14 +77,14 @@ public class Connection {
protected Connection sendCommand(final Command cmd, final byte[]... args) {
connect();
protocol.sendCommand(outputStream, cmd, args);
Protocol.sendCommand(outputStream, cmd, args);
pipelinedCommands++;
return this;
}
protected Connection sendCommand(final Command cmd) {
connect();
protocol.sendCommand(outputStream, cmd, new byte[0][]);
Protocol.sendCommand(outputStream, cmd, new byte[0][]);
pipelinedCommands++;
return this;
}
@@ -125,7 +124,7 @@ public class Connection {
socket.setTcpNoDelay(true); //Socket buffer Whetherclosed, to ensure timely delivery of data
socket.setSoLinger(true,0); //Control calls close () method, the underlying socket is closed immediately
//<-@wjw_add
socket.connect(new InetSocketAddress(host, port), timeout);
socket.setSoTimeout(timeout);
outputStream = new RedisOutputStream(socket.getOutputStream());
@@ -159,7 +158,7 @@ public class Connection {
protected String getStatusCodeReply() {
flush();
pipelinedCommands--;
final byte[] resp = (byte[]) protocol.read(inputStream);
final byte[] resp = (byte[]) Protocol.read(inputStream);
if (null == resp) {
return null;
} else {
@@ -179,13 +178,13 @@ public class Connection {
public byte[] getBinaryBulkReply() {
flush();
pipelinedCommands--;
return (byte[]) protocol.read(inputStream);
return (byte[]) Protocol.read(inputStream);
}
public Long getIntegerReply() {
flush();
pipelinedCommands--;
return (Long) protocol.read(inputStream);
return (Long) Protocol.read(inputStream);
}
public List<String> getMultiBulkReply() {
@@ -196,14 +195,14 @@ public class Connection {
public List<byte[]> getBinaryMultiBulkReply() {
flush();
pipelinedCommands--;
return (List<byte[]>) protocol.read(inputStream);
return (List<byte[]>) Protocol.read(inputStream);
}
@SuppressWarnings("unchecked")
public List<Object> getObjectMultiBulkReply() {
flush();
pipelinedCommands--;
return (List<Object>) protocol.read(inputStream);
return (List<Object>) Protocol.read(inputStream);
}
public List<Object> getAll() {
@@ -215,7 +214,7 @@ public class Connection {
flush();
while (pipelinedCommands > except) {
try{
all.add(protocol.read(inputStream));
all.add(Protocol.read(inputStream));
}catch(JedisDataException e){
all.add(e);
}
@@ -227,6 +226,6 @@ public class Connection {
public Object getOne() {
flush();
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 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) {
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) {
try {
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();
throw new JedisDataException(message);
}
private Object process(final RedisInputStream is) {
private static Object process(final RedisInputStream is) {
try {
byte b = is.readByte();
if (b == MINUS_BYTE) {
@@ -77,11 +83,11 @@ public final class Protocol {
return null;
}
private byte[] processStatusCodeReply(final RedisInputStream is) {
private static byte[] processStatusCodeReply(final RedisInputStream is) {
return SafeEncoder.encode(is.readLine());
}
private byte[] processBulkReply(final RedisInputStream is) {
private static byte[] processBulkReply(final RedisInputStream is) {
int len = Integer.parseInt(is.readLine());
if (len == -1) {
return null;
@@ -102,12 +108,12 @@ public final class Protocol {
return read;
}
private Long processInteger(final RedisInputStream is) {
private static Long processInteger(final RedisInputStream is) {
String num = is.readLine();
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());
if (num == -1) {
return null;
@@ -123,7 +129,7 @@ public final class Protocol {
return ret;
}
public Object read(final RedisInputStream is) {
public static Object read(final RedisInputStream is) {
return process(is);
}