1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 18:48:20 +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>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>4.0.1</version>
<version>4.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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.ConfigurationMetadata;
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.value.ValueManifest;
import org.jetbrains.annotations.Contract;
@@ -1,7 +1,6 @@
package cc.carm.lib.configuration.source.section;
import cc.carm.lib.configuration.function.DataFunction;
import cc.carm.lib.configuration.source.option.StandardOptions;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -14,14 +13,8 @@ import java.util.stream.Stream;
public interface ConfigureSection {
@NotNull ConfigureSource<?, ?, ?> source();
@Nullable ConfigureSection parent();
default char separator() {
return source().holder().options().get(StandardOptions.PATH_SEPARATOR);
}
@NotNull
@UnmodifiableView
default Set<String> getKeys(boolean deep) {
@@ -1,6 +1,9 @@
package cc.carm.lib.configuration.source.section;
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.Nullable;
@@ -24,28 +27,46 @@ public abstract class ConfigureSource<
return holder;
}
public void reload() throws Exception {
onReload(); // 调用重写的Reload方法
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();
/**
* @return The root {@link ConfigureSection}
* @return The root {@link ConfigureSection}, which represents the entire configuration.
*/
public abstract @NotNull SECTION section();
/**
* Save the whole configuration.
*
* @throws Exception If any error occurs while saving.
*/
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;
public char pathSeparator() {
return holder().options().get(StandardOptions.PATH_SEPARATOR);
}
public long getLastUpdateMillis() {
return this.lastUpdateMillis;
}
@@ -54,14 +75,15 @@ public abstract class ConfigureSource<
return getLastUpdateMillis() > parsedTime;
}
/**
* Source also represents the root section, so it has no parent
*
* @return null
*/
@Override
@Contract(pure = true, value = "->null")
public @Nullable ConfigureSection parent() {
return null; // Source also represents the root section, so it has no parent
}
@Override
public @NotNull ConfigureSource<?, ?, ?> source() {
return self();
return null;
}
@Override
@@ -79,21 +101,11 @@ public abstract class ConfigureSource<
return section().contains(path);
}
@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 ConfigureSection getSection(@NotNull String path) {
return section().getSection(path);
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>4.0.1</version>
<version>4.0.2</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.1</version>
<version>4.0.2</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.1</version>
<version>4.0.2</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.1</version>
<version>4.0.2</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
@@ -53,7 +53,6 @@ public class MemorySection implements ConfigureSection {
return createChild(new LinkedHashMap<>());
}
@Override
public @NotNull ConfigureSource<? extends MemorySection, ?, ?> source() {
return this.source;
}
@@ -66,6 +65,10 @@ public class MemorySection implements ConfigureSection {
return this.parent;
}
public char pathSeparator() {
return source.pathSeparator();
}
@Override
public @NotNull Map<String, Object> getValues(boolean deep) {
return Collections.unmodifiableMap(deep ? mapChildrenValues(this, null, true) : data());
@@ -109,7 +112,7 @@ public class MemorySection implements ConfigureSection {
}
private MemorySection getSectionFor(String path) {
int index = path.indexOf(separator());
int index = path.indexOf(pathSeparator());
if (index == -1) return this;
String root = path.substring(0, index);
@@ -123,7 +126,7 @@ public class MemorySection implements ConfigureSection {
}
private String childPath(String path) {
int index = path.indexOf(separator());
int index = path.indexOf(pathSeparator());
return (index == -1) ? path : path.substring(index + 1);
}
@@ -138,7 +141,7 @@ public class MemorySection implements ConfigureSection {
@Nullable String parent, boolean deep) {
Map<String, Object> output = new LinkedHashMap<>();
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.put(path, entry.getValue());
if (deep && entry.getValue() instanceof MemorySection) {
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<version>4.0.1</version>
<version>4.0.2</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.1</version>
<version>4.0.2</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.1</version>
<version>4.0.2</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.1</version>
<version>4.0.2</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -48,7 +48,7 @@ public class JSONSource extends FileConfigSource<MemorySection, Map<String, Obje
}
@Override
protected JSONSource self() {
protected @NotNull JSONSource self() {
return this;
}
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>4.0.1</version>
<version>4.0.2</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
@@ -4,7 +4,6 @@ import cc.carm.lib.configuration.commentable.Commentable;
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.option.StandardOptions;
import cc.carm.lib.configuration.source.section.ConfigureSection;
import cc.carm.lib.configuration.source.section.MemorySection;
import cc.carm.lib.yamlcommentupdater.CommentedSection;
@@ -56,7 +55,7 @@ public class YAMLSource
}
@Override
protected YAMLSource self() {
protected @NotNull YAMLSource self() {
return null;
}
@@ -70,11 +69,6 @@ public class YAMLSource
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() {
return holder().options().get(YAMLOptions.LOADER);
}
@@ -115,7 +109,7 @@ public class YAMLSource
@Override
public void save() throws Exception {
CommentedYAMLWriter writer = new CommentedYAMLWriter(
String.valueOf(this.separator()),
String.valueOf(this.pathSeparator()),
dumperOptions().getIndent(),
holder.options().get(CommentableOptions.COMMENT_EMPTY_VALUE)
);