From c2a9e2254cdf8af74b894c98102ab19f0811eb07 Mon Sep 17 00:00:00 2001 From: carm Date: Tue, 4 Mar 2025 01:06:21 +0800 Subject: [PATCH] feat(section): Add ConfigureSection#asMap function --- core/pom.xml | 2 +- .../source/section/ConfigureSection.java | 14 +- .../source/section/ConfigureSource.java | 5 + demo/pom.xml | 2 +- features/commentable/pom.xml | 2 +- features/file/pom.xml | 2 +- features/section/pom.xml | 2 +- .../source/section/AbstractMapSection.java | 7 +- .../source/section/ImmutableSection.java | 148 +++++++++--------- .../source/section/ShadedSection.java | 6 + features/text/pom.xml | 2 +- features/versioned/pom.xml | 2 +- pom.xml | 2 +- providers/gson/pom.xml | 2 +- providers/mongodb/pom.xml | 2 +- .../source/mongodb/MongoSource.java | 2 +- providers/sql/pom.xml | 2 +- .../configuration/source/sql/SQLSource.java | 4 +- providers/yaml/pom.xml | 2 +- 19 files changed, 119 insertions(+), 91 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 8f9e98b..9b16639 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.8 + 4.0.9 4.0.0 diff --git a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java index a12ded5..fee5588 100644 --- a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java +++ b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSection.java @@ -48,7 +48,6 @@ public interface ConfigureSection { return (parent().isRoot() ? "" : parent().fullPath() + pathSeparator()) + path(); } - /** * Get the path separator for the section. * @@ -143,6 +142,17 @@ public interface ConfigureSection { return getValues(false); } + /** + * Get this section as a map. + *

