mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0bda97d82a | |||
| a13ea7569c | |||
| c9f488c932 | |||
| 72584f66ac | |||
| 05e055a6f1 | |||
| e52195a8bb |
@@ -72,7 +72,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
cd docs
|
cd docs
|
||||||
git init
|
git init
|
||||||
git remote add origin git@github.com:CarmJos/EasySQL.git
|
git remote add origin git@github.com:CarmJos/EasyConfiguration.git
|
||||||
git checkout -b gh-pages
|
git checkout -b gh-pages
|
||||||
git add -A
|
git add -A
|
||||||
git commit -m "API Document generated."
|
git commit -m "API Document generated."
|
||||||
|
|||||||
@@ -97,6 +97,9 @@
|
|||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>平台依赖版本</summary>
|
||||||
|
|
||||||
**基于Spigot实现的版本**
|
**基于Spigot实现的版本**
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
|
|||||||
+1
-1
@@ -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>1.0.3</version>
|
<version>1.1.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -11,26 +11,40 @@ import java.lang.reflect.Field;
|
|||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class ConfigInitializer {
|
public class ConfigInitializer<T extends ConfigurationProvider<?>> {
|
||||||
|
|
||||||
public static void initialize(ConfigurationProvider source, Class<? extends ConfigurationRoot> rootClazz) {
|
protected final @NotNull T provider;
|
||||||
initialize(source, rootClazz, true);
|
|
||||||
|
public ConfigInitializer(@NotNull T provider) {
|
||||||
|
this.provider = provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void initialize(ConfigurationProvider provider, Class<? extends ConfigurationRoot> rootClazz, boolean saveDefault) {
|
public void initialize(Class<? extends ConfigurationRoot> rootClazz) {
|
||||||
ConfigPath sectionAnnotation = rootClazz.getAnnotation(ConfigPath.class);
|
initialize(rootClazz, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initialize(Class<? extends ConfigurationRoot> rootClazz, boolean saveDefault) {
|
||||||
String rootSection = null;
|
String rootSection = null;
|
||||||
|
|
||||||
|
ConfigPath sectionAnnotation = rootClazz.getAnnotation(ConfigPath.class);
|
||||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
||||||
rootSection = sectionAnnotation.value();
|
rootSection = sectionAnnotation.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
if (rootSection != null) {
|
||||||
initSection(provider, rootSection, innerClass, saveDefault);
|
//Not usable for null section.
|
||||||
|
ConfigComment comments = rootClazz.getAnnotation(ConfigComment.class);
|
||||||
|
if (comments != null && comments.value().length > 0) {
|
||||||
|
provider.setComments(rootSection, comments.value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Field field : rootClazz.getFields()) {
|
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
||||||
initValue(provider, rootSection, rootClazz, field, saveDefault);
|
initSection(rootSection, innerClass, saveDefault);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Field field : rootClazz.getDeclaredFields()) {
|
||||||
|
initValue(rootSection, rootClazz, field, saveDefault);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (saveDefault) {
|
if (saveDefault) {
|
||||||
@@ -43,7 +57,7 @@ public class ConfigInitializer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initSection(ConfigurationProvider provider, String parentSection, Class<?> clazz, boolean saveDefault) {
|
private void initSection(String parentSection, Class<?> clazz, boolean saveDefault) {
|
||||||
if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return;
|
if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return;
|
||||||
|
|
||||||
String section = getSectionPath(clazz.getSimpleName(), parentSection, clazz.getAnnotation(ConfigPath.class));
|
String section = getSectionPath(clazz.getSimpleName(), parentSection, clazz.getAnnotation(ConfigPath.class));
|
||||||
@@ -52,29 +66,30 @@ public class ConfigInitializer {
|
|||||||
provider.setComments(parentSection, comments.value());
|
provider.setComments(parentSection, comments.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Field field : clazz.getFields()) initValue(provider, section, clazz, field, saveDefault);
|
for (Field field : clazz.getDeclaredFields()) initValue(section, clazz, field, saveDefault);
|
||||||
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(provider, section, innerClass, saveDefault);
|
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(section, innerClass, saveDefault);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initValue(ConfigurationProvider provider, String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
|
private void initValue(String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
|
||||||
try {
|
try {
|
||||||
|
field.setAccessible(true);
|
||||||
Object object = field.get(clazz);
|
Object object = field.get(clazz);
|
||||||
if (object instanceof ConfigValue<?>) {
|
if (object instanceof ConfigValue<?>) {
|
||||||
initializeValue(
|
initializeValue(
|
||||||
provider, (ConfigValue<?>) object,
|
(ConfigValue<?>) object, saveDefault,
|
||||||
getSectionPath(field.getName(), parentSection, field.getAnnotation(ConfigPath.class)),
|
getSectionPath(field.getName(), parentSection, field.getAnnotation(ConfigPath.class)),
|
||||||
Optional.ofNullable(field.getAnnotation(ConfigComment.class))
|
Optional.ofNullable(field.getAnnotation(ConfigComment.class))
|
||||||
.map(ConfigComment::value).orElse(new String[0]),
|
.map(ConfigComment::value).orElse(new String[0])
|
||||||
saveDefault);
|
);
|
||||||
}
|
}
|
||||||
} catch (IllegalAccessException ignored) {
|
} catch (IllegalAccessException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void initializeValue(@NotNull ConfigurationProvider provider, @NotNull ConfigValue<?> value,
|
public void initializeValue(@NotNull ConfigValue<?> value, boolean saveDefault,
|
||||||
@NotNull String path, @NotNull String[] comments, boolean saveDefault) {
|
@NotNull String path, @NotNull String[] comments) {
|
||||||
value.initialize(provider, path, comments);
|
value.initialize(provider, path, comments);
|
||||||
if (saveDefault && value.getDefaultValue() != null && !provider.getConfiguration().contains(path)) {
|
if (saveDefault && value.getDefaultValue() != null && !provider.getConfiguration().contains(path)) {
|
||||||
value.setDefault();
|
value.setDefault();
|
||||||
@@ -84,16 +99,31 @@ public class ConfigInitializer {
|
|||||||
public static String getSectionPath(@NotNull String name,
|
public static String getSectionPath(@NotNull String name,
|
||||||
@Nullable String parentSection,
|
@Nullable String parentSection,
|
||||||
@Nullable ConfigPath pathAnnotation) {
|
@Nullable ConfigPath pathAnnotation) {
|
||||||
String parent = parentSection != null ? parentSection + "." : "";
|
@NotNull String parent = parentSection != null ? parentSection + "." : "";
|
||||||
if (pathAnnotation != null && pathAnnotation.value().length() > 0) {
|
@NotNull String path = getSectionName(name);
|
||||||
return parent + pathAnnotation.value();
|
boolean root = false;
|
||||||
} else {
|
if (pathAnnotation != null) {
|
||||||
return parent + getSectionName(name);
|
if (pathAnnotation.value().length() > 0) path = pathAnnotation.value();
|
||||||
|
root = pathAnnotation.root();
|
||||||
}
|
}
|
||||||
|
return (root ? "" : parent) + path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getSectionName(String codeName) {
|
/**
|
||||||
return codeName.toLowerCase().replace("_", "-");
|
* 得到指定元素的配置名称。
|
||||||
|
* 采用 全小写,以“-”链接 的命名规则。
|
||||||
|
*
|
||||||
|
* @param name 源名称
|
||||||
|
* @return 全小写,以“-”链接 的 路径名称
|
||||||
|
*/
|
||||||
|
public static String getSectionName(String name) {
|
||||||
|
return name.replaceAll("[A-Z]", "-$0") // 将驼峰转换为蛇形;
|
||||||
|
.replaceAll("-(.*)", "$1") // 若首字母也为大写,则也会被转换,需要去掉第一个横线
|
||||||
|
.replaceAll("_-([A-Z])", "_$1") // 因为命名中可能包含 _,因此需要被特殊处理一下
|
||||||
|
.replaceAll("([a-z])-([A-Z])", "$1_$2") // 然后将非全大写命名的内容进行转换
|
||||||
|
.replace("-", "") // 移除掉多余的横线
|
||||||
|
.replace("_", "-") // 将下划线替换为横线
|
||||||
|
.toLowerCase(); // 最后转为全小写
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,34 @@
|
|||||||
package cc.carm.lib.configuration.core.annotation;
|
package cc.carm.lib.configuration.core.annotation;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于标记对应类或参数的配置路径
|
||||||
|
*/
|
||||||
@Target({ElementType.TYPE, ElementType.FIELD})
|
@Target({ElementType.TYPE, ElementType.FIELD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface ConfigPath {
|
public @interface ConfigPath {
|
||||||
|
|
||||||
String value();
|
/**
|
||||||
|
* 指定路径的值。
|
||||||
|
* 若不指定,则会通过 {@link ConfigInitializer#getSectionName(String)}自动生成当前路径的值。
|
||||||
|
*
|
||||||
|
* @return 路径的值
|
||||||
|
*/
|
||||||
|
String value() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否从根节点开始。
|
||||||
|
* <br>若为 false,则会自动添加上一个路径(如果有)到本节点的路径。
|
||||||
|
* <br>若为 true,则会从根节点开始直接设置本路径。
|
||||||
|
*
|
||||||
|
* @return 是否不继承上一路径,直接从根路径为开始
|
||||||
|
*/
|
||||||
|
boolean root() default false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-3
@@ -5,20 +5,25 @@ import cc.carm.lib.configuration.core.value.ConfigValue;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public abstract class AbstractConfigBuilder<B extends AbstractConfigBuilder<B, T>, T> {
|
public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T, B, P>, P extends ConfigurationProvider<?>> {
|
||||||
|
|
||||||
|
protected final Class<? super P> providerClass;
|
||||||
|
|
||||||
protected @Nullable ConfigurationProvider provider;
|
protected @Nullable P provider;
|
||||||
protected @Nullable String path;
|
protected @Nullable String path;
|
||||||
|
|
||||||
protected @NotNull String[] comments = new String[0];
|
protected @NotNull String[] comments = new String[0];
|
||||||
protected @Nullable T defaultValue;
|
protected @Nullable T defaultValue;
|
||||||
|
|
||||||
|
public AbstractConfigBuilder(Class<? super P> providerClass) {
|
||||||
|
this.providerClass = providerClass;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract @NotNull B getThis();
|
protected abstract @NotNull B getThis();
|
||||||
|
|
||||||
public abstract @NotNull ConfigValue<?> build();
|
public abstract @NotNull ConfigValue<?> build();
|
||||||
|
|
||||||
public @NotNull B from(@Nullable ConfigurationProvider provider) {
|
public @NotNull B from(@Nullable P provider) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
return getThis();
|
return getThis();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package cc.carm.lib.configuration.core.builder;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public abstract class CommonConfigBuilder<T, B extends CommonConfigBuilder<T, B>>
|
||||||
|
extends AbstractConfigBuilder<T, B, ConfigurationProvider<?>> {
|
||||||
|
|
||||||
|
protected @Nullable ConfigurationProvider<?> provider;
|
||||||
|
protected @Nullable String path;
|
||||||
|
|
||||||
|
protected @NotNull String[] comments = new String[0];
|
||||||
|
protected @Nullable T defaultValue;
|
||||||
|
|
||||||
|
public CommonConfigBuilder() {
|
||||||
|
super(ConfigurationProvider.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract @NotNull B getThis();
|
||||||
|
|
||||||
|
public abstract @NotNull ConfigValue<?> build();
|
||||||
|
|
||||||
|
public @NotNull B from(@Nullable ConfigurationProvider<?> provider) {
|
||||||
|
this.provider = provider;
|
||||||
|
return getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull B path(@Nullable String path) {
|
||||||
|
this.path = path;
|
||||||
|
return getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull B comments(@NotNull String... comments) {
|
||||||
|
this.comments = comments;
|
||||||
|
return getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull B defaults(@Nullable T defaultValue) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
return getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+2
-3
@@ -1,14 +1,13 @@
|
|||||||
package cc.carm.lib.configuration.core.builder.list;
|
package cc.carm.lib.configuration.core.builder.list;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SourceListBuilder<S, V>
|
public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> {
|
||||||
extends AbstractConfigBuilder<SourceListBuilder<S, V>, List<V>> {
|
|
||||||
|
|
||||||
protected final @NotNull Class<S> sourceClass;
|
protected final @NotNull Class<S> sourceClass;
|
||||||
protected @NotNull ConfigDataFunction<Object, S> sourceParser;
|
protected @NotNull ConfigDataFunction<Object, S> sourceParser;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package cc.carm.lib.configuration.core.builder.map;
|
package cc.carm.lib.configuration.core.builder.map;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -8,8 +8,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class SourceMapBuilder<M extends Map<K, V>, S, K, V>
|
public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfigBuilder<M, SourceMapBuilder<M, S, K, V>> {
|
||||||
extends AbstractConfigBuilder<SourceMapBuilder<M, S, K, V>, M> {
|
|
||||||
|
|
||||||
protected final @NotNull Supplier<@NotNull M> supplier;
|
protected final @NotNull Supplier<@NotNull M> supplier;
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
package cc.carm.lib.configuration.core.builder.value;
|
package cc.carm.lib.configuration.core.builder.value;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
@@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class SectionValueBuilder<V>
|
public class SectionValueBuilder<V>
|
||||||
extends AbstractConfigBuilder<SectionValueBuilder<V>, V> {
|
extends CommonConfigBuilder<V, SectionValueBuilder<V>> {
|
||||||
|
|
||||||
|
|
||||||
protected final @NotNull Class<V> valueClass;
|
protected final @NotNull Class<V> valueClass;
|
||||||
|
|||||||
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
package cc.carm.lib.configuration.core.builder.value;
|
package cc.carm.lib.configuration.core.builder.value;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class SourceValueBuilder<S, V> extends AbstractConfigBuilder<SourceValueBuilder<S, V>, V> {
|
public class SourceValueBuilder<S, V> extends CommonConfigBuilder<V, SourceValueBuilder<S, V>> {
|
||||||
|
|
||||||
protected final @NotNull Class<S> sourceClass;
|
protected final @NotNull Class<S> sourceClass;
|
||||||
protected @NotNull ConfigDataFunction<Object, S> sourceParser;
|
protected @NotNull ConfigDataFunction<Object, S> sourceParser;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import cc.carm.lib.configuration.core.ConfigurationRoot;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public abstract class ConfigurationProvider {
|
public abstract class ConfigurationProvider<W extends ConfigurationWrapper> {
|
||||||
|
|
||||||
protected long updateTime;
|
protected long updateTime;
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ public abstract class ConfigurationProvider {
|
|||||||
return this.updateTime > time;
|
return this.updateTime > time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract @NotNull ConfigurationWrapper getConfiguration();
|
public abstract @NotNull W getConfiguration();
|
||||||
|
|
||||||
public abstract void reload() throws Exception;
|
public abstract void reload() throws Exception;
|
||||||
|
|
||||||
@@ -31,8 +31,10 @@ public abstract class ConfigurationProvider {
|
|||||||
|
|
||||||
public abstract @Nullable String[] getComments(@NotNull String path);
|
public abstract @Nullable String[] getComments(@NotNull String path);
|
||||||
|
|
||||||
|
public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();
|
||||||
|
|
||||||
public void initialize(Class<? extends ConfigurationRoot> configClazz) {
|
public void initialize(Class<? extends ConfigurationRoot> configClazz) {
|
||||||
ConfigInitializer.initialize(this, configClazz, true);
|
getInitializer().initialize(configClazz, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public interface ConfigurationWrapper extends ConfigurationReader{
|
public interface ConfigurationWrapper extends ConfigurationReader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default ConfigurationWrapper getWrapper() {
|
default ConfigurationWrapper getWrapper() {
|
||||||
@@ -68,5 +68,4 @@ public interface ConfigurationWrapper extends ConfigurationReader{
|
|||||||
@Nullable
|
@Nullable
|
||||||
ConfigurationWrapper getConfigurationSection(@NotNull String path);
|
ConfigurationWrapper getConfigurationSection(@NotNull String path);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-5
@@ -1,6 +1,7 @@
|
|||||||
package cc.carm.lib.configuration.core.source.impl;
|
package cc.carm.lib.configuration.core.source.impl;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ import java.net.URL;
|
|||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public abstract class FileConfigProvider extends ConfigurationProvider {
|
public abstract class FileConfigProvider<W extends ConfigurationWrapper> extends ConfigurationProvider<W> {
|
||||||
|
|
||||||
protected final @NotNull File file;
|
protected final @NotNull File file;
|
||||||
|
|
||||||
@@ -22,19 +23,24 @@ public abstract class FileConfigProvider extends ConfigurationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void initializeFile(@Nullable String sourcePath) throws IOException {
|
public void initializeFile(@Nullable String sourcePath) throws IOException {
|
||||||
if (getFile().exists()) return;
|
if (this.file.exists()) return;
|
||||||
if (!getFile().getParentFile().exists() && !getFile().getParentFile().mkdirs()) {
|
|
||||||
|
File parent = this.file.getParentFile();
|
||||||
|
if (parent != null && !parent.exists() && !parent.mkdirs()) {
|
||||||
throw new IOException("Failed to create directory " + file.getParentFile().getAbsolutePath());
|
throw new IOException("Failed to create directory " + file.getParentFile().getAbsolutePath());
|
||||||
}
|
}
|
||||||
if (!getFile().createNewFile()) {
|
|
||||||
|
if (!this.file.createNewFile()) {
|
||||||
throw new IOException("Failed to create file " + file.getAbsolutePath());
|
throw new IOException("Failed to create file " + file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
if (sourcePath == null) return;
|
|
||||||
|
if (sourcePath != null) {
|
||||||
try {
|
try {
|
||||||
saveResource(sourcePath, true);
|
saveResource(sourcePath, true);
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void saveResource(@NotNull String resourcePath, boolean replace)
|
public void saveResource(@NotNull String resourcePath, boolean replace)
|
||||||
throws NullPointerException, IOException, IllegalArgumentException {
|
throws NullPointerException, IOException, IllegalArgumentException {
|
||||||
|
|||||||
@@ -15,23 +15,21 @@ public abstract class ConfigValue<T> {
|
|||||||
return new ConfigBuilder();
|
return new ConfigBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected @Nullable T defaultValue;
|
protected @Nullable ConfigurationProvider<?> provider;
|
||||||
|
|
||||||
protected @Nullable ConfigurationProvider provider;
|
|
||||||
protected @Nullable String configPath;
|
protected @Nullable String configPath;
|
||||||
protected @NotNull String[] comments;
|
protected @NotNull String[] comments;
|
||||||
|
|
||||||
public ConfigValue(@Nullable ConfigurationProvider provider, @Nullable String configPath,
|
protected @Nullable T defaultValue;
|
||||||
@NotNull String[] comments, @Nullable T defaultValue) {
|
|
||||||
|
public ConfigValue(@Nullable ConfigurationProvider<?> provider,
|
||||||
|
@Nullable String configPath, @NotNull String[] comments, @Nullable T defaultValue) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.configPath = configPath;
|
this.configPath = configPath;
|
||||||
this.comments = comments;
|
this.comments = comments;
|
||||||
|
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(@NotNull ConfigurationProvider provider, @NotNull String configPath,
|
public void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath, @NotNull String... comments) {
|
||||||
@NotNull String... comments) {
|
|
||||||
if (this.provider == null) this.provider = provider;
|
if (this.provider == null) this.provider = provider;
|
||||||
if (this.configPath == null) this.configPath = configPath;
|
if (this.configPath == null) this.configPath = configPath;
|
||||||
if (this.comments.length == 0) this.comments = comments;
|
if (this.comments.length == 0) this.comments = comments;
|
||||||
@@ -68,7 +66,15 @@ public abstract class ConfigValue<T> {
|
|||||||
Optional.ofNullable(getDefaultValue()).ifPresent(this::set);
|
Optional.ofNullable(getDefaultValue()).ifPresent(this::set);
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull ConfigurationProvider getProvider() {
|
public boolean isDefault() {
|
||||||
|
T defaultValue = getDefaultValue();
|
||||||
|
T value = get();
|
||||||
|
if (defaultValue == null && value == null) return true;
|
||||||
|
else if (defaultValue != null && value != null) return defaultValue.equals(value);
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ConfigurationProvider<?> getProvider() {
|
||||||
return Optional.ofNullable(this.provider)
|
return Optional.ofNullable(this.provider)
|
||||||
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
|
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -1,6 +1,7 @@
|
|||||||
package cc.carm.lib.configuration.core.value;
|
package cc.carm.lib.configuration.core.value.impl;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -10,7 +11,7 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
|
|||||||
protected @Nullable T cachedValue;
|
protected @Nullable T cachedValue;
|
||||||
protected long parsedTime = -1;
|
protected long parsedTime = -1;
|
||||||
|
|
||||||
public CachedConfigValue(@Nullable ConfigurationProvider provider, @Nullable String sectionPath,
|
public CachedConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||||
@NotNull String[] comments, @Nullable T defaultValue) {
|
@NotNull String[] comments, @Nullable T defaultValue) {
|
||||||
super(provider, sectionPath, comments, defaultValue);
|
super(provider, sectionPath, comments, defaultValue);
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
package cc.carm.lib.configuration.core.value.type;
|
package cc.carm.lib.configuration.core.value.type;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
|
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -23,7 +21,7 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
|
|||||||
protected final @NotNull ConfigDataFunction<Object, V> parser;
|
protected final @NotNull ConfigDataFunction<Object, V> parser;
|
||||||
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
||||||
|
|
||||||
public ConfiguredList(@Nullable ConfigurationProvider provider,
|
public ConfiguredList(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @NotNull String[] comments,
|
||||||
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
|
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
|
||||||
@NotNull ConfigDataFunction<Object, V> parser,
|
@NotNull ConfigDataFunction<Object, V> parser,
|
||||||
@@ -36,6 +34,9 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull List<V> get() {
|
public @NotNull List<V> get() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||||
List<V> list = new ArrayList<>();
|
List<V> list = new ArrayList<>();
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
package cc.carm.lib.configuration.core.value.type;
|
package cc.carm.lib.configuration.core.value.type;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
|
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import cc.carm.lib.configuration.core.value.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -32,7 +31,7 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
|
|||||||
protected final @NotNull ConfigDataFunction<K, String> keySerializer;
|
protected final @NotNull ConfigDataFunction<K, String> keySerializer;
|
||||||
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
|
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
|
||||||
|
|
||||||
public ConfiguredMap(@Nullable ConfigurationProvider provider,
|
public ConfiguredMap(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @NotNull String[] comments,
|
||||||
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier,
|
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier,
|
||||||
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
package cc.carm.lib.configuration.core.value.type;
|
package cc.carm.lib.configuration.core.value.type;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder;
|
import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import cc.carm.lib.configuration.core.value.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -24,7 +23,7 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
|
|||||||
protected final @NotNull ConfigValueParser<ConfigurationWrapper, V> parser;
|
protected final @NotNull ConfigValueParser<ConfigurationWrapper, V> parser;
|
||||||
protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
|
protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
|
||||||
|
|
||||||
public ConfiguredSection(@Nullable ConfigurationProvider provider,
|
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @NotNull String[] comments,
|
||||||
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
||||||
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
|
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
package cc.carm.lib.configuration.core.value.type;
|
package cc.carm.lib.configuration.core.value.type;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
|
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
import cc.carm.lib.configuration.core.value.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
|
|||||||
protected final @NotNull ConfigValueParser<Object, V> parser;
|
protected final @NotNull ConfigValueParser<Object, V> parser;
|
||||||
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
protected final @NotNull ConfigDataFunction<V, Object> serializer;
|
||||||
|
|
||||||
public ConfiguredValue(@Nullable ConfigurationProvider provider,
|
public ConfiguredValue(@Nullable ConfigurationProvider<?> provider,
|
||||||
@Nullable String sectionPath, @NotNull String[] comments,
|
@Nullable String sectionPath, @NotNull String[] comments,
|
||||||
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
@NotNull Class<V> valueClass, @Nullable V defaultValue,
|
||||||
@NotNull ConfigValueParser<Object, V> parser,
|
@NotNull ConfigValueParser<Object, V> parser,
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class NameTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onTest() {
|
||||||
|
|
||||||
|
System.out.println(ConfigInitializer.getSectionName("LoveGames")); // -> love-games
|
||||||
|
System.out.println(ConfigInitializer.getSectionName("EASY_GAME")); // -> easy-game
|
||||||
|
System.out.println(ConfigInitializer.getSectionName("F")); //-? f
|
||||||
|
System.out.println(ConfigInitializer.getSectionName("Test123123")); // -? test123123123
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -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>1.0.3</version>
|
<version>1.1.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package cc.carm.lib.configuration.yaml;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
|
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
||||||
|
import cc.carm.lib.configuration.yaml.YamlSectionWrapper;
|
||||||
|
import cc.carm.lib.configuration.yaml.builder.YamlConfigBuilder;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public abstract class YAMLValue<T> extends CachedConfigValue<T> {
|
||||||
|
|
||||||
|
public static @NotNull YamlConfigBuilder builder() {
|
||||||
|
return new YamlConfigBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public YAMLValue(@Nullable YamlConfigProvider provider,
|
||||||
|
@Nullable String configPath, @NotNull String[] comments, @Nullable T defaultValue) {
|
||||||
|
super(provider, configPath, comments, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public YamlConfigProvider getYAMLProvider() {
|
||||||
|
ConfigurationProvider<?> provider = getProvider();
|
||||||
|
if (provider instanceof YamlConfigProvider) return (YamlConfigProvider) getProvider();
|
||||||
|
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
||||||
|
}
|
||||||
|
|
||||||
|
public YamlSectionWrapper getYAMLConfig() {
|
||||||
|
return getYAMLProvider().getConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package cc.carm.lib.configuration.yaml;
|
package cc.carm.lib.configuration.yaml;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||||
import org.bspfsystems.yamlconfiguration.commented.CommentedYamlConfiguration;
|
import org.bspfsystems.yamlconfiguration.commented.CommentedYamlConfiguration;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -8,10 +8,11 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public class YamlConfigProvider extends FileConfigProvider {
|
public class YamlConfigProvider extends FileConfigProvider<YamlSectionWrapper> {
|
||||||
|
|
||||||
YamlComments comments = new YamlComments();
|
protected final @NotNull YamlComments comments = new YamlComments();
|
||||||
CommentedYamlConfiguration configuration;
|
protected CommentedYamlConfiguration configuration;
|
||||||
|
protected ConfigInitializer<YamlConfigProvider> initializer;
|
||||||
|
|
||||||
public YamlConfigProvider(@NotNull File file) {
|
public YamlConfigProvider(@NotNull File file) {
|
||||||
super(file);
|
super(file);
|
||||||
@@ -19,10 +20,11 @@ public class YamlConfigProvider extends FileConfigProvider {
|
|||||||
|
|
||||||
public void initializeConfig() {
|
public void initializeConfig() {
|
||||||
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
||||||
|
this.initializer = new ConfigInitializer<>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ConfigurationWrapper getConfiguration() {
|
public @NotNull YamlSectionWrapper getConfiguration() {
|
||||||
return YamlSectionWrapper.of(configuration);
|
return YamlSectionWrapper.of(configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,4 +48,9 @@ public class YamlConfigProvider extends FileConfigProvider {
|
|||||||
return this.comments.get(path);
|
return this.comments.get(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfigInitializer<YamlConfigProvider> getInitializer() {
|
||||||
|
return this.initializer;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.yaml;
|
|||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
|
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
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;
|
||||||
@@ -68,4 +69,16 @@ public class YamlSectionWrapper implements ConfigurationWrapper {
|
|||||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||||
return of(this.section.getConfigurationSection(path));
|
return of(this.section.getConfigurationSection(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz) {
|
||||||
|
return getSerializable(path, clazz, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Contract("_, _, !null -> !null")
|
||||||
|
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T defaultValue) {
|
||||||
|
return this.section.getSerializable(path, clazz, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package cc.carm.lib.configuration.yaml.builder;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
||||||
|
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
||||||
|
|
||||||
|
public abstract class AbstractYAMLBuilder<T, B extends AbstractYAMLBuilder<T, B>>
|
||||||
|
extends AbstractConfigBuilder<T, B, YamlConfigProvider> {
|
||||||
|
|
||||||
|
public AbstractYAMLBuilder() {
|
||||||
|
super(YamlConfigProvider.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package cc.carm.lib.configuration.yaml.builder;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||||
|
import cc.carm.lib.configuration.yaml.builder.serializable.SerializableBuilder;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class YamlConfigBuilder extends ConfigBuilder {
|
||||||
|
|
||||||
|
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
||||||
|
return new SerializableBuilder<>(valueClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
package cc.carm.lib.configuration.yaml.builder.serializable;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.yaml.builder.AbstractYAMLBuilder;
|
||||||
|
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class SerializableBuilder<T extends ConfigurationSerializable>
|
||||||
|
extends AbstractYAMLBuilder<T, SerializableBuilder<T>> {
|
||||||
|
|
||||||
|
protected final @NotNull Class<T> valueClass;
|
||||||
|
|
||||||
|
public SerializableBuilder(@NotNull Class<T> valueClass) {
|
||||||
|
this.valueClass = valueClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull SerializableBuilder<T> getThis() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfiguredSerializable<T> build() {
|
||||||
|
return new ConfiguredSerializable<>(this.provider, this.path, this.comments, this.valueClass, this.defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
package cc.carm.lib.configuration.yaml.value;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.yaml.YAMLValue;
|
||||||
|
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> {
|
||||||
|
|
||||||
|
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
|
||||||
|
return of(valueClass, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass,
|
||||||
|
@Nullable V defaultValue) {
|
||||||
|
return builder().ofSerializable(valueClass).defaults(defaultValue).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final @NotNull Class<T> valueClass;
|
||||||
|
|
||||||
|
public ConfiguredSerializable(@Nullable YamlConfigProvider provider,
|
||||||
|
@Nullable String configPath, @NotNull String[] comments,
|
||||||
|
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
||||||
|
super(provider, configPath, comments, defaultValue);
|
||||||
|
this.valueClass = valueClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable T get() {
|
||||||
|
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||||
|
try {
|
||||||
|
// 若未出现错误,则直接更新缓存并返回。
|
||||||
|
return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 出现了解析错误,提示并返回默认值。
|
||||||
|
e.printStackTrace();
|
||||||
|
return useDefault();
|
||||||
|
}
|
||||||
|
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(@Nullable T value) {
|
||||||
|
updateCache(value);
|
||||||
|
setValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package config;
|
package config;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.EasyConfiguration;
|
import cc.carm.lib.configuration.EasyConfiguration;
|
||||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
|
||||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
||||||
import config.misc.TestUser;
|
import config.model.TestModel;
|
||||||
import config.source.TestConfiguration;
|
import config.source.ComplexConfiguration;
|
||||||
|
import config.source.DemoConfiguration;
|
||||||
|
import config.source.ImplConfiguration;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@@ -15,31 +17,16 @@ import java.util.stream.IntStream;
|
|||||||
|
|
||||||
public class ConfigTester {
|
public class ConfigTester {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onTest() {
|
public void onTest() {
|
||||||
|
ConfigurationSerialization.registerClass(TestModel.class);
|
||||||
|
|
||||||
YamlConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
YamlConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
||||||
ConfigInitializer.initialize(provider, TestConfiguration.class, true);
|
|
||||||
|
|
||||||
System.out.println("before: " + TestConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
testDemo(provider);
|
||||||
TestConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
testComplex(provider);
|
||||||
System.out.println("after: " + TestConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
testSerialization(provider);
|
||||||
|
|
||||||
|
|
||||||
TestConfiguration.Sub.That.Operators.getNotNull().forEach(System.out::println);
|
|
||||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
|
||||||
TestConfiguration.Sub.That.Operators.set(operators);
|
|
||||||
|
|
||||||
System.out.println(TestConfiguration.USER.get());
|
|
||||||
TestUser b = new TestUser(UUID.randomUUID().toString().substring(0, 3), UUID.randomUUID());
|
|
||||||
TestConfiguration.USER.set(b);
|
|
||||||
|
|
||||||
TestConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
|
||||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
data.put((int) (1000 * Math.random()), UUID.randomUUID());
|
|
||||||
}
|
|
||||||
TestConfiguration.USERS.set(data);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
provider.save();
|
provider.save();
|
||||||
@@ -49,5 +36,45 @@ public class ConfigTester {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void testSerialization(YamlConfigProvider provider) {
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
provider.initialize(ImplConfiguration.class);
|
||||||
|
System.out.println(ImplConfiguration.TEST.get());
|
||||||
|
ImplConfiguration.TEST.set(TestModel.random());
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testDemo(YamlConfigProvider provider) {
|
||||||
|
provider.initialize(DemoConfiguration.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testComplex(YamlConfigProvider provider) {
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
provider.initialize(ComplexConfiguration.class);
|
||||||
|
|
||||||
|
System.out.println("> Test Value:");
|
||||||
|
System.out.println("before: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||||
|
ComplexConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||||
|
System.out.println("after: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||||
|
|
||||||
|
System.out.println("> Test List:");
|
||||||
|
ComplexConfiguration.Sub.That.Operators.getNotNull().forEach(System.out::println);
|
||||||
|
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||||
|
ComplexConfiguration.Sub.That.Operators.set(operators);
|
||||||
|
|
||||||
|
System.out.println("> Test Section:");
|
||||||
|
System.out.println(ComplexConfiguration.USER.get());
|
||||||
|
ComplexConfiguration.USER.set(TestModel.random());
|
||||||
|
|
||||||
|
System.out.println("> Test Maps:");
|
||||||
|
ComplexConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||||
|
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
data.put((int) (1000 * Math.random()), UUID.randomUUID());
|
||||||
|
}
|
||||||
|
ComplexConfiguration.USERS.set(data);
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
package config.misc;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class TestUser {
|
|
||||||
|
|
||||||
public String name;
|
|
||||||
public UUID uuid;
|
|
||||||
|
|
||||||
public TestUser(String name, UUID uuid) {
|
|
||||||
this.name = name;
|
|
||||||
this.uuid = uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUuid(UUID uuid) {
|
|
||||||
this.uuid = uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UUID getUuid() {
|
|
||||||
return uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "TestUser{" +
|
|
||||||
"name='" + name + '\'' +
|
|
||||||
", uuid=" + uuid +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package config.model;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||||
|
import org.bspfsystems.yamlconfiguration.serialization.SerializableAs;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.TestOnly;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@SerializableAs("TestModel")
|
||||||
|
public class TestModel implements ConfigurationSerializable {
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
public UUID uuid;
|
||||||
|
|
||||||
|
public TestModel(String name, UUID uuid) {
|
||||||
|
this.name = name;
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUuid(UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UUID getUuid() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Map<String, Object> serialize() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("name", name);
|
||||||
|
Map<String, Object> map2 = new HashMap<>();
|
||||||
|
map2.put("uuid", uuid.toString());
|
||||||
|
map.put("info", map2);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TestModel deserialize(ConfigurationWrapper section) {
|
||||||
|
String name = section.getString("name");
|
||||||
|
if (name == null) throw new NullPointerException("name is null");
|
||||||
|
String uuidString = section.getString("info.uuid");
|
||||||
|
if (uuidString == null) throw new NullPointerException("uuid is null");
|
||||||
|
return new TestModel(name, UUID.fromString(uuidString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestOnly
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static TestModel deserialize(Map<String, ?> args) {
|
||||||
|
String name = (String) args.get("name");
|
||||||
|
if (name == null) throw new NullPointerException("name is null");
|
||||||
|
Map<String, ?> map = (Map<String, ?>) args.get("info");
|
||||||
|
String uuidString = (String) map.get("uuid");
|
||||||
|
if (uuidString == null) throw new NullPointerException("uuid is null");
|
||||||
|
return new TestModel(name, UUID.fromString(uuidString));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TestModel random() {
|
||||||
|
return new TestModel(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "TestUser{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", uuid=" + uuid +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
-15
@@ -3,33 +3,26 @@ package config.source;
|
|||||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
import cc.carm.lib.configuration.core.util.MapFactory;
|
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
import config.misc.TestUser;
|
import config.model.TestModel;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class TestConfiguration extends ConfigurationRoot {
|
public class ComplexConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
@ConfigComment({"User测试"})
|
@ConfigComment({"User测试"})
|
||||||
public static final ConfigValue<TestUser> USER = ConfiguredSection
|
public static final ConfigValue<TestModel> USER = ConfiguredSection
|
||||||
.builder(TestUser.class)
|
.builder(TestModel.class)
|
||||||
.defaults(new TestUser("Carm", UUID.randomUUID()))
|
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||||
.parseValue((section, defaultValue) -> new TestUser(
|
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||||
section.getString("name"),
|
.serializeValue(TestModel::serialize).build();
|
||||||
UUID.fromString(section.getString("user.uuid", UUID.randomUUID().toString()))
|
|
||||||
)).serializeValue(user -> MapFactory.<String, Object>linkedMap()
|
|
||||||
.put("name", user.getName())
|
|
||||||
.put("user.uuid", user.getUuid().toString())
|
|
||||||
.build()
|
|
||||||
).build();
|
|
||||||
|
|
||||||
@ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"})
|
@ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||||
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
|
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
|
||||||
@@ -40,7 +33,7 @@ public class TestConfiguration extends ConfigurationRoot {
|
|||||||
|
|
||||||
public static class Sub {
|
public static class Sub {
|
||||||
|
|
||||||
@ConfigPath("uuid")
|
@ConfigPath(value = "uuid", root = true)
|
||||||
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
|
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
|
||||||
.builder(UUID.class).fromString()
|
.builder(UUID.class).fromString()
|
||||||
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
.parseValue((data, defaultValue) -> UUID.fromString(data))
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package config.source;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigComment;
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
|
|
||||||
|
@ConfigPath("database")
|
||||||
|
@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
|
||||||
|
public class DemoConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
|
@ConfigPath(root = true)
|
||||||
|
@ConfigComment({
|
||||||
|
"有时候,需要在配置文件最上面显示点东西,",
|
||||||
|
"此时就推荐添加一个可以用到但并不重要的参数到最上面",
|
||||||
|
"并给他添加对应的注释。"
|
||||||
|
})
|
||||||
|
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
|
||||||
|
|
||||||
|
|
||||||
|
@ConfigPath("driver")
|
||||||
|
@ConfigComment({
|
||||||
|
"数据库驱动配置,请根据数据库类型设置。",
|
||||||
|
"- MySQL: com.mysql.cj.jdbc.Driver",
|
||||||
|
"- MariaDB(推荐): org.mariadb.jdbc.Driver",
|
||||||
|
})
|
||||||
|
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
|
||||||
|
String.class, "com.mysql.cj.jdbc.Driver"
|
||||||
|
);
|
||||||
|
|
||||||
|
protected static final ConfigValue<String> HOST = ConfiguredValue.of(String.class, "127.0.0.1");
|
||||||
|
protected static final ConfigValue<Integer> PORT = ConfiguredValue.of(Integer.class, 3306);
|
||||||
|
|
||||||
|
protected static final ConfigValue<String> DATABASE = ConfiguredValue.of(String.class, "minecraft");
|
||||||
|
protected static final ConfigValue<String> USERNAME = ConfiguredValue.of(String.class, "root");
|
||||||
|
protected static final ConfigValue<String> PASSWORD = ConfiguredValue.of(String.class, "password");
|
||||||
|
|
||||||
|
protected static final ConfigValue<String> EXTRA = ConfiguredValue.of(String.class, "?useSSL=false");
|
||||||
|
|
||||||
|
protected static String buildJDBC() {
|
||||||
|
return String.format("jdbc:mysql://%s:%s/%s%s",
|
||||||
|
HOST.get(), PORT.get(), DATABASE.get(), EXTRA.get()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package config.source;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||||
|
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||||
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
|
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||||
|
import config.model.TestModel;
|
||||||
|
|
||||||
|
@ConfigPath("ImplConfiguration")
|
||||||
|
public class ImplConfiguration extends ConfigurationRoot {
|
||||||
|
|
||||||
|
public static final ConfigValue<TestModel> TEST = ConfiguredSerializable.of(TestModel.class);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1 +1,2 @@
|
|||||||
something: 123123
|
# Test Header
|
||||||
|
version: 1.0
|
||||||
@@ -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>1.0.3</version>
|
<version>1.1.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
+11
-4
@@ -1,6 +1,6 @@
|
|||||||
package cc.carm.lib.configuration.bungee;
|
package cc.carm.lib.configuration.bungee;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||||
import net.md_5.bungee.config.Configuration;
|
import net.md_5.bungee.config.Configuration;
|
||||||
import net.md_5.bungee.config.ConfigurationProvider;
|
import net.md_5.bungee.config.ConfigurationProvider;
|
||||||
@@ -11,9 +11,10 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class BungeeConfigProvider extends FileConfigProvider {
|
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
|
||||||
|
|
||||||
Configuration configuration;
|
protected Configuration configuration;
|
||||||
|
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
||||||
|
|
||||||
public BungeeConfigProvider(@NotNull File file) {
|
public BungeeConfigProvider(@NotNull File file) {
|
||||||
super(file);
|
super(file);
|
||||||
@@ -21,10 +22,11 @@ public class BungeeConfigProvider extends FileConfigProvider {
|
|||||||
|
|
||||||
public void initializeConfig() throws IOException {
|
public void initializeConfig() throws IOException {
|
||||||
this.configuration = getLoader().load(file);
|
this.configuration = getLoader().load(file);
|
||||||
|
this.initializer = new ConfigInitializer<>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ConfigurationWrapper getConfiguration() {
|
public @NotNull BungeeSectionWrapper getConfiguration() {
|
||||||
return BungeeSectionWrapper.of(configuration);
|
return BungeeSectionWrapper.of(configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +49,11 @@ public class BungeeConfigProvider extends FileConfigProvider {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
|
||||||
|
return this.initializer;
|
||||||
|
}
|
||||||
|
|
||||||
public static ConfigurationProvider getLoader() {
|
public static ConfigurationProvider getLoader() {
|
||||||
return ConfigurationProvider.getProvider(YamlConfiguration.class);
|
return ConfigurationProvider.getProvider(YamlConfiguration.class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>1.0.3</version>
|
<version>1.1.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
+1
-1
@@ -17,7 +17,7 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An hacky extension of {@link Yaml} that allows to write comments when dumping.
|
* A hacky extension of {@link Yaml} that allows to write comments when dumping.
|
||||||
*/
|
*/
|
||||||
public class CommentedYaml extends Yaml {
|
public class CommentedYaml extends Yaml {
|
||||||
|
|
||||||
|
|||||||
+17
-6
@@ -1,17 +1,18 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
package cc.carm.lib.configuration.spigot;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.commented.CommentedYamlConfiguration;
|
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||||
|
import cc.carm.lib.configuration.commented.CommentedYamlConfiguration;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public class SpigotConfigProvider extends FileConfigProvider {
|
public class SpigotConfigProvider extends FileConfigProvider<SpigotSectionWrapper> {
|
||||||
|
|
||||||
ConfigComments comments = new ConfigComments();
|
protected final ConfigComments comments = new ConfigComments();
|
||||||
CommentedYamlConfiguration configuration;
|
protected ConfigInitializer<SpigotConfigProvider> initializer;
|
||||||
|
protected CommentedYamlConfiguration configuration;
|
||||||
|
|
||||||
public SpigotConfigProvider(@NotNull File file) {
|
public SpigotConfigProvider(@NotNull File file) {
|
||||||
super(file);
|
super(file);
|
||||||
@@ -19,10 +20,15 @@ public class SpigotConfigProvider extends FileConfigProvider {
|
|||||||
|
|
||||||
public void initializeConfig() {
|
public void initializeConfig() {
|
||||||
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
||||||
|
this.initializer = new ConfigInitializer<>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void say() {
|
||||||
|
System.out.println("Hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ConfigurationWrapper getConfiguration() {
|
public @NotNull SpigotSectionWrapper getConfiguration() {
|
||||||
return SpigotSectionWrapper.of(configuration);
|
return SpigotSectionWrapper.of(configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,4 +52,9 @@ public class SpigotConfigProvider extends FileConfigProvider {
|
|||||||
return this.comments.get(path);
|
return this.comments.get(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfigInitializer<SpigotConfigProvider> getInitializer() {
|
||||||
|
return this.initializer;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+14
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.spigot;
|
|||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||||
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;
|
||||||
@@ -68,4 +69,17 @@ public class SpigotSectionWrapper implements ConfigurationWrapper {
|
|||||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||||
return of(this.section.getConfigurationSection(path));
|
return of(this.section.getConfigurationSection(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz) {
|
||||||
|
return getSerializable(path, clazz, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Contract("_, _, !null -> !null")
|
||||||
|
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T defaultValue) {
|
||||||
|
return this.section.getSerializable(path, clazz, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package cc.carm.lib.configuration.spigot;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||||
|
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||||
|
import cc.carm.lib.configuration.spigot.builder.SpigotConfigBuilder;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public abstract class SpigotValue<T> extends CachedConfigValue<T> {
|
||||||
|
|
||||||
|
public static @NotNull SpigotConfigBuilder builder() {
|
||||||
|
return new SpigotConfigBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public SpigotValue(@Nullable SpigotConfigProvider provider,
|
||||||
|
@Nullable String configPath, @NotNull String[] comments, @Nullable T defaultValue) {
|
||||||
|
super(provider, configPath, comments, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpigotConfigProvider getSpigotProvider() {
|
||||||
|
ConfigurationProvider<?> provider = getProvider();
|
||||||
|
if (provider instanceof SpigotConfigProvider) return (SpigotConfigProvider) getProvider();
|
||||||
|
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpigotSectionWrapper getSpigotConfig() {
|
||||||
|
return getSpigotProvider().getConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package cc.carm.lib.configuration.spigot.builder;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
||||||
|
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
||||||
|
|
||||||
|
public abstract class AbstractSpigotBuilder<T, B extends AbstractSpigotBuilder<T, B>>
|
||||||
|
extends AbstractConfigBuilder<T, B, SpigotConfigProvider> {
|
||||||
|
|
||||||
|
public AbstractSpigotBuilder() {
|
||||||
|
super(SpigotConfigProvider.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package cc.carm.lib.configuration.spigot.builder;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||||
|
import cc.carm.lib.configuration.spigot.builder.serializable.SerializableBuilder;
|
||||||
|
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class SpigotConfigBuilder extends ConfigBuilder {
|
||||||
|
|
||||||
|
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
||||||
|
return new SerializableBuilder<>(valueClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
package cc.carm.lib.configuration.spigot.builder.serializable;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.spigot.builder.AbstractSpigotBuilder;
|
||||||
|
import cc.carm.lib.configuration.spigot.value.ConfiguredSerializable;
|
||||||
|
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class SerializableBuilder<T extends ConfigurationSerializable>
|
||||||
|
extends AbstractSpigotBuilder<T, SerializableBuilder<T>> {
|
||||||
|
|
||||||
|
protected final @NotNull Class<T> valueClass;
|
||||||
|
|
||||||
|
public SerializableBuilder(@NotNull Class<T> valueClass) {
|
||||||
|
this.valueClass = valueClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull SerializableBuilder<T> getThis() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfiguredSerializable<T> build() {
|
||||||
|
return new ConfiguredSerializable<>(this.provider, this.path, this.comments, this.valueClass, this.defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
package cc.carm.lib.configuration.spigot.item;
|
||||||
|
|
||||||
|
public class ItemConfiguration {
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
package cc.carm.lib.configuration.spigot.value;
|
||||||
|
|
||||||
|
public class ConfiguredItem {
|
||||||
|
}
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
package cc.carm.lib.configuration.spigot.value;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
||||||
|
import cc.carm.lib.configuration.spigot.SpigotValue;
|
||||||
|
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends SpigotValue<T> {
|
||||||
|
|
||||||
|
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
|
||||||
|
return of(valueClass, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass,
|
||||||
|
@Nullable V defaultValue) {
|
||||||
|
return builder().ofSerializable(valueClass).defaults(defaultValue).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final @NotNull Class<T> valueClass;
|
||||||
|
|
||||||
|
public ConfiguredSerializable(@Nullable SpigotConfigProvider provider,
|
||||||
|
@Nullable String configPath, @NotNull String[] comments,
|
||||||
|
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
||||||
|
super(provider, configPath, comments, defaultValue);
|
||||||
|
this.valueClass = valueClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable T get() {
|
||||||
|
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||||
|
try {
|
||||||
|
// 若未出现错误,则直接更新缓存并返回。
|
||||||
|
return updateCache(getSpigotConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 出现了解析错误,提示并返回默认值。
|
||||||
|
e.printStackTrace();
|
||||||
|
return useDefault();
|
||||||
|
}
|
||||||
|
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(@Nullable T value) {
|
||||||
|
updateCache(value);
|
||||||
|
setValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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>1.0.3</version>
|
<version>1.1.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>core</module>
|
<module>core</module>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user