Docs: Write JavaDoc for added or modified components

This commit is contained in:
Lars Simon Winzer
2026-04-08 18:06:55 +02:00
parent ffca8e5e29
commit 15062df711
4 changed files with 85 additions and 0 deletions
@@ -6,16 +6,40 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Abstract base class for handling requests of a specific type.
*
* @param <T> the type of request this handler processes, must extend {@link Request}
*/
public abstract class CommandHandler<T extends Request> {
private final List<HandlerCheck> checks = new ArrayList<>();
/**
* Adds a handler check to be performed before request execution.
*
* <p>The {@code execute} Method is only invoked, if all specified checks passed.
*
* @param check the {@link HandlerCheck} to add
*/
protected final void addCheck(HandlerCheck check) {
checks.add(check);
}
/**
* Returns an unmodifiable list of all registered handler checks.
*
* @return an unmodifiable list of {@link HandlerCheck} objects
*/
public final List<HandlerCheck> getChecks() {
return Collections.unmodifiableList(checks);
}
/**
* Executes the request.
*
* <p>Subclasses should override this method to implement their specific request handling logic.
*
* @param request the request to execute, of type T
*/
public void execute(T request) {}
}
@@ -6,13 +6,33 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
import java.util.Optional;
/**
* Invokes execution of the {@link CommandHandler} if all defined pre-execution checks pass.
*
* <p>If any check fails, the response returned by the check is dispatched immediately and execution
* is aborted. Otherwise, the handler will invoke execution normally.
*/
public class CommandHandlerExecutor {
private final ResponseDispatcher responseDispatcher;
/**
* Creates a new CommandHandlerExecutor to execute pre-execution checks and invoke the {@link
* CommandHandler} if all checks pass.
*
* @param responseDispatcher to dispatch the response with if an check fails
*/
public CommandHandlerExecutor(ResponseDispatcher responseDispatcher) {
this.responseDispatcher = responseDispatcher;
}
/**
* Executes a command handler if all defined pre-execution checks pass.
*
* @param handler the command handler containing checks and execution logic
* @param request the incoming request to validate and process
* @param <Request> the type of request being processed
* @throws NullPointerException if handler or request is null
*/
public void execute(CommandHandler<Request> handler, Request request) {
for (HandlerCheck check : handler.getChecks()) {
Optional<Response> result = check.check(request);
@@ -4,18 +4,46 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
import java.util.HashMap;
import java.util.Map;
/** Routes incoming requests to their corresponding {@link CommandHandler} implementations. */
public class CommandRouter {
private final Map<Class<? extends Request>, CommandHandler<?>> handlers = new HashMap<>();
private final CommandHandlerExecutor handlerExecutor;
/**
* Creates a new CommandRouter with the specified handler executor.
*
* @param commandHandlerExecutor to delegate execution of checks and invocation of {@link
* CommandHandler} to
*/
public CommandRouter(CommandHandlerExecutor commandHandlerExecutor) {
this.handlerExecutor = commandHandlerExecutor;
}
/**
* Registers a {@link CommandHandler} for a specific request type.
*
* <p>This method establishes a type-safe mapping between a request class and its handler.
*
* <p>Only one handler can be registered per request type; subsequent registrations will
* overwrite previous ones.
*
* @param <T> the type of request handled by the provided handler
* @param request the request class to associate with the given command handler
* @param handler the command handler for the given request type
*/
public <T extends Request> void register(Class<T> request, CommandHandler<T> handler) {
handlers.put(request, handler);
}
/**
* Executes the appropriate handler for the given request.
*
* <p>Looks up the handler registered for the request's class and executes it through the
* configured {@link CommandHandlerExecutor}.
*
* @param request the request to be executed
* @throws UnknownRequestException if no handler is registered for the request type
*/
// Safe, because during registration, it's ensured that the provided CommandHandler only
// receives requests it can handle.
@SuppressWarnings("unchecked")
@@ -1,10 +1,23 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.checks;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorResponse;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response;
import java.util.Optional;
/**
* Functional interface used by pre-execution checks. Executed before the execute method of the
* {@link CommandHandler} is called
*/
@FunctionalInterface
public interface HandlerCheck {
/**
* Checks that the requirements outlined by this HandlerCheck are fulfilled.
*
* @param request to execute the check with
* @return Optional containing no value if the check was successful. And a {@link ErrorResponse}
* if the check failed, the response will be dispatched to the client.
*/
Optional<Response> check(Request request);
}