From f6fb43ae6c7bde15f15d47507f6a434de2e83071 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Wed, 24 Jan 2024 10:48:21 +0000 Subject: [PATCH] Use map factory in getValues --- .../config/inject/ConfigProducerUtil.java | 2 +- .../java/io/smallrye/config/Converters.java | 22 ++++++++++++++----- .../io/smallrye/config/SmallRyeConfig.java | 9 ++++---- .../smallrye/config/SmallRyeConfigTest.java | 21 ++++++++++++++++++ 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java b/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java index 24e3595ad..9db8f3606 100644 --- a/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java +++ b/cdi/src/main/java/io/smallrye/config/inject/ConfigProducerUtil.java @@ -172,7 +172,7 @@ private static Converter resolveConverter(final Type type, final SmallRye return (Converter) newCollectionConverter(resolveConverter(typeArgs[0], config), HashSet::new); } else if (rawType == Map.class) { return (Converter) newMapConverter(resolveConverter(typeArgs[0], config), - resolveConverter(typeArgs[1], config)); + resolveConverter(typeArgs[1], config), HashMap::new); } else if (rawType == Optional.class) { return (Converter) newOptionalConverter(resolveConverter(typeArgs[0], config)); } else if (rawType == Supplier.class) { diff --git a/implementation/src/main/java/io/smallrye/config/Converters.java b/implementation/src/main/java/io/smallrye/config/Converters.java index d2e89136a..70e6bc6db 100644 --- a/implementation/src/main/java/io/smallrye/config/Converters.java +++ b/implementation/src/main/java/io/smallrye/config/Converters.java @@ -322,9 +322,15 @@ public static Converter newArrayConverter(Converter itemC * @param the type of the values * @return the new converter (not {@code null}) */ + @Deprecated public static Converter> newMapConverter(Converter keyConverter, Converter valueConverter) { - return new MapConverter<>(keyConverter, valueConverter); + return newMapConverter(keyConverter, valueConverter, HashMap::new); + } + + public static Converter> newMapConverter(Converter keyConverter, + Converter valueConverter, IntFunction> mapFactory) { + return new MapConverter<>(keyConverter, valueConverter, mapFactory); } /** @@ -1037,16 +1043,22 @@ static class MapConverter extends AbstractConverter> { * The converter to use the for values. */ private final Converter valueConverter; + private final IntFunction> mapFactory; /** * Construct a {@code MapConverter} with the given converters. * * @param keyConverter the converter to use the for keys * @param valueConverter the converter to use the for values + * @param mapFactory */ - MapConverter(Converter keyConverter, Converter valueConverter) { + MapConverter( + final Converter keyConverter, + final Converter valueConverter, + final IntFunction> mapFactory) { this.keyConverter = keyConverter; this.valueConverter = valueConverter; + this.mapFactory = mapFactory; } @Override @@ -1054,8 +1066,8 @@ public Map convert(String value) throws IllegalArgumentException, NullPoin if (value == null) { return null; } - final Map map = new HashMap<>(); - final StringBuilder currentLine = new StringBuilder(value.length()); + Map map = mapFactory.apply(0); + StringBuilder currentLine = new StringBuilder(value.length()); int fromIndex = 0; for (int idx; (idx = value.indexOf(';', fromIndex)) >= 0; fromIndex = idx + 1) { if (value.charAt(idx - 1) == '\\') { @@ -1082,7 +1094,7 @@ public Map convert(String value) throws IllegalArgumentException, NullPoin * @throws NoSuchElementException if the line could not be converted into an entry or doesn't have the expected format. */ private void processLine(Map map, String value, String rawLine) { - final String line = rawLine.replace("\\;", ";"); + String line = rawLine.replace("\\;", ";"); for (int idx, fromIndex = 0; (idx = line.indexOf('=', fromIndex)) >= 0; fromIndex = idx + 1) { if (line.charAt(idx - 1) == '\\') { // The key separator has been escaped diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java index c1b63970d..4fcfaeeab 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java @@ -215,7 +215,7 @@ public Map getValues( Converter valueConverter, IntFunction> mapFactory) { try { - return getValue(name, newMapConverter(keyConverter, valueConverter)); + return getValue(name, newMapConverter(keyConverter, valueConverter, mapFactory)); } catch (NoSuchElementException e) { Map mapKeys = getMapKeys(name); if (mapKeys.isEmpty()) { @@ -246,7 +246,8 @@ public > Map getValues( IntFunction> mapFactory, IntFunction collectionFactory) { try { - return getValue(name, newMapConverter(keyConverter, newCollectionConverter(valueConverter, collectionFactory))); + return getValue(name, + newMapConverter(keyConverter, newCollectionConverter(valueConverter, collectionFactory), mapFactory)); } catch (NoSuchElementException e) { Map mapCollectionKeys = getMapKeys(name); if (mapCollectionKeys.isEmpty()) { @@ -463,7 +464,7 @@ public Optional> getOptionalValues(String name, Converter ke public Optional> getOptionalValues(String name, Converter keyConverter, Converter valueConverter, IntFunction> mapFactory) { - Optional> optionalValue = getOptionalValue(name, newMapConverter(keyConverter, valueConverter)); + Optional> optionalValue = getOptionalValue(name, newMapConverter(keyConverter, valueConverter, mapFactory)); if (optionalValue.isPresent()) { return optionalValue; } @@ -491,7 +492,7 @@ public > Optional> getOptionalValues( IntFunction> mapFactory, IntFunction collectionFactory) { Optional> optionalValue = getOptionalValue(name, - newMapConverter(keyConverter, newCollectionConverter(valueConverter, collectionFactory))); + newMapConverter(keyConverter, newCollectionConverter(valueConverter, collectionFactory), mapFactory)); if (optionalValue.isPresent()) { return optionalValue; } diff --git a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java index 0801559c0..18c7fe7b3 100644 --- a/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java +++ b/implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java @@ -21,10 +21,12 @@ import java.util.NoSuchElementException; import java.util.Optional; import java.util.Set; +import java.util.TreeMap; import java.util.function.IntFunction; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.Converter; import org.junit.jupiter.api.Test; import io.smallrye.config.common.AbstractConfigSource; @@ -377,6 +379,10 @@ void getValuesMap() { assertEquals("value", map.get("key.nested")); assertEquals("value", map.get("key.quoted")); + Converter stringConverter = config.requireConverter(String.class); + Map treeMap = config.getValues("my.prop", stringConverter, stringConverter, t -> new TreeMap<>()); + assertTrue(treeMap instanceof TreeMap); + Optional> optionalMap = config.getOptionalValues("my.prop", String.class, String.class); assertTrue(optionalMap.isPresent()); assertEquals(3, optionalMap.get().size()); @@ -384,6 +390,11 @@ void getValuesMap() { assertEquals("value", optionalMap.get().get("key.nested")); assertEquals("value", optionalMap.get().get("key.quoted")); + Optional> optionalTreeMap = config.getOptionalValues("my.prop", stringConverter, stringConverter, + t -> new TreeMap<>()); + assertTrue(optionalTreeMap.isPresent()); + assertTrue(optionalTreeMap.get() instanceof TreeMap); + assertTrue(config.getOptionalValues("my.optional", String.class, String.class).isEmpty()); } @@ -428,6 +439,11 @@ void getValuesMapList() { assertEquals("value", map.get("key.quoted").get(0)); assertEquals("value", map.get("key.quoted").get(1)); + Converter stringConverter = config.requireConverter(String.class); + Map> treeMap = config.getValues("my.prop", stringConverter, stringConverter, t -> new TreeMap<>(), + ArrayList::new); + assertTrue(treeMap instanceof TreeMap); + Optional>> optionalMap = config.getOptionalValues("my.prop", String.class, String.class, ArrayList::new); assertTrue(optionalMap.isPresent()); @@ -439,6 +455,11 @@ void getValuesMapList() { assertEquals("value", optionalMap.get().get("key.quoted").get(0)); assertEquals("value", optionalMap.get().get("key.quoted").get(1)); + Optional>> optionalTreeMap = config.getOptionalValues("my.prop", stringConverter, + stringConverter, t -> new TreeMap<>(), ArrayList::new); + assertTrue(optionalTreeMap.isPresent()); + assertTrue(optionalTreeMap.get() instanceof TreeMap); + assertTrue(config.getOptionalValues("my.optional", String.class, String.class, ArrayList::new).isEmpty()); }