mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
feat: Support standard "pre-load" option
This commit is contained in:
@@ -6,8 +6,8 @@ import cc.carm.lib.configuration.adapter.ValueType;
|
|||||||
import cc.carm.lib.configuration.source.loader.ConfigurationInitializer;
|
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.ConfigurationOption;
|
||||||
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;
|
||||||
@@ -51,6 +51,10 @@ public abstract class ConfigurationHolder<SOURCE extends ConfigureSource<?, ?, S
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <O> O option(@NotNull ConfigurationOption<O> option) {
|
||||||
|
return options().get(option);
|
||||||
|
}
|
||||||
|
|
||||||
public @NotNull Map<String, ConfigurationMetaHolder> metadata() {
|
public @NotNull Map<String, ConfigurationMetaHolder> metadata() {
|
||||||
return this.metadata;
|
return this.metadata;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-5
@@ -108,13 +108,13 @@ public class ConfigurationInitializer {
|
|||||||
public void initialize(@NotNull ConfigurationHolder<?> holder,
|
public void initialize(@NotNull ConfigurationHolder<?> holder,
|
||||||
@NotNull Configuration config) throws Exception {
|
@NotNull Configuration config) throws Exception {
|
||||||
initializeInstance(holder, config, null, null);
|
initializeInstance(holder, config, null, null);
|
||||||
if (holder.options().get(StandardOptions.SET_DEFAULTS)) holder.save();
|
if (holder.option(StandardOptions.SET_DEFAULTS)) holder.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(@NotNull ConfigurationHolder<?> holder,
|
public void initialize(@NotNull ConfigurationHolder<?> holder,
|
||||||
@NotNull Class<? extends Configuration> clazz) throws Exception {
|
@NotNull Class<? extends Configuration> clazz) throws Exception {
|
||||||
initializeStaticClass(holder, clazz, null, null);
|
initializeStaticClass(holder, clazz, null, null);
|
||||||
if (holder.options().get(StandardOptions.SET_DEFAULTS)) holder.save();
|
if (holder.option(StandardOptions.SET_DEFAULTS)) holder.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -149,7 +149,7 @@ public class ConfigurationInitializer {
|
|||||||
initializeField(holder, clazz, field, path);
|
initializeField(holder, clazz, field, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (holder.options().get(StandardOptions.LOAD_SUB_CLASSES)) {
|
if (holder.option(StandardOptions.LOAD_SUB_CLASSES)) {
|
||||||
Class<?>[] classes = clazz.getDeclaredClasses();
|
Class<?>[] classes = clazz.getDeclaredClasses();
|
||||||
for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。
|
for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。
|
||||||
initializeStaticClass(holder, classes[i], path, null);
|
initializeStaticClass(holder, classes[i], path, null);
|
||||||
@@ -175,8 +175,11 @@ public class ConfigurationInitializer {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (holder.options().get(StandardOptions.SET_DEFAULTS)) {
|
if (holder.option(StandardOptions.SET_DEFAULTS)) {
|
||||||
value.setDefault();
|
value.setDefault(); // Set default value.
|
||||||
|
}
|
||||||
|
if (holder.option(StandardOptions.PRELOAD)) {
|
||||||
|
value.get(); // Preload the value by calling #get method.
|
||||||
}
|
}
|
||||||
} else if (source instanceof Configuration && object instanceof Configuration) {
|
} else if (source instanceof Configuration && object instanceof Configuration) {
|
||||||
// 当且仅当 源字段与字段 均为Configuration实例时,才对目标字段进行下一步初始化加载。
|
// 当且仅当 源字段与字段 均为Configuration实例时,才对目标字段进行下一步初始化加载。
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ public class PathGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static char pathSeparator(ConfigurationHolder<?> holder) {
|
public static char pathSeparator(ConfigurationHolder<?> holder) {
|
||||||
return holder.options().get(StandardOptions.PATH_SEPARATOR);
|
return holder.option(StandardOptions.PATH_SEPARATOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+2
-14
@@ -6,32 +6,20 @@ import java.util.function.Supplier;
|
|||||||
|
|
||||||
public class ConfigurationOption<V> {
|
public class ConfigurationOption<V> {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <T> ConfigurationOption<T> of(@NotNull T defaultValue) {
|
public static <T> ConfigurationOption<T> of(@NotNull T defaultValue) {
|
||||||
return of((Class<T>) defaultValue.getClass(), defaultValue);
|
return new ConfigurationOption<>(defaultValue);
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ConfigurationOption<T> of(@NotNull Class<T> valueClazz, @NotNull T defaultValue) {
|
|
||||||
return new ConfigurationOption<>(valueClazz, defaultValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> ConfigurationOption<T> of(@NotNull Supplier<T> defaultValue) {
|
public static <T> ConfigurationOption<T> of(@NotNull Supplier<T> defaultValue) {
|
||||||
return of(defaultValue.get());
|
return of(defaultValue.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
private final @NotNull Class<V> valueClazz;
|
|
||||||
private @NotNull V defaultValue;
|
private @NotNull V defaultValue;
|
||||||
|
|
||||||
public ConfigurationOption(@NotNull Class<V> valueClazz, @NotNull V defaultValue) {
|
public ConfigurationOption(@NotNull V defaultValue) {
|
||||||
this.valueClazz = valueClazz;
|
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public Class<V> valueClass() {
|
|
||||||
return this.valueClazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull V defaults() {
|
public @NotNull V defaults() {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ public abstract class ConfigValue<T> extends ValueManifest<T> {
|
|||||||
* @param override Whether to overwrite existing configured value
|
* @param override Whether to overwrite existing configured value
|
||||||
*/
|
*/
|
||||||
public void setDefault(boolean override) {
|
public void setDefault(boolean override) {
|
||||||
if (!override && config().contains(path())) return;
|
if (config().contains(path()) && !override) return; // Skip if the value is already set
|
||||||
set(defaults()); // Set the default value
|
set(defaults()); // Set the default value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ public class Commentable {
|
|||||||
String comment = getInlineComment(holder, path, null);
|
String comment = getInlineComment(holder, path, null);
|
||||||
if (comment != null) return comment;
|
if (comment != null) return comment;
|
||||||
|
|
||||||
String sep = String.valueOf(holder.options().get(StandardOptions.PATH_SEPARATOR));
|
String sep = String.valueOf(holder.option(StandardOptions.PATH_SEPARATOR));
|
||||||
|
|
||||||
// If the comment is not found, try to get the comment from the parent section
|
// If the comment is not found, try to get the comment from the parent section
|
||||||
String[] keys = path.split(Pattern.quote(sep));
|
String[] keys = path.split(Pattern.quote(sep));
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ public class YAMLSource
|
|||||||
CommentedYAMLWriter writer = new CommentedYAMLWriter(
|
CommentedYAMLWriter writer = new CommentedYAMLWriter(
|
||||||
String.valueOf(this.pathSeparator()),
|
String.valueOf(this.pathSeparator()),
|
||||||
dumperOptions().getIndent(),
|
dumperOptions().getIndent(),
|
||||||
holder.options().get(CommentableOptions.COMMENT_EMPTY_VALUE)
|
holder.option(CommentableOptions.COMMENT_EMPTY_VALUE)
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
fileWriter(w -> w.write(writer.saveToString(this)));
|
fileWriter(w -> w.write(writer.saveToString(this)));
|
||||||
|
|||||||
Reference in New Issue
Block a user