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

feat: Simplify the ConfigureSection functions to support more different usages.

This commit is contained in:
2025-02-18 17:08:12 +08:00
parent 1bac201427
commit 00170e6d77
16 changed files with 53 additions and 50 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>4.0.1</version> <version>4.0.2</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
@@ -7,6 +7,7 @@ import cc.carm.lib.configuration.source.loader.ConfigurationInitializer;
import cc.carm.lib.configuration.source.meta.ConfigurationMetaHolder; import cc.carm.lib.configuration.source.meta.ConfigurationMetaHolder;
import cc.carm.lib.configuration.source.meta.ConfigurationMetadata; import cc.carm.lib.configuration.source.meta.ConfigurationMetadata;
import cc.carm.lib.configuration.source.option.ConfigurationOptionHolder; import cc.carm.lib.configuration.source.option.ConfigurationOptionHolder;
import cc.carm.lib.configuration.source.option.StandardOptions;
import cc.carm.lib.configuration.source.section.ConfigureSource; import cc.carm.lib.configuration.source.section.ConfigureSource;
import cc.carm.lib.configuration.value.ValueManifest; import cc.carm.lib.configuration.value.ValueManifest;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
@@ -1,7 +1,6 @@
package cc.carm.lib.configuration.source.section; package cc.carm.lib.configuration.source.section;
import cc.carm.lib.configuration.function.DataFunction; import cc.carm.lib.configuration.function.DataFunction;
import cc.carm.lib.configuration.source.option.StandardOptions;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -14,14 +13,8 @@ import java.util.stream.Stream;
public interface ConfigureSection { public interface ConfigureSection {
@NotNull ConfigureSource<?, ?, ?> source();
@Nullable ConfigureSection parent(); @Nullable ConfigureSection parent();
default char separator() {
return source().holder().options().get(StandardOptions.PATH_SEPARATOR);
}
@NotNull @NotNull
@UnmodifiableView @UnmodifiableView
default Set<String> getKeys(boolean deep) { default Set<String> getKeys(boolean deep) {
@@ -1,6 +1,9 @@
package cc.carm.lib.configuration.source.section; package cc.carm.lib.configuration.source.section;
import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.source.option.StandardOptions;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -24,28 +27,46 @@ public abstract class ConfigureSource<
return holder; return holder;
} }
public void reload() throws Exception { public void reload() throws Exception {
onReload(); // 调用重写的Reload方法 onReload(); // 调用重写的Reload方法
this.lastUpdateMillis = System.currentTimeMillis(); this.lastUpdateMillis = System.currentTimeMillis();
} }
protected abstract SELF self(); @Contract(pure = true)
@ApiStatus.Internal
protected abstract @NotNull SELF self();
/** /**
* @return Original configuration object * @return The original configuration object.
*/ */
@Contract(pure = true)
public abstract @NotNull ORIGINAL original(); public abstract @NotNull ORIGINAL original();
/** /**
* @return The root {@link ConfigureSection} * @return The root {@link ConfigureSection}, which represents the entire configuration.
*/ */
public abstract @NotNull SECTION section(); public abstract @NotNull SECTION section();
/**
* Save the whole configuration.
*
* @throws Exception If any error occurs while saving.
*/
public abstract void save() throws Exception; public abstract void save() throws Exception;
/**
* Reload the configuration.
* <br>This used for implementation, for external usage, use {@link #reload()}
*
* @throws Exception If any error occurs while reloading.
*/
@ApiStatus.OverrideOnly
protected abstract void onReload() throws Exception; protected abstract void onReload() throws Exception;
public char pathSeparator() {
return holder().options().get(StandardOptions.PATH_SEPARATOR);
}
public long getLastUpdateMillis() { public long getLastUpdateMillis() {
return this.lastUpdateMillis; return this.lastUpdateMillis;
} }
@@ -54,14 +75,15 @@ public abstract class ConfigureSource<
return getLastUpdateMillis() > parsedTime; return getLastUpdateMillis() > parsedTime;
} }
/**
* Source also represents the root section, so it has no parent
*
* @return null
*/
@Override @Override
@Contract(pure = true, value = "->null")
public @Nullable ConfigureSection parent() { public @Nullable ConfigureSection parent() {
return null; // Source also represents the root section, so it has no parent return null;
}
@Override
public @NotNull ConfigureSource<?, ?, ?> source() {
return self();
} }
@Override @Override
@@ -79,21 +101,11 @@ public abstract class ConfigureSource<
return section().contains(path); return section().contains(path);
} }
@Override
public boolean isList(@NotNull String path) {
return section().isList(path);
}
@Override @Override
public @Nullable List<?> getList(@NotNull String path) { public @Nullable List<?> getList(@NotNull String path) {
return section().getList(path); return section().getList(path);
} }
@Override
public boolean isSection(@NotNull String path) {
return section().isSection(path);
}
@Override @Override
public @Nullable ConfigureSection getSection(@NotNull String path) { public @Nullable ConfigureSection getSection(@NotNull String path) {
return section().getSection(path); return section().getSection(path);
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>4.0.1</version> <version>4.0.2</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<version>4.0.1</version> <version>4.0.2</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<version>4.0.1</version> <version>4.0.2</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<version>4.0.1</version> <version>4.0.2</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>
@@ -53,7 +53,6 @@ public class MemorySection implements ConfigureSection {
return createChild(new LinkedHashMap<>()); return createChild(new LinkedHashMap<>());
} }
@Override
public @NotNull ConfigureSource<? extends MemorySection, ?, ?> source() { public @NotNull ConfigureSource<? extends MemorySection, ?, ?> source() {
return this.source; return this.source;
} }
@@ -66,6 +65,10 @@ public class MemorySection implements ConfigureSection {
return this.parent; return this.parent;
} }
public char pathSeparator() {
return source.pathSeparator();
}
@Override @Override
public @NotNull Map<String, Object> getValues(boolean deep) { public @NotNull Map<String, Object> getValues(boolean deep) {
return Collections.unmodifiableMap(deep ? mapChildrenValues(this, null, true) : data()); return Collections.unmodifiableMap(deep ? mapChildrenValues(this, null, true) : data());
@@ -109,7 +112,7 @@ public class MemorySection implements ConfigureSection {
} }
private MemorySection getSectionFor(String path) { private MemorySection getSectionFor(String path) {
int index = path.indexOf(separator()); int index = path.indexOf(pathSeparator());
if (index == -1) return this; if (index == -1) return this;
String root = path.substring(0, index); String root = path.substring(0, index);
@@ -123,7 +126,7 @@ public class MemorySection implements ConfigureSection {
} }
private String childPath(String path) { private String childPath(String path) {
int index = path.indexOf(separator()); int index = path.indexOf(pathSeparator());
return (index == -1) ? path : path.substring(index + 1); return (index == -1) ? path : path.substring(index + 1);
} }
@@ -138,7 +141,7 @@ public class MemorySection implements ConfigureSection {
@Nullable String parent, boolean deep) { @Nullable String parent, boolean deep) {
Map<String, Object> output = new LinkedHashMap<>(); Map<String, Object> output = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : section.data().entrySet()) { for (Map.Entry<String, Object> entry : section.data().entrySet()) {
String path = (parent == null ? "" : parent + separator()) + entry.getKey(); String path = (parent == null ? "" : parent + pathSeparator()) + entry.getKey();
output.remove(path); output.remove(path);
output.put(path, entry.getValue()); output.put(path, entry.getValue());
if (deep && entry.getValue() instanceof MemorySection) { if (deep && entry.getValue() instanceof MemorySection) {
+1 -1
View File
@@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<version>4.0.1</version> <version>4.0.2</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<version>4.0.1</version> <version>4.0.2</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>
+1 -1
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>4.0.1</version> <version>4.0.2</version>
<modules> <modules>
<module>core</module> <module>core</module>
<module>features/section</module> <module>features/section</module>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>4.0.1</version> <version>4.0.2</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -48,7 +48,7 @@ public class JSONSource extends FileConfigSource<MemorySection, Map<String, Obje
} }
@Override @Override
protected JSONSource self() { protected @NotNull JSONSource self() {
return this; return this;
} }
+1 -1
View File
@@ -6,7 +6,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>4.0.1</version> <version>4.0.2</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>
@@ -4,7 +4,6 @@ import cc.carm.lib.configuration.commentable.Commentable;
import cc.carm.lib.configuration.commentable.CommentableOptions; import cc.carm.lib.configuration.commentable.CommentableOptions;
import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.source.file.FileConfigSource; import cc.carm.lib.configuration.source.file.FileConfigSource;
import cc.carm.lib.configuration.source.option.StandardOptions;
import cc.carm.lib.configuration.source.section.ConfigureSection; import cc.carm.lib.configuration.source.section.ConfigureSection;
import cc.carm.lib.configuration.source.section.MemorySection; import cc.carm.lib.configuration.source.section.MemorySection;
import cc.carm.lib.yamlcommentupdater.CommentedSection; import cc.carm.lib.yamlcommentupdater.CommentedSection;
@@ -56,7 +55,7 @@ public class YAMLSource
} }
@Override @Override
protected YAMLSource self() { protected @NotNull YAMLSource self() {
return null; return null;
} }
@@ -70,11 +69,6 @@ public class YAMLSource
return Objects.requireNonNull(this.rootSection, "Root section is not initialized."); return Objects.requireNonNull(this.rootSection, "Root section is not initialized.");
} }
@Override
public char separator() {
return holder().options().get(StandardOptions.PATH_SEPARATOR);
}
public @NotNull LoaderOptions loaderOptions() { public @NotNull LoaderOptions loaderOptions() {
return holder().options().get(YAMLOptions.LOADER); return holder().options().get(YAMLOptions.LOADER);
} }
@@ -115,7 +109,7 @@ public class YAMLSource
@Override @Override
public void save() throws Exception { public void save() throws Exception {
CommentedYAMLWriter writer = new CommentedYAMLWriter( CommentedYAMLWriter writer = new CommentedYAMLWriter(
String.valueOf(this.separator()), String.valueOf(this.pathSeparator()),
dumperOptions().getIndent(), dumperOptions().getIndent(),
holder.options().get(CommentableOptions.COMMENT_EMPTY_VALUE) holder.options().get(CommentableOptions.COMMENT_EMPTY_VALUE)
); );