RequestParameterAccessor to allow simple access to Parameters in PrimitiveRequest #208

Merged
lars.winzer merged 7 commits from feat/21-requestparameteraccessor into main 2026-03-30 13:22:38 +02:00
4 changed files with 83 additions and 1 deletions
Showing only changes of commit e6b62726bd - Show all commits
@@ -1,13 +1,25 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/** Exception thrown when a required parameter key is not found. */
public class MissingParameterException extends RuntimeException {
private final String parameterKey;
/**
* Creates a new exception for a missing required parameter.
*
* @param message human-readable description of the missing parameter
* @param parameterKey key of the parameter that could not be found
*/
public MissingParameterException(String message, String parameterKey) {
super(message);
this.parameterKey = parameterKey;
}
/**
* Returns the missing parameter key.
*
* @return key of the parameter that could not be found
*/
public String getParameterKey() {
return parameterKey;
}
@@ -1,6 +1,13 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/** Exception thrown when a parameter value cannot be converted to the requested type. */
public class ParameterParseException extends RuntimeException {
/**
* Creates a new parse exception with a root cause.
*
* @param message human-readable description of the parsing failure
* @param cause original exception thrown during parsing
*/
public ParameterParseException(String message, Throwable cause) {
super(message, cause);
}
@@ -4,9 +4,20 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Provides typed access to a request's parameters by indexing them by key.
*
* <p>Supports required and optional lookups, with optional conversion from {@link String} values to
* domain-specific types via parser functions.
*/
public class RequestParameterAccessor {
private final Map<String, String> index;
/**
* Creates an accessor
*
* @param parameters to use
*/
public RequestParameterAccessor(List<Parameter> parameters) {
this.index = parameters.stream()
.collect(Collectors.toUnmodifiableMap(
@@ -15,6 +26,13 @@ public class RequestParameterAccessor {
));
}
/**
* Returns the raw value for a required parameter key.
*
* @param key parameter key to look up
* @return raw parameter value
* @throws MissingParameterException if no parameter with the given key exists
*/
public String require(String key) throws MissingParameterException {
String value = index.get(key);
if (value == null) {
@@ -23,6 +41,16 @@ public class RequestParameterAccessor {
return value;
}
/**
* Returns a parsed value for a required parameter key.
*
* @param key parameter key to look up
* @param parser parser used to convert the raw value
* @param <T> target type returned by the parser
* @return parsed parameter value
* @throws MissingParameterException if no parameter with the given key exists
* @throws ParameterParseException if parsing the raw value fails
*/
public <T> T require(String key, ThrowingParser<T> parser) throws MissingParameterException, ParameterParseException {
String value = require(key);
try {
@@ -32,6 +60,13 @@ public class RequestParameterAccessor {
}
}
/**
* Returns the raw value for a parameter key or the provided default value if missing.
*
* @param key parameter key to look up
* @param defaultValue value returned when the key does not exist
* @return found parameter value or {@code defaultValue} if absent
*/
public String optional(String key, String defaultValue) {
String value = index.get(key);
if (value == null) {
@@ -40,6 +75,16 @@ public class RequestParameterAccessor {
return value;
}
/**
* Returns a parsed value for a parameter key or the provided default value if missing.
*
* @param key parameter key to look up
* @param defaultValue value returned when the key does not exist
* @param parser parser used to convert the raw value
* @param <T> target type returned by the parser
* @return parsed parameter value or {@code defaultValue} if absent
* @throws ParameterParseException if parsing the raw value fails
*/
public <T> T optional(String key, T defaultValue, ThrowingParser<T> parser) throws ParameterParseException {
String value = index.get(key);
if (value == null) {
@@ -53,6 +98,12 @@ public class RequestParameterAccessor {
}
}
/**
* Checks whether a parameter with the given key exists.
*
* @param key parameter key to check
* @return {@code true} if the key exists, otherwise {@code false}
*/
public boolean containsKey(String key) {
return index.containsKey(key);
}
@@ -1,6 +1,18 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/**
* Functional parser interface used to convert a raw string parameter into a target type.
*
* @param <T> target type produced by the parser
*/
@FunctionalInterface
interface ThrowingParser<T> {
/**
* Parses the provided raw parameter value.
*
* @param value raw parameter value
* @return parsed value
* @throws Exception if the value cannot be parsed
*/
T parse(String value) throws Exception;
}