Add CommandParserDispatcher to dispatch PrimitiveRequest to matching CommandParser #188

Merged
lars.winzer merged 4 commits from feat/command-parser into main 2026-03-15 17:21:08 +01:00
4 changed files with 66 additions and 0 deletions
@@ -0,0 +1,15 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/**
* Parser to convert the PrimitiveRequest to a Request and performing checks for required fields and
* data types
*/
public interface CommandParser {
/**
* Parses the provided PrimitiveRequest into a command-specific request
*
* @param primitiveRequest
* @return
*/
Request parse(PrimitiveRequest primitiveRequest);
}
@@ -0,0 +1,36 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
import java.util.HashMap;
import java.util.Map;
/** Dispatcher all CommandParser are registered in */
public class CommandParserDispatcher {
private final Map<String, CommandParser> parsers = new HashMap<>();
/**
* Register a new CommandParser
*
* @param command
* @param parser the parser class
*/
public void register(String command, CommandParser parser) {
parsers.put(command, parser);
}
/**
* Parses the PrimitiveRequest into a Request using the appropriate CommandParser
*
* @param primitiveRequest the PrimitiveRequest to parse
* @return the parsed Request object
*/
public Request parse(PrimitiveRequest primitiveRequest) {
String command = primitiveRequest.command();
CommandParser parser = parsers.get(command);
if (parser == null) {
throw new UnknownCommandException(command);
}
return parser.parse(primitiveRequest);
}
}
@@ -0,0 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/** Request, produced by the CommandParser */
public interface Request {}
@@ -0,0 +1,11 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/**
* Exception thrown when the CommandParserDispatcher has no registered handler for the provided
* request
*/
public class UnknownCommandException extends RuntimeException {
public UnknownCommandException(String message) {
super(message);
}
}