Docs: Add JavaDoc to all new interfaces, classes and methods
Co-authored-by: Copilot
This commit is contained in:
+12
@@ -1,13 +1,25 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
|
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 {
|
public class MissingParameterException extends RuntimeException {
|
||||||
private final String parameterKey;
|
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) {
|
public MissingParameterException(String message, String parameterKey) {
|
||||||
super(message);
|
super(message);
|
||||||
this.parameterKey = parameterKey;
|
this.parameterKey = parameterKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the missing parameter key.
|
||||||
|
*
|
||||||
|
* @return key of the parameter that could not be found
|
||||||
|
*/
|
||||||
public String getParameterKey() {
|
public String getParameterKey() {
|
||||||
return parameterKey;
|
return parameterKey;
|
||||||
}
|
}
|
||||||
|
|||||||
+7
@@ -1,6 +1,13 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
|
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 {
|
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) {
|
public ParameterParseException(String message, Throwable cause) {
|
||||||
super(message, cause);
|
super(message, cause);
|
||||||
}
|
}
|
||||||
|
|||||||
+51
@@ -4,9 +4,20 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
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 {
|
public class RequestParameterAccessor {
|
||||||
private final Map<String, String> index;
|
private final Map<String, String> index;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an accessor
|
||||||
|
*
|
||||||
|
* @param parameters to use
|
||||||
|
*/
|
||||||
public RequestParameterAccessor(List<Parameter> parameters) {
|
public RequestParameterAccessor(List<Parameter> parameters) {
|
||||||
this.index = parameters.stream()
|
this.index = parameters.stream()
|
||||||
.collect(Collectors.toUnmodifiableMap(
|
.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 {
|
public String require(String key) throws MissingParameterException {
|
||||||
String value = index.get(key);
|
String value = index.get(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
@@ -23,6 +41,16 @@ public class RequestParameterAccessor {
|
|||||||
return value;
|
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 {
|
public <T> T require(String key, ThrowingParser<T> parser) throws MissingParameterException, ParameterParseException {
|
||||||
String value = require(key);
|
String value = require(key);
|
||||||
try {
|
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) {
|
public String optional(String key, String defaultValue) {
|
||||||
String value = index.get(key);
|
String value = index.get(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
@@ -40,6 +75,16 @@ public class RequestParameterAccessor {
|
|||||||
return value;
|
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 {
|
public <T> T optional(String key, T defaultValue, ThrowingParser<T> parser) throws ParameterParseException {
|
||||||
String value = index.get(key);
|
String value = index.get(key);
|
||||||
if (value == null) {
|
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) {
|
public boolean containsKey(String key) {
|
||||||
return index.containsKey(key);
|
return index.containsKey(key);
|
||||||
}
|
}
|
||||||
|
|||||||
+12
@@ -1,6 +1,18 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
|
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
|
@FunctionalInterface
|
||||||
interface ThrowingParser<T> {
|
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;
|
T parse(String value) throws Exception;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user