1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 10:38:19 +08:00

feat: Split MapSection and MemorySection

This commit is contained in:
2025-02-20 03:01:45 +08:00
parent 80f03ec501
commit 8e7ac263e7
13 changed files with 28 additions and 25 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>4.0.3</version>
<version>4.0.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>4.0.3</version>
<version>4.0.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<version>4.0.3</version>
<version>4.0.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<version>4.0.3</version>
<version>4.0.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<version>4.0.3</version>
<version>4.0.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
@@ -10,9 +10,12 @@ public abstract class MapSection<R extends MapSection<R>> implements ConfigureSe
protected final @NotNull Map<String, Object> data;
protected final @Nullable R parent;
protected MapSection(@NotNull Map<?, ?> data, @Nullable R parent) {
protected MapSection(@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();
if (entry.getValue() instanceof Map) {
@@ -8,22 +8,22 @@ import java.util.Map;
public class MemorySection extends MapSection<MemorySection> {
public static @NotNull MemorySection root(@NotNull ConfigureSource<? extends MemorySection, ?, ?> source) {
return new MemorySection(source, new LinkedHashMap<>(), null);
}
public static @NotNull MemorySection root(@NotNull ConfigureSource<? extends MemorySection, ?, ?> source,
@Nullable Map<?, ?> data) {
return new MemorySection(source, data == null ? new LinkedHashMap<>() : data, null);
@Nullable Map<?, ?> raw) {
return new MemorySection(source, raw == null ? new LinkedHashMap<>() : raw, null);
}
protected final @NotNull ConfigureSource<? extends MemorySection, ?, ?> source;
protected MemorySection(@NotNull ConfigureSource<? extends MemorySection, ?, ?> source,
@NotNull Map<?, ?> data, @Nullable MemorySection parent) {
super(data, parent);
@NotNull Map<?, ?> raw, @Nullable MemorySection parent) {
super(parent);
this.source = source;
migrate(raw);
}
public @NotNull ConfigureSource<? extends MemorySection, ?, ?> source() {
@@ -32,7 +32,7 @@ public class MemorySection extends MapSection<MemorySection> {
@Override
public char pathSeparator() {
return source.pathSeparator();
return source().pathSeparator();
}
@Override
@@ -42,7 +42,7 @@ public class MemorySection extends MapSection<MemorySection> {
@Override
protected @NotNull MemorySection createChild(@NotNull Map<?, ?> data) {
return new MemorySection(source, data, this);
return new MemorySection(source(), data, this);
}
}
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<version>4.0.3</version>
<version>4.0.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<version>4.0.3</version>
<version>4.0.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
+1 -1
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<packaging>pom</packaging>
<version>4.0.3</version>
<version>4.0.4</version>
<modules>
<module>core</module>
<module>features/section</module>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>4.0.3</version>
<version>4.0.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -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.MapSection;
import cc.carm.lib.configuration.source.section.MemorySection;
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<MapSection, Map<String, Object>, JSONSource> {
public class JSONSource extends FileConfigSource<MemorySection, Map<String, Object>, JSONSource> {
public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder()
.serializeNulls().disableHtmlEscaping().setPrettyPrinting()
.registerTypeAdapter(
MapSection.class,
(JsonSerializer<MapSection>) (src, t, c) -> c.serialize(src.data())
MemorySection.class,
(JsonSerializer<MemorySection>) (src, t, c) -> c.serialize(src.data())
).create();
protected final @NotNull Gson gson;
protected @Nullable MapSection rootSection;
protected @Nullable MemorySection rootSection;
protected JSONSource(@NotNull ConfigurationHolder<? extends JSONSource> holder,
@NotNull File file, @Nullable String resourcePath) {
@@ -58,7 +58,7 @@ public class JSONSource extends FileConfigSource<MapSection, Map<String, Object>
}
@Override
public @NotNull MapSection section() {
public @NotNull MemorySection section() {
return Objects.requireNonNull(this.rootSection, "Root section is not initialized");
}
@@ -70,7 +70,7 @@ public class JSONSource extends FileConfigSource<MapSection, Map<String, Object>
@Override
protected void onReload() throws Exception {
Map<?, ?> data = fileReader(reader -> gson.fromJson(reader, LinkedHashMap.class));
this.rootSection = MapSection.root(this, data);
this.rootSection = MemorySection.root(this, data);
this.lastUpdateMillis = System.currentTimeMillis(); // 更新时间
}
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>4.0.3</version>
<version>4.0.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>