Commit c9425153 authored by amir.yosef's avatar amir.yosef

make the Invoker independent from command's args

parent 10acd291
......@@ -2,8 +2,7 @@ package command;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public interface CommandHandler {
void execute(List<String> args, OutputStream os) throws IOException;
void execute( OutputStream os) throws IOException;
}
......@@ -2,10 +2,9 @@ package command;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class CommandInvoker {
public static void invoke(CommandHandler command, List<String> CommandAndArgs, OutputStream os) throws IOException {
command.execute(CommandAndArgs, os);
public static void invoke(CommandHandler command, OutputStream os) throws IOException {
command.execute(os);
}
}
......@@ -7,8 +7,13 @@ import java.io.OutputStream;
import java.util.List;
public class EchoCommand implements CommandHandler {
private final List<String> args;
public EchoCommand(List<String> args) {
this.args = args;
}
@Override
public void execute(List<String> args, OutputStream os) {
public void execute(OutputStream os) {
try {
os.write(Response.getResponse(args.getFirst()));
os.flush();
......
......@@ -10,10 +10,14 @@ import java.util.List;
public class GetCommand implements CommandHandler {
private final Storage storage = Storage.getInstance();
private final List<String> args;
public GetCommand(List<String> args) {
this.args = args;
}
@Override
public void execute(List<String> args, OutputStream os) {
public void execute(OutputStream os) {
try {
String response = storage.get(args.getFirst().toLowerCase());
......
......@@ -5,13 +5,11 @@ import model.Command;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class PingCommand implements CommandHandler {
@Override
public void execute(List<String> args, OutputStream os) {
System.out.printf("Processing PING args %s%n", args);
public void execute(OutputStream os) {
try {
os.write(("+" + Command.PONG.getValue() + "\r\n").getBytes());
os.flush();
......
......@@ -10,10 +10,15 @@ import java.util.List;
import java.util.Map;
public class SetCommand implements CommandHandler {
private final Storage redisStorage = Storage.getInstance();
private final Storage storage = Storage.getInstance();
private final List<String>args;
public SetCommand(List<String> args) {
this.args = args;
}
@Override
public void execute(List<String> args, OutputStream os) {
public void execute(OutputStream os) {
Map<String, String> commandsMap = new HashMap<>();
......@@ -27,12 +32,12 @@ public class SetCommand implements CommandHandler {
if (expiration != null) {
try {
Long expirationTime = Long.parseLong(expiration);
redisStorage.save(args.getFirst().toLowerCase(), value, expirationTime);
storage.save(args.getFirst().toLowerCase(), value, expirationTime);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage());
}
} else {
redisStorage.save(args.getFirst().toLowerCase(), value);
storage.save(args.getFirst().toLowerCase(), value);
}
try {
......
......@@ -2,11 +2,10 @@ package command;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class UnknownCommand implements CommandHandler {
@Override
public void execute(List<String> args, OutputStream os) {
public void execute(OutputStream os) {
try {
os.write(("-" + "Unknown Command" + "\r\n").getBytes());
os.flush();
......
......@@ -3,6 +3,8 @@ package factory;
import command.*;
import model.Command;
import java.util.List;
public class CommandFactory implements Factory {
private static final class FactoryHolder {
......@@ -18,12 +20,12 @@ public class CommandFactory implements Factory {
}
@Override
public CommandHandler getCommand(Command command) {
public CommandHandler getCommand(Command command, List<String> args) {
return switch (command) {
case PING -> new PingCommand();
case ECHO -> new EchoCommand();
case SET -> new SetCommand();
case GET -> new GetCommand();
case ECHO -> new EchoCommand(args);
case SET -> new SetCommand(args);
case GET -> new GetCommand(args);
// case REPLCONF -> new ReplConfCommand(replicaReceiver);
// case PSYNC -> new FullResyncCommandProcessor(replicaSender);
// case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver);
......
......@@ -4,6 +4,8 @@ package factory;
import command.CommandHandler;
import model.Command;
import java.util.List;
public interface Factory {
CommandHandler getCommand(Command command);
CommandHandler getCommand(Command command, List<String> args);
}
......@@ -24,13 +24,11 @@ public class ClientCommandHandler {
public void execute() {
Command command = CommandUtil.getCommand(commands.getFirst());
commands.removeFirst();
CommandHandler commandProcessor = factory.getCommand(command);
CommandHandler commandProcessor = factory.getCommand(command, commands);
try {
CommandInvoker.invoke(commandProcessor, commands, os);
CommandInvoker.invoke(commandProcessor, os);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment