From 40ec717146e1748048502aa8f559e43cc4f42dcc Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Fri, 31 Jul 2020 16:18:45 +0100 Subject: [PATCH] Remove toMap from KeyMap. --- .../main/java/io/smallrye/config/KeyMap.java | 26 ------------------- .../config/KeyMapBackedConfigSource.java | 5 ++-- .../config/SmallRyeConfigBuilder.java | 15 ++--------- .../config/KeyMapBackedConfigSourceTest.java | 15 ++++------- .../java/io/smallrye/config/KeyMapTest.java | 21 --------------- 5 files changed, 10 insertions(+), 72 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/KeyMap.java b/implementation/src/main/java/io/smallrye/config/KeyMap.java index df35f6611..2ea85c7db 100644 --- a/implementation/src/main/java/io/smallrye/config/KeyMap.java +++ b/implementation/src/main/java/io/smallrye/config/KeyMap.java @@ -4,7 +4,6 @@ import java.util.HashMap; import java.util.IdentityHashMap; import java.util.Iterator; -import java.util.Map; import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; @@ -257,29 +256,4 @@ public StringBuilder toString(StringBuilder b) { public String toString() { return toString(new StringBuilder()).toString(); } - - public Map toMap() { - final Map map = new HashMap<>(); - unwrap(this, "", map); - return map; - } - - private void unwrap(KeyMap keyMap, String key, Map map) { - for (String path : keyMap.keySet()) { - final KeyMap nested = keyMap.get(path); - final String nestedKey = key.length() == 0 ? path : key + "." + path; - if (nested.any != null) { - map.put(nestedKey + ".*", nested.any.getRootValue()); - unwrap(nested.any, nestedKey + ".*", map); - } - if (!nested.hasRootValue()) { - unwrap(nested, nestedKey, map); - } else { - map.put(nestedKey, nested.getRootValue()); - if (!nested.keySet().isEmpty()) { - unwrap(nested, nestedKey, map); - } - } - } - } } diff --git a/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java b/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java index 0a24d1959..b985d5f27 100644 --- a/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/KeyMapBackedConfigSource.java @@ -1,12 +1,13 @@ package io.smallrye.config; +import java.util.Collections; import java.util.Map; import org.eclipse.microprofile.config.spi.ConfigSource; import io.smallrye.config.common.AbstractConfigSource; -public abstract class KeyMapBackedConfigSource extends AbstractConfigSource { +public class KeyMapBackedConfigSource extends AbstractConfigSource { private static final long serialVersionUID = 4378754290346888762L; private final KeyMap properties; @@ -23,7 +24,7 @@ public KeyMapBackedConfigSource(final String name, final int ordinal, final KeyM @Override public Map getProperties() { - return properties.toMap(); + return Collections.emptyMap(); } @Override diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java index e14c9b49e..dce4a71a6 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java @@ -333,19 +333,8 @@ public SmallRyeConfig build() { if (!defaultValues.isEmpty() || !mappingProvider.getDefaultValues().isEmpty()) { final KeyMap mappingProviderDefaultValues = mappingProvider.getDefaultValues(); defaultValues.forEach((key, value) -> mappingProviderDefaultValues.findOrAdd(key).putRootValue(value)); - withSources(new KeyMapBackedConfigSource("DefaultValuesConfigSource", mappingProviderDefaultValues) { - private static final long serialVersionUID = 7847969724478280439L; - - @Override - public Map getProperties() { - return new HashMap<>(); - } - - @Override - public int getOrdinal() { - return Integer.MIN_VALUE; - } - }); + withSources( + new KeyMapBackedConfigSource("DefaultValuesConfigSource", Integer.MIN_VALUE, mappingProviderDefaultValues)); } try { diff --git a/implementation/src/test/java/io/smallrye/config/KeyMapBackedConfigSourceTest.java b/implementation/src/test/java/io/smallrye/config/KeyMapBackedConfigSourceTest.java index 7b8272ba6..6ce693a26 100644 --- a/implementation/src/test/java/io/smallrye/config/KeyMapBackedConfigSourceTest.java +++ b/implementation/src/test/java/io/smallrye/config/KeyMapBackedConfigSourceTest.java @@ -8,7 +8,6 @@ import org.junit.jupiter.api.Test; class KeyMapBackedConfigSourceTest { - @Test void getProperties() { KeyMap map = new KeyMap<>(); @@ -17,12 +16,9 @@ void getProperties() { map.findOrAdd("root.foo.bar.*").putRootValue("baz"); map.findOrAdd("root.foo.bar.*.baz").putRootValue("anything"); - final ConfigSource source = getSource(map); - final Map properties = source.getProperties(); - assertTrue(properties.containsKey("root.foo")); - assertTrue(properties.containsKey("root.foo.bar")); - assertTrue(properties.containsKey("root.foo.bar.*")); - assertTrue(properties.containsKey("root.foo.bar.*.baz")); + ConfigSource source = getSource(map); + Map properties = source.getProperties(); + assertTrue(properties.isEmpty()); } @Test @@ -33,7 +29,7 @@ void getValue() { map.findOrAdd("root.foo.bar.*").putRootValue("baz"); map.findOrAdd("root.foo.bar.*.baz").putRootValue("anything"); - final ConfigSource source = getSource(map); + ConfigSource source = getSource(map); assertEquals("bar", source.getValue("root.foo")); assertEquals("baz", source.getValue("root.foo.bar")); assertEquals("baz", source.getValue("root.foo.bar.x")); @@ -45,7 +41,6 @@ void getValue() { } private ConfigSource getSource(final KeyMap properties) { - return new KeyMapBackedConfigSource("test", 0, properties) { - }; + return new KeyMapBackedConfigSource("test", 0, properties); } } diff --git a/implementation/src/test/java/io/smallrye/config/KeyMapTest.java b/implementation/src/test/java/io/smallrye/config/KeyMapTest.java index 2e200196c..0a93065c9 100644 --- a/implementation/src/test/java/io/smallrye/config/KeyMapTest.java +++ b/implementation/src/test/java/io/smallrye/config/KeyMapTest.java @@ -3,7 +3,6 @@ import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashMap; import java.util.Map; @@ -106,26 +105,6 @@ void empty() { assertEquals("bar", map.findRootValue(".foo")); } - @Test - void unwrap() { - KeyMap map = new KeyMap<>(); - map.findOrAdd("root.foo").putRootValue("bar"); - map.findOrAdd("root.foo.bar").putRootValue("baz"); - map.findOrAdd("root.foo.bar.*").putRootValue("baz"); - map.findOrAdd("root.foo.bar.*.baz").putRootValue("anything"); - - Map properties = map.toMap(); - assertTrue(properties.containsKey("root.foo")); - assertTrue(properties.containsKey("root.foo.bar")); - assertTrue(properties.containsKey("root.foo.bar.*")); - assertTrue(properties.containsKey("root.foo.bar.*.baz")); - - assertEquals("bar", properties.get("root.foo")); - assertEquals("baz", properties.get("root.foo.bar")); - assertEquals("baz", properties.get("root.foo.bar.*")); - assertEquals("anything", properties.get("root.foo.bar.*.baz")); - } - @Test void string() { KeyMap map = new KeyMap<>();