AgentContext: Convenience value getter and setter;

This commit is contained in:
extio-de
2025-06-23 12:48:59 +02:00
parent 92a7e00698
commit 52963e4eed
2 changed files with 22 additions and 1 deletions

View File

@@ -4,7 +4,7 @@
<artifactId>lm-lib</artifactId>
<groupId>de.extio</groupId>
<name>lm-lib</name>
<version>1.5.2</version>
<version>1.5.3</version>
<properties>
<java.version>21</java.version>

View File

@@ -86,6 +86,27 @@ public final class AgentContext {
this.context.put(key, List.copyOf(value));
}
public <T> T getValue(final String key, final Class<T> type) {
final var values = this.context.get(key);
if (values == null || values.isEmpty()) {
return null;
}
final var firstValue = values.getFirst();
if (type.isInstance(firstValue)) {
return type.cast(firstValue);
}
throw new IllegalArgumentException("Value for key '" + key + "' is not of type " + type.getName());
}
public <T> void setValue(final String key, final T value) {
if (value == null) {
this.context.remove(key);
}
else {
this.context.put(key, List.of(value));
}
}
public Map<String, List<? extends Object>> getContext() {
return this.context;
}