From 3be97c61805361b46bfbf0695ef483ae3a3f4685 Mon Sep 17 00:00:00 2001 From: marecabo <23156476+marecabo@users.noreply.github.com> Date: Tue, 10 Oct 2023 09:42:35 +0200 Subject: [PATCH] Support EnumMaps --- .../org/matsim/core/config/ReflectiveConfigGroup.java | 5 +++-- .../org/matsim/core/config/ReflectiveConfigGroupTest.java | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/matsim/src/main/java/org/matsim/core/config/ReflectiveConfigGroup.java b/matsim/src/main/java/org/matsim/core/config/ReflectiveConfigGroup.java index 391c1577394..7d6d86f0a4d 100644 --- a/matsim/src/main/java/org/matsim/core/config/ReflectiveConfigGroup.java +++ b/matsim/src/main/java/org/matsim/core/config/ReflectiveConfigGroup.java @@ -433,8 +433,9 @@ private Object fromString(String value, Class type, @Nullable Field paramFiel } else if (paramField != null && paramField.getGenericType() instanceof ParameterizedType pType) { Class keyClass = TypeFactory.rawClass(pType.getActualTypeArguments()[0]); Class valueClass = TypeFactory.rawClass(pType.getActualTypeArguments()[1]); - JavaType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType( - LinkedHashMap.class, keyClass, valueClass); + Class mapClass = keyClass.isEnum() ? EnumMap.class : LinkedHashMap.class; + JavaType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(mapClass, keyClass, + valueClass); try { return OBJECT_MAPPER.readValue(value, mapType); } catch (JsonProcessingException e) { diff --git a/matsim/src/test/java/org/matsim/core/config/ReflectiveConfigGroupTest.java b/matsim/src/test/java/org/matsim/core/config/ReflectiveConfigGroupTest.java index e410b659522..b19c1552100 100644 --- a/matsim/src/test/java/org/matsim/core/config/ReflectiveConfigGroupTest.java +++ b/matsim/src/test/java/org/matsim/core/config/ReflectiveConfigGroupTest.java @@ -27,6 +27,7 @@ import java.time.LocalTime; import java.util.Collection; import java.util.Collections; +import java.util.EnumMap; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -74,6 +75,8 @@ public void testDumpAndRead() { dumpedModule.integerMapField = Map.of("a\"b", 1, "b", 2); dumpedModule.localDateMapField = Map.of('d', LocalDate.of(2023, 10, 9)); dumpedModule.booleanMapField = Map.of(0.1, true); + dumpedModule.enumMapField = new EnumMap<>(MyEnum.class); + dumpedModule.enumMapField.put(MyEnum.VALUE1, "abc"); dumpedModule.setField = ImmutableSet.of("a", "b", "c"); dumpedModule.listField = List.of("1", "2", "3"); assertEqualAfterDumpAndRead(dumpedModule); @@ -198,6 +201,7 @@ public void testComments() { expectedComments.put("integerMapField", "map of Integer"); expectedComments.put("localDateMapField", "map of LocalDate"); expectedComments.put("booleanMapField", "map of Boolean"); + expectedComments.put("enumMapField", "map of Enum"); assertThat(new MyModule().getComments()).isEqualTo(expectedComments); } @@ -515,6 +519,10 @@ private static class MyModule extends ReflectiveConfigGroup { @Parameter private Map booleanMapField; + @Comment("map of Enum") + @Parameter + private Map enumMapField; + // Object fields: // Id: string representation is toString private Id idField;