From 5d7c946db5327908df776f10162379326bf1b69a Mon Sep 17 00:00:00 2001 From: carm Date: Fri, 21 Feb 2025 18:45:15 +0800 Subject: [PATCH] feat(section): Implement more sections --- .../source/section/ConfigureSection.java | 38 ++ ...apSection.java => AbstractMapSection.java} | 32 +- .../source/section/ImmutableSection.java | 372 ++++++++++++++++++ .../source/section/MemorySection.java | 50 ++- .../source/section/RawMapSection.java | 56 --- .../source/section/ShadedSection.java | 24 +- .../source/section/SourcedSection.java | 48 +++ .../src/test/java/test/section/ShadeTest.java | 11 +- .../configuration/source/json/JSONSource.java | 14 +- .../configuration/source/yaml/YAMLSource.java | 16 +- 10 files changed, 542 insertions(+), 119 deletions(-) rename features/section/src/main/java/cc/carm/lib/configuration/source/section/{MapSection.java => AbstractMapSection.java} (76%) create mode 100644 features/section/src/main/java/cc/carm/lib/configuration/source/section/ImmutableSection.java delete mode 100644 features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java create mode 100644 features/section/src/main/java/cc/carm/lib/configuration/source/section/SourcedSection.java 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 be89b21..b232232 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 @@ -235,6 +235,44 @@ public interface ConfigureSection { return (val instanceof ConfigureSection) ? (ConfigureSection) val : null; } + /** + * Creates a new {@link ConfigureSection} with specified values. + *

The {@link #parent()} of the new section will be this section. + * + * @param data The data to be used to create section. + * @return Newly created section + */ + @NotNull ConfigureSection createSection(@NotNull Map data); + + /** + * Creates a new empty {@link ConfigureSection}. + *

The {@link #parent()} of the new section will be this section. + * + * @return Newly created section + */ + default @NotNull ConfigureSection createSection() { + return createSection(new LinkedHashMap<>()); + } + + /** + * Get or create a section at the given path. + *

+ * Any value previously set at this path will be replaced if it is not a section + * or will be returned if it is an exists {@link ConfigureSection}. + * + * @param path The path to get the section from. + * @return The section at the path, or a new section if it does not exist. + * @see #createSection() + */ + default @NotNull ConfigureSection computeSection(@NotNull String path) { + ConfigureSection current = getSection(path); + if (current == null) { + current = createSection(); + set(path, current); + } + return current; + } + /** * Get the origin value of the path. * diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/AbstractMapSection.java similarity index 76% rename from features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java rename to features/section/src/main/java/cc/carm/lib/configuration/source/section/AbstractMapSection.java index 6d8eab6..e52b929 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/AbstractMapSection.java @@ -6,26 +6,26 @@ import org.jetbrains.annotations.UnmodifiableView; import java.util.*; -public abstract class MapSection> implements ConfigureSection { +public abstract class AbstractMapSection> implements ConfigureSection { protected final @NotNull Map data; protected final @Nullable R parent; - protected MapSection(@Nullable R parent) { + protected AbstractMapSection(@Nullable R parent) { this.parent = parent; this.data = new LinkedHashMap<>(); } public void migrate(Map data) { for (Map.Entry entry : data.entrySet()) { - String key = (entry.getKey() == null) ? "null" : entry.getKey().toString(); + String key = (entry.getKey() == null) ? "" : entry.getKey().toString(); if (entry.getValue() instanceof Map) { - this.data.put(key, createChild((Map) entry.getValue())); + this.data.put(key, createSection((Map) entry.getValue())); } else if (entry.getValue() instanceof List) { List list = new ArrayList<>(); for (Object obj : (List) entry.getValue()) { if (obj instanceof Map) { - list.add(createChild((Map) obj)); + list.add(createSection((Map) obj)); } else { list.add(obj); } @@ -39,10 +39,12 @@ public abstract class MapSection> implements ConfigureSe public abstract @NotNull R self(); - protected abstract @NotNull R createChild(@NotNull Map data); + @Override + public abstract @NotNull R createSection(@NotNull Map data); - protected @NotNull R createChild() { - return createChild(new LinkedHashMap<>()); + @Override + public @NotNull R createSection() { + return createSection(new LinkedHashMap<>()); } @Override @@ -68,8 +70,8 @@ public abstract class MapSection> implements ConfigureSe public @NotNull Map rawMap() { Map output = new LinkedHashMap<>(); for (Map.Entry entry : this.data.entrySet()) { - if (entry.getValue() instanceof MapSection) { - output.put(entry.getKey(), ((MapSection) entry.getValue()).rawMap()); + if (entry.getValue() instanceof AbstractMapSection) { + output.put(entry.getKey(), ((AbstractMapSection) entry.getValue()).rawMap()); } else { output.put(entry.getKey(), entry.getValue()); } @@ -89,7 +91,7 @@ public abstract class MapSection> implements ConfigureSe @Override public void set(@NotNull String path, @Nullable Object value) { - if (value instanceof Map) value = createChild((Map) value); + if (value instanceof Map) value = createSection((Map) value); R section = getSectionFor(path); if (section == this) { @@ -123,7 +125,7 @@ public abstract class MapSection> implements ConfigureSe if (index == -1) return self(); String root = path.substring(0, index); - return (R) data().computeIfAbsent(root, k -> createChild()); + return (R) data().computeIfAbsent(root, k -> createSection()); } /** @@ -133,14 +135,14 @@ public abstract class MapSection> implements ConfigureSe * @param parent The parent path * @param deep If the mapping should be deep */ - protected Map mappingValues(@NotNull MapSection section, @Nullable String parent, boolean deep) { + protected Map mappingValues(@NotNull AbstractMapSection section, @Nullable String parent, boolean deep) { Map output = new LinkedHashMap<>(); for (Map.Entry entry : section.data().entrySet()) { String path = (parent == null ? "" : parent + pathSeparator()) + entry.getKey(); output.remove(path); output.put(path, entry.getValue()); - if (deep && entry.getValue() instanceof MapSection) { - output.putAll(mappingValues((MapSection) entry.getValue(), path, true)); + if (deep && entry.getValue() instanceof AbstractMapSection) { + output.putAll(mappingValues((AbstractMapSection) entry.getValue(), path, true)); } } return output; 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 new file mode 100644 index 0000000..10274c5 --- /dev/null +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/ImmutableSection.java @@ -0,0 +1,372 @@ +package cc.carm.lib.configuration.source.section; + +import cc.carm.lib.configuration.function.DataFunction; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.UnmodifiableView; + +import java.util.*; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Stream; + +public class ImmutableSection implements ConfigureSection { + + public static ImmutableSection of(@NotNull ConfigureSection section) { + if (section instanceof ImmutableSection) { + return (ImmutableSection) section; + } else return new ImmutableSection(null, section); + } + + protected final @Nullable ImmutableSection parent; + protected final @NotNull ConfigureSection section; + + private ImmutableSection(@Nullable ImmutableSection parent, @NotNull ConfigureSection section) { + this.parent = parent; + this.section = section; + } + + @Override + public @Nullable ImmutableSection parent() { + return this.parent; + } + + private @NotNull ConfigureSection section() { + return section; + } + + @Override + public @NotNull @UnmodifiableView Map getValues(boolean deep) { + return section().getValues(deep); + } + + @Override + public void set(@NotNull String path, @Nullable Object value) { + throw new IllegalStateException("This section is not modifiable!"); + } + + @Override + public void remove(@NotNull String path) { + throw new IllegalStateException("This section is not modifiable!"); + } + + @Override + public @NotNull ConfigureSection createSection(@NotNull Map data) { + return new ImmutableSection(this, section().createSection(data)); + } + + @Override + public @Nullable Object get(@NotNull String path) { + Object value = section().get(path); + if (value instanceof ConfigureSection && !(value instanceof ImmutableSection)) { + return new ImmutableSection(this, (ConfigureSection) value); + } + return value; + } + + @Override + public @Nullable ConfigureSection getSection(@NotNull String path) { + ConfigureSection get = section().getSection(path); + if (get != null && !(get instanceof ImmutableSection)) { + return new ImmutableSection(this, get); + } + return get; + } + + @Override + public char pathSeparator() { + return section().pathSeparator(); + } + + @Override + public boolean isRoot() { + return section().isRoot(); + } + + @Override + public boolean isEmpty() { + return section().isEmpty(); + } + + @Override + public @NotNull @UnmodifiableView Set getKeys(boolean deep) { + return section().getKeys(deep); + } + + @Override + public @NotNull @UnmodifiableView Set keys() { + return section().keys(); + } + + @Override + public @NotNull @UnmodifiableView Map values() { + return section().values(); + } + + @Override + public Stream> stream() { + return section().stream(); + } + + @Override + public void forEach(@NotNull BiConsumer action) { + section().forEach(action); + } + + @Override + public boolean contains(@NotNull String path) { + return section().contains(path); + } + + @Override + public boolean containsValue(@NotNull String path) { + return section().containsValue(path); + } + + @Override + public boolean isType(@NotNull String path, @NotNull Class typeClass) { + return section().isType(path, typeClass); + } + + @Override + public boolean isList(@NotNull String path) { + return section().isList(path); + } + + @Override + public @Nullable List getList(@NotNull String path) { + return section().getList(path); + } + + @Override + public boolean isSection(@NotNull String path) { + return section().isSection(path); + } + + @Override + public @Nullable T get(@NotNull String path, @NotNull Class type) { + return section().get(path, type); + } + + @Override + public @Nullable T get(@NotNull String path, @NotNull DataFunction<@Nullable Object, T> parser) { + return section().get(path, parser); + } + + @Override + public @Nullable T get(@NotNull String path, @Nullable T defaults, @NotNull Class clazz) { + return section().get(path, defaults, clazz); + } + + @Override + public @Nullable T get(@NotNull String path, @Nullable T defaultValue, @NotNull DataFunction parser) { + return section().get(path, defaultValue, parser); + } + + @Override + public boolean isBoolean(@NotNull String path) { + return section().isBoolean(path); + } + + @Override + public boolean getBoolean(@NotNull String path) { + return section().getBoolean(path); + } + + @Override + public @Nullable Boolean getBoolean(@NotNull String path, @Nullable Boolean def) { + return section().getBoolean(path, def); + } + + @Override + public @Nullable Boolean isByte(@NotNull String path) { + return section().isByte(path); + } + + @Override + public @Nullable Byte getByte(@NotNull String path) { + return section().getByte(path); + } + + @Override + public @Nullable Byte getByte(@NotNull String path, @Nullable Byte def) { + return section().getByte(path, def); + } + + @Override + public boolean isShort(@NotNull String path) { + return section().isShort(path); + } + + @Override + public @Nullable Short getShort(@NotNull String path) { + return section().getShort(path); + } + + @Override + public @Nullable Short getShort(@NotNull String path, @Nullable Short def) { + return section().getShort(path, def); + } + + @Override + public boolean isInt(@NotNull String path) { + return section().isInt(path); + } + + @Override + public @Nullable Integer getInt(@NotNull String path) { + return section().getInt(path); + } + + @Override + public @Nullable Integer getInt(@NotNull String path, @Nullable Integer def) { + return section().getInt(path, def); + } + + @Override + public boolean isLong(@NotNull String path) { + return section().isLong(path); + } + + @Override + public @Nullable Long getLong(@NotNull String path) { + return section().getLong(path); + } + + @Override + public @Nullable Long getLong(@NotNull String path, @Nullable Long def) { + return section().getLong(path, def); + } + + @Override + public boolean isFloat(@NotNull String path) { + return section().isFloat(path); + } + + @Override + public @Nullable Float getFloat(@NotNull String path) { + return section().getFloat(path); + } + + @Override + public @Nullable Float getFloat(@NotNull String path, @Nullable Float def) { + return section().getFloat(path, def); + } + + @Override + public boolean isDouble(@NotNull String path) { + return section().isDouble(path); + } + + @Override + public @Nullable Double getDouble(@NotNull String path) { + return section().getDouble(path); + } + + @Override + public @Nullable Double getDouble(@NotNull String path, @Nullable Double def) { + return section().getDouble(path, def); + } + + @Override + public boolean isChar(@NotNull String path) { + return section().isChar(path); + } + + @Override + public @Nullable Character getChar(@NotNull String path) { + return section().getChar(path); + } + + @Override + public @Nullable Character getChar(@NotNull String path, @Nullable Character def) { + return section().getChar(path, def); + } + + @Override + public boolean isString(@NotNull String path) { + return section().isString(path); + } + + @Override + public @Nullable String getString(@NotNull String path) { + return section().getString(path); + } + + @Override + public @Nullable String getString(@NotNull String path, @Nullable String def) { + return section().getString(path, def); + } + + @Override + public @NotNull List getList(@NotNull String path, @NotNull DataFunction parser) { + return section().getList(path, parser); + } + + @Override + public @NotNull List getStringList(@NotNull String path) { + return section().getStringList(path); + } + + @Override + public @NotNull List getIntegerList(@NotNull String path) { + return section().getIntegerList(path); + } + + @Override + public @NotNull List getLongList(@NotNull String path) { + return section().getLongList(path); + } + + @Override + public @NotNull List getDoubleList(@NotNull String path) { + return section().getDoubleList(path); + } + + @Override + public @NotNull List getFloatList(@NotNull String path) { + return section().getFloatList(path); + } + + @Override + public @NotNull List getByteList(@NotNull String path) { + return section().getByteList(path); + } + + @Override + public @NotNull List getCharList(@NotNull String path) { + return section().getCharList(path); + } + + @Override + public > @NotNull C getCollection(@NotNull String path, @NotNull Supplier constructor, @NotNull DataFunction parser) { + return section().getCollection(path, constructor, parser); + } + + @Override + public @NotNull Stream stream(@NotNull String path) { + return section().stream(path); + } + + @Override + public @NotNull Stream stream(@NotNull String path, @NotNull Function parser) { + return section().stream(path, parser); + } + + @Override + public String childPath(String path) { + return section().childPath(path); + } + + @Override + public int hashCode() { + return section.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(section, obj); + } + +} diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java index d184057..1dfddfe 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java @@ -5,44 +5,52 @@ import org.jetbrains.annotations.Nullable; import java.util.LinkedHashMap; import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; -public class MemorySection extends MapSection { +public class MemorySection extends AbstractMapSection { - public static @NotNull MemorySection root(@NotNull ConfigureSource source) { - return new MemorySection(source, new LinkedHashMap<>(), null); + public static MemorySection of() { + return of((MemorySection) null); } - public static @NotNull MemorySection root(@NotNull ConfigureSource source, - @Nullable Map raw) { - return new MemorySection(source, raw == null ? new LinkedHashMap<>() : raw, null); + public static MemorySection of(@NotNull Consumer> data) { + return of(() -> { + Map map = new LinkedHashMap<>(); + data.accept(map); + return map; + }); } - protected final @NotNull ConfigureSource source; + public static MemorySection of(@NotNull Supplier> data) { + return of(data.get(), null); + } - protected MemorySection(@NotNull ConfigureSource source, - @NotNull Map raw, @Nullable MemorySection parent) { + public static MemorySection of(@NotNull Map data) { + return of(data, null); + } + + public static MemorySection of(@Nullable MemorySection parent) { + return of(new LinkedHashMap<>(), parent); + } + + public static MemorySection of(@NotNull Map data, @Nullable MemorySection parent) { + return new MemorySection(data, parent); + } + + public MemorySection(@NotNull Map raw, @Nullable MemorySection parent) { super(parent); - this.source = source; migrate(raw); } - public @NotNull ConfigureSource source() { - return source; - } - - @Override - public char pathSeparator() { - return source().pathSeparator(); - } - @Override public @NotNull MemorySection self() { return this; } @Override - protected @NotNull MemorySection createChild(@NotNull Map data) { - return new MemorySection(source(), data, this); + public @NotNull MemorySection createSection(@NotNull Map data) { + return new MemorySection(data, this); } } diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java deleted file mode 100644 index 94681cd..0000000 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/RawMapSection.java +++ /dev/null @@ -1,56 +0,0 @@ -package cc.carm.lib.configuration.source.section; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Consumer; -import java.util.function.Supplier; - -public class RawMapSection extends MapSection { - - public static RawMapSection of() { - return of((RawMapSection) null); - } - - public static RawMapSection of(@NotNull Consumer> data) { - return of(() -> { - Map map = new LinkedHashMap<>(); - data.accept(map); - return map; - }); - } - - public static RawMapSection of(@NotNull Supplier> data) { - return of(data.get(), null); - } - - public static RawMapSection of(@NotNull Map data) { - return of(data, null); - } - - public static RawMapSection of(@Nullable RawMapSection parent) { - return of(new LinkedHashMap<>(), parent); - } - - public static RawMapSection of(@NotNull Map data, @Nullable RawMapSection parent) { - return new RawMapSection(data, parent); - } - - protected RawMapSection(@NotNull Map raw, @Nullable RawMapSection parent) { - super(parent); - migrate(raw); - } - - @Override - public @NotNull RawMapSection self() { - return this; - } - - @Override - protected @NotNull RawMapSection createChild(@NotNull Map data) { - return new RawMapSection(data, this); - } - -} 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 4f2fc02..28d9a45 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 @@ -5,30 +5,33 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnmodifiableView; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.Map; public class ShadedSection implements ConfigureSection { - - protected final @Nullable ShadedSection parent; protected final @NotNull ConfigureSection section; - protected final @Nullable ConfigureSection template; + protected final @NotNull ConfigureSection template; - public ShadedSection(@NotNull ShadedSection parent, - @NotNull ConfigureSection section, @Nullable ConfigureSection template) { - this.parent = parent; + public ShadedSection(@NotNull ConfigureSection section, @Nullable ConfigureSection template) { this.section = section; this.template = template; } - @Override public @Nullable ShadedSection parent() { - return this.parent; + return null; } @Override public @NotNull @UnmodifiableView Map getValues(boolean deep) { + if (deep) { + Map values = new LinkedHashMap<>(template.getValues(true)); + values.putAll(section.getValues(true)); + return values; + } + + return Collections.emptyMap(); } @@ -58,4 +61,9 @@ public class ShadedSection implements ConfigureSection { section.remove(path); } + @Override + public @NotNull ConfigureSection createSection(@NotNull Map data) { + return null; + } + } \ No newline at end of file diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/SourcedSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/SourcedSection.java new file mode 100644 index 0000000..205fd92 --- /dev/null +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/SourcedSection.java @@ -0,0 +1,48 @@ +package cc.carm.lib.configuration.source.section; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class SourcedSection extends AbstractMapSection { + + public static @NotNull SourcedSection root(@NotNull ConfigureSource source) { + return new SourcedSection(source, new LinkedHashMap<>(), null); + } + + public static @NotNull SourcedSection root(@NotNull ConfigureSource source, + @Nullable Map raw) { + return new SourcedSection(source, raw == null ? new LinkedHashMap<>() : raw, null); + } + + protected final @NotNull ConfigureSource source; + + public SourcedSection(@NotNull ConfigureSource source, + @NotNull Map raw, @Nullable SourcedSection parent) { + super(parent); + this.source = source; + migrate(raw); + } + + public @NotNull ConfigureSource source() { + return source; + } + + @Override + public char pathSeparator() { + return source().pathSeparator(); + } + + @Override + public @NotNull SourcedSection self() { + return this; + } + + @Override + public @NotNull SourcedSection createSection(@NotNull Map data) { + return new SourcedSection(source(), data, this); + } + +} diff --git a/features/section/src/test/java/test/section/ShadeTest.java b/features/section/src/test/java/test/section/ShadeTest.java index 1626846..b1a182e 100644 --- a/features/section/src/test/java/test/section/ShadeTest.java +++ b/features/section/src/test/java/test/section/ShadeTest.java @@ -1,7 +1,7 @@ package test.section; import cc.carm.lib.configuration.source.section.ConfigureSection; -import cc.carm.lib.configuration.source.section.RawMapSection; +import cc.carm.lib.configuration.source.section.MemorySection; import org.junit.Test; import java.util.Arrays; @@ -16,7 +16,7 @@ public class ShadeTest { @Test public void test() { - ConfigureSection template = RawMapSection.of(data -> { + ConfigureSection template = MemorySection.of(data -> { data.put("name", "GentleMan"); data.put("age", 12); data.put("gender", "male"); @@ -26,8 +26,7 @@ public class ShadeTest { data.put("addresses", address); data.put("cards", Arrays.asList("00000", "11111", "22222")); }); - - ConfigureSection source = RawMapSection.of(data -> { + ConfigureSection source = MemorySection.of(data -> { data.put("age", 25); Map address = new LinkedHashMap<>(); address.put("NewOne", "Guangdong Road 505"); @@ -35,6 +34,10 @@ public class ShadeTest { data.put("cards", Arrays.asList("33333", "55555")); // 应当直接覆盖原先的List }); + + + + } } diff --git a/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java b/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java index 22e8f50..ce473c6 100644 --- a/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java +++ b/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java @@ -2,7 +2,7 @@ package cc.carm.lib.configuration.source.json; import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.file.FileConfigSource; -import cc.carm.lib.configuration.source.section.MemorySection; +import cc.carm.lib.configuration.source.section.SourcedSection; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSerializer; @@ -14,17 +14,17 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; -public class JSONSource extends FileConfigSource, JSONSource> { +public class JSONSource extends FileConfigSource, JSONSource> { public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder() .serializeNulls().disableHtmlEscaping().setPrettyPrinting() .registerTypeAdapter( - MemorySection.class, - (JsonSerializer) (src, t, c) -> c.serialize(src.data()) + SourcedSection.class, + (JsonSerializer) (src, t, c) -> c.serialize(src.data()) ).create(); protected final @NotNull Gson gson; - protected @Nullable MemorySection rootSection; + protected @Nullable SourcedSection rootSection; protected JSONSource(@NotNull ConfigurationHolder holder, @NotNull File file, @Nullable String resourcePath) { @@ -58,7 +58,7 @@ public class JSONSource extends FileConfigSource data = fileReader(reader -> gson.fromJson(reader, LinkedHashMap.class)); - this.rootSection = MemorySection.root(this, data); + this.rootSection = SourcedSection.root(this, data); this.lastUpdateMillis = System.currentTimeMillis(); // 更新时间 } diff --git a/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java b/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java index 6e10a6b..1480f2a 100644 --- a/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java +++ b/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java @@ -5,7 +5,7 @@ import cc.carm.lib.configuration.commentable.CommentableOptions; import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.file.FileConfigSource; import cc.carm.lib.configuration.source.section.ConfigureSection; -import cc.carm.lib.configuration.source.section.MemorySection; +import cc.carm.lib.configuration.source.section.SourcedSection; import cc.carm.lib.yamlcommentupdater.CommentedSection; import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter; import org.jetbrains.annotations.NotNull; @@ -26,14 +26,14 @@ import java.nio.charset.StandardCharsets; import java.util.*; public class YAMLSource - extends FileConfigSource, YAMLSource> + extends FileConfigSource, YAMLSource> implements CommentedSection { protected final @NotNull YamlConstructor yamlConstructor; protected final @NotNull YamlRepresenter yamlRepresenter; protected final @NotNull Yaml yaml; - protected @Nullable MemorySection rootSection; + protected @Nullable SourcedSection rootSection; protected YAMLSource(@NotNull ConfigurationHolder holder, @NotNull File file, @Nullable String resourcePath) { @@ -65,7 +65,7 @@ public class YAMLSource } @Override - public @NotNull MemorySection section() { + public @NotNull SourcedSection section() { return Objects.requireNonNull(this.rootSection, "Root section is not initialized."); } @@ -130,18 +130,18 @@ public class YAMLSource return this.saveToString(section()); } - public @NotNull MemorySection loadFromString(@NotNull String data) throws Exception { + public @NotNull SourcedSection loadFromString(@NotNull String data) throws Exception { MappingNode mappingNode; try (Reader reader = new UnicodeReader(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)))) { Node rawNode = this.yaml.compose(reader); mappingNode = (MappingNode) rawNode; } - if (mappingNode == null) return MemorySection.root(this); + if (mappingNode == null) return SourcedSection.root(this); Map map = new LinkedHashMap<>(); this.constructMap(mappingNode, map); - return MemorySection.root(this, map); + return SourcedSection.root(this, map); } private void constructMap(@NotNull MappingNode mappingNode, @NotNull Map section) { @@ -170,7 +170,7 @@ public class YAMLSource public String serializeValue(@NotNull String key, @NotNull Object value) { Map map = new LinkedHashMap<>(); map.put(key, value); - return saveToString(MemorySection.root(this, map)); + return saveToString(SourcedSection.root(this, map)); } @Override