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

Adding Validator class for arguments

parent 62cf7a72
package command;
import model.Command;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CommandValidator {
private CommandValidator() {
}
private static final class InstanceHolder {
private static final CommandValidator instance = new CommandValidator();
}
public static CommandValidator getInstance() {
return InstanceHolder.instance;
}
public boolean validateSetCommand(List<String> args) {
if (args == null || args.isEmpty()) {
return false;
}
if (args.size() < 2) {
return false;
}
if (args.size() > 2) {
Map<String, String> commandsMap = IntStream.iterate(2, i -> i < args.size(), i -> i + 2)
.boxed()
.collect(Collectors.toMap(i -> args.get(i).toLowerCase(), i -> args.get(i + 1), (a, b) -> b));
String expiration = commandsMap.get(Command.PX.getValue().toLowerCase());
if (expiration != null) {
try {
Long.parseLong(expiration);
} catch (NumberFormatException e) {
return false;
}
}
}
return true;
}
public boolean validateGetCommand(List<String> args) {
return args != null && args.size() == 1;
}
}
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
public class GetCommand implements CommandExecutable<byte[]> { public class GetCommand implements CommandExecutable<byte[]> {
private final Storage storage = Storage.getInstance(); private final Storage storage = Storage.getInstance();
private final List<String> args; private final List<String> args;
private final CommandValidator validator = CommandValidator.getInstance();
public GetCommand(List<String> args) { public GetCommand(List<String> args) {
this.args = args; this.args = args;
...@@ -16,7 +17,9 @@ public class GetCommand implements CommandExecutable<byte[]> { ...@@ -16,7 +17,9 @@ public class GetCommand implements CommandExecutable<byte[]> {
@Override @Override
public byte[] execute() { public byte[] execute() {
if (!validator.validateGetCommand(args)) {
return "wrong args".getBytes();
}
String response = storage.get(args.getFirst().toLowerCase()); String response = storage.get(args.getFirst().toLowerCase());
if (response == null || response.isEmpty() || response.isBlank()) { if (response == null || response.isEmpty() || response.isBlank()) {
return ("$-1\r\n".getBytes()); return ("$-1\r\n".getBytes());
......
...@@ -11,6 +11,7 @@ import java.util.stream.IntStream; ...@@ -11,6 +11,7 @@ import java.util.stream.IntStream;
public class SetCommand implements CommandExecutable<byte[]> { public class SetCommand implements CommandExecutable<byte[]> {
private final Storage storage = Storage.getInstance(); private final Storage storage = Storage.getInstance();
private final List<String> args; private final List<String> args;
private final CommandValidator validator = CommandValidator.getInstance();
public SetCommand(List<String> args) { public SetCommand(List<String> args) {
this.args = args; this.args = args;
...@@ -18,7 +19,10 @@ public class SetCommand implements CommandExecutable<byte[]> { ...@@ -18,7 +19,10 @@ public class SetCommand implements CommandExecutable<byte[]> {
@Override @Override
public byte[] execute() { public byte[] execute() {
if (!args.isEmpty()) { if (!validator.validateSetCommand(args)) {
return "wrong args".getBytes();
}
Map<String, String> commandsMap = IntStream.iterate(2, i -> i < args.size(), i -> i + 2) Map<String, String> commandsMap = IntStream.iterate(2, i -> i < args.size(), i -> i + 2)
.boxed() .boxed()
.collect(Collectors.toMap(i -> args.get(i).toLowerCase(), i -> args.get(i + 1), (a, b) -> b)); .collect(Collectors.toMap(i -> args.get(i).toLowerCase(), i -> args.get(i + 1), (a, b) -> b));
...@@ -36,8 +40,5 @@ public class SetCommand implements CommandExecutable<byte[]> { ...@@ -36,8 +40,5 @@ public class SetCommand implements CommandExecutable<byte[]> {
storage.save(args.getFirst().toLowerCase(), value); storage.save(args.getFirst().toLowerCase(), value);
} }
return ("+OK\r\n".getBytes()); return ("+OK\r\n".getBytes());
} else {
return "wrong args".getBytes();
}
} }
} }
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