Support chaining evaluation context additions.

Refs #29
This commit is contained in:
Justin Abrahms 2022-08-07 21:08:45 -07:00
parent efc1c048b6
commit 4cbb87fbab
No known key found for this signature in database
GPG Key ID: 599E2E12011DC474
2 changed files with 19 additions and 5 deletions

View File

@ -21,8 +21,9 @@ public class EvaluationContext {
// TODO Not sure if I should have sneakythrows or checked exceptions here..
@SneakyThrows
public <T> void addStructureAttribute(String key, T value) {
public <T> EvaluationContext addStructureAttribute(String key, T value) {
attributes.put(key, new Pair<>(FlagValueType.OBJECT, value));
return this;
}
@SneakyThrows
@ -34,8 +35,9 @@ public class EvaluationContext {
return getAttributesByType(FlagValueType.OBJECT);
}
public void addStringAttribute(String key, String value) {
public EvaluationContext addStringAttribute(String key, String value) {
attributes.put(key, new Pair<>(FlagValueType.STRING, value));
return this;
}
public String getStringAttribute(String key) {
@ -68,8 +70,9 @@ public class EvaluationContext {
return getAttributesByType(FlagValueType.STRING);
}
public void addIntegerAttribute(String key, Integer value) {
public EvaluationContext addIntegerAttribute(String key, Integer value) {
attributes.put(key, new Pair<>(FlagValueType.INTEGER, value));
return this;
}
public Integer getIntegerAttribute(String key) {
@ -84,16 +87,18 @@ public class EvaluationContext {
return getAttributeByType(key, FlagValueType.BOOLEAN);
}
public void addBooleanAttribute(String key, Boolean b) {
public EvaluationContext addBooleanAttribute(String key, Boolean b) {
attributes.put(key, new Pair<>(FlagValueType.BOOLEAN, b));
return this;
}
public Map<String, Boolean> getBooleanAttributes() {
return getAttributesByType(FlagValueType.BOOLEAN);
}
public void addDatetimeAttribute(String key, ZonedDateTime value) {
public EvaluationContext addDatetimeAttribute(String key, ZonedDateTime value) {
attributes.put(key, new Pair<>(FlagValueType.STRING, value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)));
return this;
}
public ZonedDateTime getDatetimeAttribute(String key) {

View File

@ -109,4 +109,13 @@ public class EvalContextTest {
assertEquals(null, ec.getStringAttribute("key"));
assertEquals(3, ec.getIntegerAttribute("key"));
}
@Test void can_chain_attribute_addition() {
EvaluationContext ec = new EvaluationContext();
EvaluationContext out = ec.addStructureAttribute("str", "test")
.addIntegerAttribute("int", 4)
.addBooleanAttribute("bool", false)
.addStructureAttribute("str", new Node<Integer>());
assertEquals(EvaluationContext.class, out.getClass());
}
}