+ * In this map, child {@link ConfigureSection}s will also be represented as {@link Map}s. + * + * @return Map of data values contained within this Section. + */ + @NotNull + @UnmodifiableView + Map asMap(); + /** * Create a stream of all values in this section. * @@ -153,7 +163,7 @@ public interface ConfigureSection { } /** - * Iterates over all keys in this section. + * Iterates over all key-values in this section (include child sections) * * @param action The action to apply to each key. */ diff --git a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java index 202249d..ee95f15 100644 --- a/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java +++ b/core/src/main/java/cc/carm/lib/configuration/source/section/ConfigureSource.java @@ -107,6 +107,11 @@ public abstract class ConfigureSource< return section().getKeys(deep); } + @Override + public @NotNull @UnmodifiableView Map asMap() { + return section().asMap(); + } + @Override public @NotNull ConfigureSection createSection(@NotNull String path, @NotNull Map data) { return section().createSection(path, data); diff --git a/demo/pom.xml b/demo/pom.xml index 293fa60..5646638 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.8 + 4.0.9 4.0.0 diff --git a/features/commentable/pom.xml b/features/commentable/pom.xml index 61912b7..d4298ed 100644 --- a/features/commentable/pom.xml +++ b/features/commentable/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.8 + 4.0.9 ../../pom.xml diff --git a/features/file/pom.xml b/features/file/pom.xml index 5ac7568..3e020bf 100644 --- a/features/file/pom.xml +++ b/features/file/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.8 + 4.0.9 ../../pom.xml diff --git a/features/section/pom.xml b/features/section/pom.xml index c3969b1..73d49c7 100644 --- a/features/section/pom.xml +++ b/features/section/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.8 + 4.0.9 ../../pom.xml diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/AbstractMapSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/AbstractMapSection.java index 0bd40f1..478aea4 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/AbstractMapSection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/AbstractMapSection.java @@ -75,17 +75,18 @@ public abstract class AbstractMapSection> implem return deep ? getKeys(true).size() : this.data.size(); } + @Override @UnmodifiableView - public @NotNull Map rawMap() { + public @NotNull Map asMap() { Map output = new LinkedHashMap<>(); for (Map.Entry entry : this.data.entrySet()) { if (entry.getValue() instanceof AbstractMapSection) { - output.put(entry.getKey(), ((AbstractMapSection) entry.getValue()).rawMap()); + output.put(entry.getKey(), ((AbstractMapSection) entry.getValue()).asMap()); } else if (entry.getValue() instanceof List) { List list = new ArrayList<>(); for (Object obj : (List) entry.getValue()) { if (obj instanceof AbstractMapSection) { - list.add(((AbstractMapSection) obj).rawMap()); + list.add(((AbstractMapSection) obj).asMap()); } else { list.add(obj); } diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/ImmutableSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/ImmutableSection.java index 2d87dfd..c2430ce 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/ImmutableSection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/ImmutableSection.java @@ -20,13 +20,18 @@ public class ImmutableSection implements ConfigureSection { } protected final @Nullable ImmutableSection parent; - protected final @NotNull ConfigureSection section; + protected final @NotNull ConfigureSection raw; - private ImmutableSection(@Nullable ImmutableSection parent, @NotNull ConfigureSection section) { + private ImmutableSection(@Nullable ImmutableSection parent, @NotNull ConfigureSection raw) { this.parent = parent; - this.section = section; + this.raw = raw; } + private @NotNull ConfigureSection raw() { + return raw; + } + + @Override public @Nullable ImmutableSection parent() { return this.parent; @@ -34,16 +39,12 @@ public class ImmutableSection implements ConfigureSection { @Override public @NotNull String path() { - return section().path(); - } - - private @NotNull ConfigureSection section() { - return section; + return raw().path(); } @Override public @NotNull @UnmodifiableView Map getValues(boolean deep) { - return section().getValues(deep); + return raw().getValues(deep); } @Override @@ -58,12 +59,12 @@ public class ImmutableSection implements ConfigureSection { @Override public @NotNull ImmutableSection createSection(@NotNull String path, @NotNull Map data) { - return new ImmutableSection(this, section().createSection(path, data)); + return new ImmutableSection(this, raw().createSection(path, data)); } @Override public @Nullable Object get(@NotNull String path) { - Object value = section().get(path); + Object value = raw().get(path); if (value instanceof ConfigureSection && !(value instanceof ImmutableSection)) { return new ImmutableSection(this, (ConfigureSection) value); } @@ -72,7 +73,7 @@ public class ImmutableSection implements ConfigureSection { @Override public @Nullable ConfigureSection getSection(@NotNull String path) { - ConfigureSection get = section().getSection(path); + ConfigureSection get = raw().getSection(path); if (get != null && !(get instanceof ImmutableSection)) { return new ImmutableSection(this, get); } @@ -81,297 +82,302 @@ public class ImmutableSection implements ConfigureSection { @Override public char pathSeparator() { - return section().pathSeparator(); + return raw().pathSeparator(); } @Override public boolean isRoot() { - return section().isRoot(); + return raw().isRoot(); } @Override public boolean isEmpty() { - return section().isEmpty(); + return raw().isEmpty(); } @Override public @NotNull @UnmodifiableView Set getKeys(boolean deep) { - return section().getKeys(deep); + return raw().getKeys(deep); } @Override public @NotNull @UnmodifiableView Set keys() { - return section().keys(); + return raw().keys(); } @Override public @NotNull @UnmodifiableView Map values() { - return section().values(); + return raw().values(); + } + + @Override + public @NotNull @UnmodifiableView Map asMap() { + return raw().asMap(); } @Override public Stream> stream() { - return section().stream(); + return raw().stream(); } @Override public void forEach(@NotNull BiConsumer action) { - section().forEach(action); + raw().forEach(action); } @Override public boolean contains(@NotNull String path) { - return section().contains(path); + return raw().contains(path); } @Override public boolean containsValue(@NotNull String path) { - return section().containsValue(path); + return raw().containsValue(path); } @Override public boolean isType(@NotNull String path, @NotNull Class typeClass) { - return section().isType(path, typeClass); + return raw().isType(path, typeClass); } @Override public boolean isList(@NotNull String path) { - return section().isList(path); + return raw().isList(path); } @Override public @Nullable List getList(@NotNull String path) { - return section().getList(path); + return raw().getList(path); } @Override public boolean isSection(@NotNull String path) { - return section().isSection(path); + return raw().isSection(path); } @Override public @Nullable T get(@NotNull String path, @NotNull Class type) { - return section().get(path, type); + return raw().get(path, type); } @Override public @Nullable T get(@NotNull String path, @NotNull DataFunction<@Nullable Object, T> parser) { - return section().get(path, parser); + return raw().get(path, parser); } @Override public @Nullable T get(@NotNull String path, @Nullable T defaults, @NotNull Class clazz) { - return section().get(path, defaults, clazz); + return raw().get(path, defaults, clazz); } @Override public @Nullable T get(@NotNull String path, @Nullable T defaultValue, @NotNull DataFunction parser) { - return section().get(path, defaultValue, parser); + return raw().get(path, defaultValue, parser); } @Override public boolean isBoolean(@NotNull String path) { - return section().isBoolean(path); + return raw().isBoolean(path); } @Override public boolean getBoolean(@NotNull String path) { - return section().getBoolean(path); + return raw().getBoolean(path); } @Override public @Nullable Boolean getBoolean(@NotNull String path, @Nullable Boolean def) { - return section().getBoolean(path, def); + return raw().getBoolean(path, def); } @Override public @Nullable Boolean isByte(@NotNull String path) { - return section().isByte(path); + return raw().isByte(path); } @Override public @Nullable Byte getByte(@NotNull String path) { - return section().getByte(path); + return raw().getByte(path); } @Override public @Nullable Byte getByte(@NotNull String path, @Nullable Byte def) { - return section().getByte(path, def); + return raw().getByte(path, def); } @Override public boolean isShort(@NotNull String path) { - return section().isShort(path); + return raw().isShort(path); } @Override public @Nullable Short getShort(@NotNull String path) { - return section().getShort(path); + return raw().getShort(path); } @Override public @Nullable Short getShort(@NotNull String path, @Nullable Short def) { - return section().getShort(path, def); + return raw().getShort(path, def); } @Override public boolean isInt(@NotNull String path) { - return section().isInt(path); + return raw().isInt(path); } @Override public @Nullable Integer getInt(@NotNull String path) { - return section().getInt(path); + return raw().getInt(path); } @Override public @Nullable Integer getInt(@NotNull String path, @Nullable Integer def) { - return section().getInt(path, def); + return raw().getInt(path, def); } @Override public boolean isLong(@NotNull String path) { - return section().isLong(path); + return raw().isLong(path); } @Override public @Nullable Long getLong(@NotNull String path) { - return section().getLong(path); + return raw().getLong(path); } @Override public @Nullable Long getLong(@NotNull String path, @Nullable Long def) { - return section().getLong(path, def); + return raw().getLong(path, def); } @Override public boolean isFloat(@NotNull String path) { - return section().isFloat(path); + return raw().isFloat(path); } @Override public @Nullable Float getFloat(@NotNull String path) { - return section().getFloat(path); + return raw().getFloat(path); } @Override public @Nullable Float getFloat(@NotNull String path, @Nullable Float def) { - return section().getFloat(path, def); + return raw().getFloat(path, def); } @Override public boolean isDouble(@NotNull String path) { - return section().isDouble(path); + return raw().isDouble(path); } @Override public @Nullable Double getDouble(@NotNull String path) { - return section().getDouble(path); + return raw().getDouble(path); } @Override public @Nullable Double getDouble(@NotNull String path, @Nullable Double def) { - return section().getDouble(path, def); + return raw().getDouble(path, def); } @Override public boolean isChar(@NotNull String path) { - return section().isChar(path); + return raw().isChar(path); } @Override public @Nullable Character getChar(@NotNull String path) { - return section().getChar(path); + return raw().getChar(path); } @Override public @Nullable Character getChar(@NotNull String path, @Nullable Character def) { - return section().getChar(path, def); + return raw().getChar(path, def); } @Override public boolean isString(@NotNull String path) { - return section().isString(path); + return raw().isString(path); } @Override public @Nullable String getString(@NotNull String path) { - return section().getString(path); + return raw().getString(path); } @Override public @Nullable String getString(@NotNull String path, @Nullable String def) { - return section().getString(path, def); + return raw().getString(path, def); } @Override public @NotNull List getList(@NotNull String path, @NotNull DataFunction parser) { - return section().getList(path, parser); + return raw().getList(path, parser); } @Override public @NotNull List getStringList(@NotNull String path) { - return section().getStringList(path); + return raw().getStringList(path); } @Override public @NotNull List getIntegerList(@NotNull String path) { - return section().getIntegerList(path); + return raw().getIntegerList(path); } @Override public @NotNull List getLongList(@NotNull String path) { - return section().getLongList(path); + return raw().getLongList(path); } @Override public @NotNull List getDoubleList(@NotNull String path) { - return section().getDoubleList(path); + return raw().getDoubleList(path); } @Override public @NotNull List getFloatList(@NotNull String path) { - return section().getFloatList(path); + return raw().getFloatList(path); } @Override public @NotNull List getByteList(@NotNull String path) { - return section().getByteList(path); + return raw().getByteList(path); } @Override public @NotNull List getCharList(@NotNull String path) { - return section().getCharList(path); + return raw().getCharList(path); } @Override public > @NotNull C getCollection(@NotNull String path, @NotNull Supplier constructor, @NotNull DataFunction parser) { - return section().getCollection(path, constructor, parser); + return raw().getCollection(path, constructor, parser); } @Override public @NotNull Stream stream(@NotNull String path) { - return section().stream(path); + return raw().stream(path); } @Override public @NotNull Stream stream(@NotNull String path, @NotNull Function parser) { - return section().stream(path, parser); + return raw().stream(path, parser); } @Override public String childPath(String path) { - return section().childPath(path); + return raw().childPath(path); } @Override public int hashCode() { - return section.hashCode(); + return raw.hashCode(); } @Override public boolean equals(Object obj) { - return Objects.equals(section, obj); + return Objects.equals(raw, obj); } } diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/ShadedSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/ShadedSection.java index 6141483..d7273a1 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/ShadedSection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/ShadedSection.java @@ -40,6 +40,12 @@ public class ShadedSection implements ConfigureSection { return merge(template, source).getValues(deep); } + @Override + public @NotNull @UnmodifiableView Map asMap() { + if (source == null) return template.asMap(); + return merge(template, source).asMap(); + } + @Override public @NotNull @UnmodifiableView Set getKeys(boolean deep) { Set keys = new HashSet<>(template.getKeys(deep)); diff --git a/features/text/pom.xml b/features/text/pom.xml index efc9d0e..6115b37 100644 --- a/features/text/pom.xml +++ b/features/text/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.8 + 4.0.9 ../../pom.xml diff --git a/features/versioned/pom.xml b/features/versioned/pom.xml index 44b4aae..5f54fe8 100644 --- a/features/versioned/pom.xml +++ b/features/versioned/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.8 + 4.0.9 ../../pom.xml diff --git a/pom.xml b/pom.xml index e29d57d..ec69c79 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ cc.carm.lib easyconfiguration-parent pom - 4.0.8 + 4.0.9 core features/section diff --git a/providers/gson/pom.xml b/providers/gson/pom.xml index a3c26c0..e756244 100644 --- a/providers/gson/pom.xml +++ b/providers/gson/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.8 + 4.0.9 ../../pom.xml 4.0.0 diff --git a/providers/mongodb/pom.xml b/providers/mongodb/pom.xml index 2768078..6de61ed 100644 --- a/providers/mongodb/pom.xml +++ b/providers/mongodb/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.8 + 4.0.9 ../../pom.xml 4.0.0 diff --git a/providers/mongodb/src/main/java/cc/carm/lib/configuration/source/mongodb/MongoSource.java b/providers/mongodb/src/main/java/cc/carm/lib/configuration/source/mongodb/MongoSource.java index d7d1866..e09d4c3 100644 --- a/providers/mongodb/src/main/java/cc/carm/lib/configuration/source/mongodb/MongoSource.java +++ b/providers/mongodb/src/main/java/cc/carm/lib/configuration/source/mongodb/MongoSource.java @@ -55,7 +55,7 @@ public class MongoSource extends ConfigureSource data = this.rootSection.rawMap(); + Map data = this.rootSection.asMap(); if (data.isEmpty()) return; // Skip saving if empty if (data.containsKey("_id") && data.size() == 1) return; // Skip saving if only contains _id diff --git a/providers/sql/pom.xml b/providers/sql/pom.xml index 97e9872..597177c 100644 --- a/providers/sql/pom.xml +++ b/providers/sql/pom.xml @@ -6,7 +6,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.8 + 4.0.9 ../../pom.xml diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java index ec95151..2cc70aa 100644 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java +++ b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java @@ -109,12 +109,12 @@ public class SQLSource extends ConfigureSource) { List list = new ArrayList<>(); for (Object obj : (List) value) { if (obj instanceof SourcedSection) { - list.add(((SourcedSection) obj).rawMap()); + list.add(((SourcedSection) obj).asMap()); } else { list.add(obj); } diff --git a/providers/yaml/pom.xml b/providers/yaml/pom.xml index 42e4ace..b02e384 100644 --- a/providers/yaml/pom.xml +++ b/providers/yaml/pom.xml @@ -6,7 +6,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.8 + 4.0.9 ../../pom.xml