From 52963e4eed0bbb3043e08cf90cae77dc7045d09c Mon Sep 17 00:00:00 2001 From: extio-de Date: Mon, 23 Jun 2025 12:48:59 +0200 Subject: [PATCH] AgentContext: Convenience value getter and setter; --- pom.xml | 2 +- .../de/extio/lmlib/agent/AgentContext.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 46299a8..ffd71b2 100755 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ lm-lib de.extio lm-lib - 1.5.2 + 1.5.3 21 diff --git a/src/main/java/de/extio/lmlib/agent/AgentContext.java b/src/main/java/de/extio/lmlib/agent/AgentContext.java index 3aa8392..c4982bb 100755 --- a/src/main/java/de/extio/lmlib/agent/AgentContext.java +++ b/src/main/java/de/extio/lmlib/agent/AgentContext.java @@ -86,6 +86,27 @@ public final class AgentContext { this.context.put(key, List.copyOf(value)); } + public T getValue(final String key, final Class 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 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> getContext() { return this.context; }