AgentContext: Convenience value getter and setter;
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<artifactId>lm-lib</artifactId>
|
<artifactId>lm-lib</artifactId>
|
||||||
<groupId>de.extio</groupId>
|
<groupId>de.extio</groupId>
|
||||||
<name>lm-lib</name>
|
<name>lm-lib</name>
|
||||||
<version>1.5.2</version>
|
<version>1.5.3</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>21</java.version>
|
<java.version>21</java.version>
|
||||||
|
|||||||
@@ -86,6 +86,27 @@ public final class AgentContext {
|
|||||||
this.context.put(key, List.copyOf(value));
|
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() {
|
public Map<String, List<? extends Object>> getContext() {
|
||||||
return this.context;
|
return this.context;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user