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,26 +19,26 @@ public class SetCommand implements CommandExecutable<byte[]> { ...@@ -18,26 +19,26 @@ public class SetCommand implements CommandExecutable<byte[]> {
@Override @Override
public byte[] execute() { public byte[] execute() {
if (!args.isEmpty()) { if (!validator.validateSetCommand(args)) {
Map<String, String> commandsMap = IntStream.iterate(2, i -> i < args.size(), i -> i + 2) return "wrong args".getBytes();
.boxed() }
.collect(Collectors.toMap(i -> args.get(i).toLowerCase(), i -> args.get(i + 1), (a, b) -> b));
String value = args.get(1); Map<String, String> commandsMap = IntStream.iterate(2, i -> i < args.size(), i -> i + 2)
String expiration = commandsMap.get(Command.PX.getValue().toLowerCase()); .boxed()
if (expiration != null) { .collect(Collectors.toMap(i -> args.get(i).toLowerCase(), i -> args.get(i + 1), (a, b) -> b));
try {
Long expirationTime = Long.parseLong(expiration); String value = args.get(1);
storage.save(args.getFirst().toLowerCase(), value, expirationTime); String expiration = commandsMap.get(Command.PX.getValue().toLowerCase());
} catch (NumberFormatException e) { if (expiration != null) {
System.out.println("NumberFormatException: " + e.getMessage()); try {
} Long expirationTime = Long.parseLong(expiration);
} else { storage.save(args.getFirst().toLowerCase(), value, expirationTime);
storage.save(args.getFirst().toLowerCase(), value); } catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage());
} }
return ("+OK\r\n".getBytes());
} else { } else {
return "wrong args".getBytes(); storage.save(args.getFirst().toLowerCase(), value);
} }
return ("+OK\r\n".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