mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +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.meta.ConfigurationMetaHolder;
|
||||
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.StandardOptions;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSource;
|
||||
import cc.carm.lib.configuration.value.ValueManifest;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
@@ -51,6 +51,10 @@ public abstract class ConfigurationHolder<SOURCE extends ConfigureSource<?, ?, S
|
||||
return options;
|
||||
}
|
||||
|
||||
public <O> O option(@NotNull ConfigurationOption<O> option) {
|
||||
return options().get(option);
|
||||
}
|
||||
|
||||
public @NotNull Map<String, ConfigurationMetaHolder> metadata() {
|
||||
return this.metadata;
|
||||
}
|
||||
|
||||
+8
-5
@@ -108,13 +108,13 @@ public class ConfigurationInitializer {
|
||||
public void initialize(@NotNull ConfigurationHolder<?> holder,
|
||||
@NotNull Configuration config) throws Exception {
|
||||
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,
|
||||
@NotNull Class<? extends Configuration> clazz) throws Exception {
|
||||
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);
|
||||
}
|
||||
|
||||
if (holder.options().get(StandardOptions.LOAD_SUB_CLASSES)) {
|
||||
if (holder.option(StandardOptions.LOAD_SUB_CLASSES)) {
|
||||
Class<?>[] classes = clazz.getDeclaredClasses();
|
||||
for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。
|
||||
initializeStaticClass(holder, classes[i], path, null);
|
||||
@@ -175,8 +175,11 @@ public class ConfigurationInitializer {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (holder.options().get(StandardOptions.SET_DEFAULTS)) {
|
||||
value.setDefault();
|
||||
if (holder.option(StandardOptions.SET_DEFAULTS)) {
|
||||
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) {
|
||||
// 当且仅当 源字段与字段 均为Configuration实例时,才对目标字段进行下一步初始化加载。
|
||||
|
||||
@@ -86,7 +86,7 @@ public class PathGenerator {
|
||||
}
|
||||
|
||||
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> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> ConfigurationOption<T> of(@NotNull T defaultValue) {
|
||||
return of((Class<T>) defaultValue.getClass(), defaultValue);
|
||||
}
|
||||
|
||||
public static <T> ConfigurationOption<T> of(@NotNull Class<T> valueClazz, @NotNull T defaultValue) {
|
||||
return new ConfigurationOption<>(valueClazz, defaultValue);
|
||||
return new ConfigurationOption<>(defaultValue);
|
||||
}
|
||||
|
||||
public static <T> ConfigurationOption<T> of(@NotNull Supplier<T> defaultValue) {
|
||||
return of(defaultValue.get());
|
||||
}
|
||||
|
||||
private final @NotNull Class<V> valueClazz;
|
||||
private @NotNull V defaultValue;
|
||||
|
||||
public ConfigurationOption(@NotNull Class<V> valueClazz, @NotNull V defaultValue) {
|
||||
this.valueClazz = valueClazz;
|
||||
public ConfigurationOption(@NotNull V defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Class<V> valueClass() {
|
||||
return this.valueClazz;
|
||||
}
|
||||
|
||||
public @NotNull V defaults() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ public abstract class ConfigValue<T> extends ValueManifest<T> {
|
||||
* @param override Whether to overwrite existing configured value
|
||||
*/
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user