1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 18:48:20 +08:00

Compare commits

..

28 Commits

Author SHA1 Message Date
carm 6f97166192 fix(parse): 修复Number基础类型转型丢失的问题。 fix #17 2022-12-06 21:53:05 +08:00
dependabot[bot] ccd239ad6b chore(deps): Bump yamlconfiguration from 1.2.2 to 1.3.0 (#16)
Bumps [yamlconfiguration](https://github.com/bspfsystems/YamlConfiguration) from 1.2.2 to 1.3.0.
- [Release notes](https://github.com/bspfsystems/YamlConfiguration/releases)
- [Commits](https://github.com/bspfsystems/YamlConfiguration/compare/v1.2.2...v1.3.0)

---
updated-dependencies:
- dependency-name: org.bspfsystems:yamlconfiguration
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-11-03 19:50:13 +08:00
dependabot[bot] 69b27476bc chore(deps): Bump maven-shade-plugin from 3.4.0 to 3.4.1 (#15)
Bumps [maven-shade-plugin](https://github.com/apache/maven-shade-plugin) from 3.4.0 to 3.4.1.
- [Release notes](https://github.com/apache/maven-shade-plugin/releases)
- [Commits](https://github.com/apache/maven-shade-plugin/compare/maven-shade-plugin-3.4.0...maven-shade-plugin-3.4.1)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-shade-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-11-02 18:31:07 +08:00
dependabot[bot] 0651cac6b0 chore(deps): Bump gson from 2.9.1 to 2.10 (#14)
Bumps [gson](https://github.com/google/gson) from 2.9.1 to 2.10.
- [Release notes](https://github.com/google/gson/releases)
- [Changelog](https://github.com/google/gson/blob/master/CHANGELOG.md)
- [Commits](https://github.com/google/gson/compare/gson-parent-2.9.1...gson-parent-2.10)

---
updated-dependencies:
- dependency-name: com.google.code.gson:gson
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-10-27 00:00:49 +08:00
dependabot[bot] 1a1efad283 Bump maven-shade-plugin from 3.3.0 to 3.4.0 (#11)
Bumps [maven-shade-plugin](https://github.com/apache/maven-shade-plugin) from 3.3.0 to 3.4.0.
- [Release notes](https://github.com/apache/maven-shade-plugin/releases)
- [Commits](https://github.com/apache/maven-shade-plugin/compare/maven-shade-plugin-3.3.0...maven-shade-plugin-3.4.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-shade-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-20 00:12:03 +08:00
dependabot[bot] 3c1ba61b61 Bump yamlconfiguration from 1.2.1 to 1.2.2 (#10)
Bumps [yamlconfiguration](https://github.com/bspfsystems/YamlConfiguration) from 1.2.1 to 1.2.2.
- [Release notes](https://github.com/bspfsystems/YamlConfiguration/releases)
- [Commits](https://github.com/bspfsystems/YamlConfiguration/compare/v1.2.1...v1.2.2)

---
updated-dependencies:
- dependency-name: org.bspfsystems:yamlconfiguration
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-20 00:11:44 +08:00
dependabot[bot] 1a3e84a787 Bump maven-jar-plugin from 3.2.2 to 3.3.0 (#12)
Bumps [maven-jar-plugin](https://github.com/apache/maven-jar-plugin) from 3.2.2 to 3.3.0.
- [Release notes](https://github.com/apache/maven-jar-plugin/releases)
- [Commits](https://github.com/apache/maven-jar-plugin/compare/maven-jar-plugin-3.2.2...maven-jar-plugin-3.3.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-jar-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-20 00:11:31 +08:00
carm 00228db2c4 chore(dev): 取消开发中模块的部署 2022-09-10 01:11:04 +08:00
carm 0fddfe28af feat(source): 为 ConfigurationWrapper 添加 getSource() 方法以获取源实现内容。
BREAKING-CHANGE: ConfigurationWrapper 更改为泛型类,并新增 “getSource” 方法需要实现。
2022-09-10 00:36:46 +08:00
dependabot[bot] 4a17089da0 Bump gson from 2.9.0 to 2.9.1 (#6)
Bumps [gson](https://github.com/google/gson) from 2.9.0 to 2.9.1.
- [Release notes](https://github.com/google/gson/releases)
- [Changelog](https://github.com/google/gson/blob/master/CHANGELOG.md)
- [Commits](https://github.com/google/gson/compare/gson-parent-2.9.0...gson-parent-2.9.1)

---
updated-dependencies:
- dependency-name: com.google.code.gson:gson
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-09 21:54:36 +08:00
dependabot[bot] 8faa7b1c24 Bump yamlconfiguration from 1.2.0 to 1.2.1 (#7)
Bumps [yamlconfiguration](https://github.com/bspfsystems/YamlConfiguration) from 1.2.0 to 1.2.1.
- [Release notes](https://github.com/bspfsystems/YamlConfiguration/releases)
- [Commits](https://github.com/bspfsystems/YamlConfiguration/compare/v1.2.0...v1.2.1)

---
updated-dependencies:
- dependency-name: org.bspfsystems:yamlconfiguration
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-09 21:54:25 +08:00
dependabot[bot] 5e525428fe Bump maven-javadoc-plugin from 3.4.0 to 3.4.1 (#8)
Bumps [maven-javadoc-plugin](https://github.com/apache/maven-javadoc-plugin) from 3.4.0 to 3.4.1.
- [Release notes](https://github.com/apache/maven-javadoc-plugin/releases)
- [Commits](https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.4.0...maven-javadoc-plugin-3.4.1)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-javadoc-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-09 21:54:16 +08:00
carm bc0dfd5698 feat(source): 为 ConfigurationWrapper 添加 getSource() 方法以获取源实现内容。
BREAKING-CHANGE: ConfigurationWrapper 更改为泛型类,并新增 “getSource” 方法需要实现。
2022-09-09 21:54:05 +08:00
carm dc28d743db feat(source): 为 ConfigurationWrapper 添加 getSource() 方法以获取源实现内容。
BREAKING-CHANGE: ConfigurationWrapper 更改为泛型类,并新增 “getSource” 方法需要实现。
2022-09-09 21:52:26 +08:00
carm f61294c5f3 feat(sql): 优化数据表结构 2022-08-11 17:43:42 +08:00
carm 6883a464db feat(sql): 添加数据库存储支持 2022-08-11 16:02:12 +08:00
carm bcdf0d9bd1 refactor(comments): 优化注释的存储方式 2022-08-11 16:01:27 +08:00
carm 5f89ff4db7 Merge remote-tracking branch 'origin/master' 2022-05-20 18:18:47 +08:00
carm d6f4970277 [3.1.0] fix(api): configuration reload problem
BREAKING CHANGES: Now we override "onReload()" method to define ConfigurationProvider how to reload the configs.

Fix the problem that when the configuration file is read, the "reload()" method not working.
2022-05-20 18:16:44 +08:00
carm c045ca1489 Merge pull request #5 from CarmJos/dependabot/maven/org.bspfsystems-yamlconfiguration-1.2.0
Bump yamlconfiguration from 1.1.0 to 1.2.0
2022-05-20 10:10:38 +08:00
dependabot[bot] ceea900b08 Bump yamlconfiguration from 1.1.0 to 1.2.0
Bumps [yamlconfiguration](https://github.com/bspfsystems/YamlConfiguration) from 1.1.0 to 1.2.0.
- [Release notes](https://github.com/bspfsystems/YamlConfiguration/releases)
- [Commits](https://github.com/bspfsystems/YamlConfiguration/compare/v1.1.0...v1.2.0)

---
updated-dependencies:
- dependency-name: org.bspfsystems:yamlconfiguration
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-19 14:41:47 +00:00
carm 85bacb24f3 Merge remote-tracking branch 'origin/master' 2022-05-18 23:23:25 +08:00
carm 6b3a353fcc [3.0.0] (breaking-update) update comments usage. 2022-05-18 23:23:11 +08:00
carm 2c026fc0b0 Merge pull request #4 from CarmJos/dependabot/maven/org.bspfsystems-yamlconfiguration-1.1.0
Bump yamlconfiguration from 1.0.11 to 1.1.0
2022-04-25 22:59:37 +08:00
dependabot[bot] f8b4bbd3a9 Bump yamlconfiguration from 1.0.11 to 1.1.0
Bumps [yamlconfiguration](https://github.com/bspfsystems/YamlConfiguration) from 1.0.11 to 1.1.0.
- [Release notes](https://github.com/bspfsystems/YamlConfiguration/releases)
- [Commits](https://github.com/bspfsystems/YamlConfiguration/compare/v1.0.11...v1.1.0)

---
updated-dependencies:
- dependency-name: org.bspfsystems:yamlconfiguration
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-04-25 14:45:52 +00:00
carm e9a0f0ff30 [ci skip] 补充README介绍 2022-04-23 21:37:25 +08:00
carm e3fe6e7c80 [ci skip] 补充README介绍 2022-04-23 21:37:18 +08:00
carm c179fa2ccd 添加部分注释 2022-04-23 20:52:08 +08:00
56 changed files with 747 additions and 468 deletions
+16
View File
@@ -20,6 +20,9 @@
## 优势
- 基于类的配置文件初始化、加载、获取与更新机制,方便快捷。
- 支持复杂配置的手动序列化、反序列化。
- 提供多种builder形式,快速构建 `ConfigValue<?>` 对象。
- 支持通过注解规定配置对应的路径、注释等信息。
## 开发
@@ -91,6 +94,15 @@
<scope>compile</scope>
</dependency>
<!--基于JSON文件的实现版本,可用于全部Java环境。-->
<!--需要注意的是,JSON不支持文件注释。-->
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-json</artifactId>
<version>[LATEST RELEASE]</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
```
@@ -131,6 +143,10 @@ dependencies {
//基于YAML文件的实现版本,可用于全部Java环境。
api "cc.carm.lib:easyconfiguration-yaml:[LATEST RELEASE]"
//基于JSON文件的实现版本,可用于全部Java环境。
//需要注意的是,JSON不支持文件注释。
api "cc.carm.lib:easyconfiguration-json:[LATEST RELEASE]"
}
```
+3 -1
View File
@@ -5,12 +5,14 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>2.3.0</version>
<version>3.2.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyconfiguration-core</artifactId>
@@ -1,14 +1,16 @@
package cc.carm.lib.configuration.core;
import cc.carm.lib.configuration.core.annotation.ConfigComment;
import cc.carm.lib.configuration.core.annotation.ConfigPath;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.annotation.InlineComment;
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;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
/**
* 配置文件类初始化方法
@@ -45,7 +47,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
public void initialize(@NotNull Class<? extends ConfigurationRoot> clazz,
boolean saveDefaults, boolean loadSubClasses) {
initializeClass(
clazz, null,
clazz, null, null,
null, null, null,
saveDefaults, loadSubClasses
);
@@ -60,33 +62,29 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
protected void initializeClass(@NotNull Class<?> clazz,
@Nullable String parentPath, @Nullable String fieldName,
@Nullable ConfigPath fieldPath, @Nullable ConfigComment filedComments,
@Nullable ConfigPath fieldPath,
@Nullable HeaderComment fieldHeaderComments,
@Nullable InlineComment fieldInlineComments,
boolean saveDefaults, boolean loadSubClasses) {
String path = getClassPath(clazz, parentPath, fieldName, fieldPath);
this.provider.setComment(path, getClassComments(clazz, filedComments));
this.provider.setHeaderComment(path, getClassHeaderComments(clazz, fieldHeaderComments));
if (path != null) this.provider.setInlineComment(path, readInlineComments(fieldInlineComments));
for (Field field : clazz.getDeclaredFields()) {
initializeField(clazz, field, path, saveDefaults, loadSubClasses);
}
if (!loadSubClasses) return;
Class<?>[] classes = clazz.getDeclaredClasses();
if (loadSubClasses && classes.length > 0) {
// 逆向加载,保持顺序。
for (int i = classes.length - 1; i >= 0; i--) {
initializeClass(
classes[i], path, classes[i].getSimpleName(),
null, null,
saveDefaults, true
);
}
for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。
initializeClass(
classes[i], path, classes[i].getSimpleName(),
null, null, null,
saveDefaults, true
);
}
}
protected void initializeValue(@NotNull ConfigValue<?> value, @NotNull String path,
@Nullable ConfigCommentInfo comments, boolean saveDefaults) {
value.initialize(provider, saveDefaults, path, comments);
}
private void initializeField(@NotNull Class<?> source, @NotNull Field field, @Nullable String parent,
boolean saveDefaults, boolean loadSubClasses) {
try {
@@ -95,14 +93,16 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
if (object instanceof ConfigValue<?>) {
initializeValue(
(ConfigValue<?>) object, getFieldPath(field, parent),
ConfigCommentInfo.fromAnnotation(field.getAnnotation(ConfigComment.class)),
field.getAnnotation(HeaderComment.class),
field.getAnnotation(InlineComment.class),
saveDefaults
);
} else if (object instanceof Class<?>) {
initializeClass(
(Class<?>) object, parent, field.getName(),
field.getAnnotation(ConfigPath.class),
field.getAnnotation(ConfigComment.class),
field.getAnnotation(HeaderComment.class),
field.getAnnotation(InlineComment.class),
saveDefaults, loadSubClasses
);
}
@@ -110,15 +110,36 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
}
}
protected void setComments(@Nullable String path, @Nullable ConfigCommentInfo comments) {
if (comments != null) this.provider.setComment(path, comments);
protected void initializeValue(@NotNull ConfigValue<?> value, @NotNull String path,
@Nullable HeaderComment fieldHeaderComment,
@Nullable InlineComment fieldInlineComment,
boolean saveDefaults) {
value.initialize(
provider, saveDefaults, path,
readHeaderComments(fieldHeaderComment),
readInlineComments(fieldInlineComment)
);
}
protected static @Nullable ConfigCommentInfo getClassComments(@NotNull Class<?> clazz,
@Nullable ConfigComment fieldAnnotation) {
ConfigCommentInfo classComments = ConfigCommentInfo.fromAnnotation(clazz.getAnnotation(ConfigComment.class));
protected static @Nullable List<String> getClassHeaderComments(@NotNull Class<?> clazz,
@Nullable HeaderComment fieldAnnotation) {
List<String> classComments = readHeaderComments(clazz.getAnnotation(HeaderComment.class));
if (classComments != null) return classComments;
return ConfigCommentInfo.fromAnnotation(fieldAnnotation);
else return readHeaderComments(fieldAnnotation);
}
protected static List<String> readHeaderComments(@Nullable HeaderComment annotation) {
if (annotation == null) return null;
String[] value = annotation.value();
return value.length > 0 ? Arrays.asList(value) : null;
}
protected static @Nullable String readInlineComments(@Nullable InlineComment annotation) {
if (annotation == null) return null;
String value = annotation.value();
return value.length() > 0 ? value : null;
}
protected static @Nullable String getClassPath(@NotNull Class<?> clazz,
@@ -1,45 +0,0 @@
package cc.carm.lib.configuration.core.annotation;
import org.jetbrains.annotations.NotNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigComment {
@NotNull
String[] value() default "";
/**
* 首行换行,即会在注释开始前进行一次换行,与上方配置分离,优化观感。
* 如:
* <blockquote><pre>
* some-key: "SomeValue"
*
* # 注释第一行
* # 注释第二行
* startWrap: true
* </pre></blockquote>
*
* @return 是否在结尾添加换行符
*/
boolean startWrap() default true;
/**
* 末尾换行,即会在注释结束后进行一次换行,如:
* <blockquote><pre>
* # 注释第一行
* # 注释第二行
*
* endWrap: true
* </pre></blockquote>
* <p> 该功能可用于编写配置文件的顶部注释。
*
* @return 是否在结尾添加换行符
*/
boolean endWrap() default false;
}
@@ -0,0 +1,39 @@
package cc.carm.lib.configuration.core.annotation;
import org.jetbrains.annotations.NotNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 顶部注释,用于给对应配置的顶部添加注释,便于使用者阅读查看。
* <p>如:
* <blockquote><pre>
* # 注释第一行
* # 注释第二行
* foo: "bar"
* </pre></blockquote>
*/
@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HeaderComment {
/**
* 注释内容,若内容长度为0则会视为一个空行。
* <p> 如 <b>{"foo","","bar"}</b>
* 会被添加为
* <blockquote><pre>
* # foo
*
* # bar
* foo: "bar"
* </pre></blockquote>
*
* @return 注释内容
*/
@NotNull
String[] value() default "";
}
@@ -0,0 +1,33 @@
package cc.carm.lib.configuration.core.annotation;
import org.jetbrains.annotations.NotNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 行内注释,用于给对应配置的所在行添加注释,便于使用者阅读查看。
* 如:
* <blockquote><pre>
* foo: "bar" # 注释内容
* </pre></blockquote>
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface InlineComment {
/**
* 注释内容,若内容长度为0则不会添加注释
* <p> 如 <b>"foobar"</b> 将被设定为
* <blockquote><pre>
* foo: "bar" # foobar
* </pre></blockquote>
*
* @return 注释内容
*/
@NotNull
String value() default "";
}
@@ -1,11 +1,13 @@
package cc.carm.lib.configuration.core.builder;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
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;
import java.util.Arrays;
import java.util.List;
public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T, B, P>, P extends ConfigurationProvider<?>> {
protected final Class<? super P> providerClass;
@@ -13,9 +15,8 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
protected @Nullable P provider;
protected @Nullable String path;
protected @NotNull String[] comments = new String[0];
protected boolean startWrap = true;
protected boolean endWrap = false;
protected @Nullable List<String> headerComments;
protected @Nullable String inlineComment;
protected @Nullable T defaultValue;
@@ -38,38 +39,26 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
}
public @NotNull B comments(@NotNull String... comments) {
this.comments = comments;
return headerComments(comments);
}
public @NotNull B headerComments(@NotNull String... comments) {
return headerComments(Arrays.asList(comments));
}
public @NotNull B headerComments(@NotNull List<String> comments) {
this.headerComments = comments;
return getThis();
}
public @NotNull B setStartWarp(boolean enable) {
this.startWrap = enable;
public @NotNull B inlineComment(@NotNull String comment) {
this.inlineComment = comment;
return getThis();
}
public @NotNull B startWarp() {
return setStartWarp(true);
}
public @NotNull B setEndWarp(boolean enable) {
this.endWrap = enable;
return getThis();
}
public @NotNull B endWarp() {
return setEndWarp(true);
}
public @NotNull B defaults(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
return getThis();
}
protected @Nullable ConfigCommentInfo buildComments() {
ConfigCommentInfo info = ConfigCommentInfo.of(this.comments, this.startWrap, this.endWrap);
if (info.equals(ConfigCommentInfo.defaults())) return null;
else return info;
}
}
@@ -1,8 +1,10 @@
package cc.carm.lib.configuration.core.builder.list;
import cc.carm.lib.configuration.core.annotation.InlineComment;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import org.jetbrains.annotations.NotNull;
public class ConfigListBuilder<V> {
protected final @NotNull Class<V> valueClass;
@@ -65,7 +65,8 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
@Override
public @NotNull ConfiguredList<V> build() {
return new ConfiguredList<>(
this.provider, this.path, this.buildComments(),
this.provider, this.path,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.sourceParser.andThen(this.valueParser),
this.valueSerializer.andThen(sourceSerializer)
@@ -89,7 +89,8 @@ public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfig
@Override
public @NotNull ConfiguredMap<K, V> build() {
return new ConfiguredMap<>(
this.provider, this.path,this.buildComments(),
this.provider, this.path,
this.headerComments, this.inlineComment,
this.defaultValue, this.supplier,
this.keyClass, this.keyParser,
this.valueClass, this.sourceParser.andThen(this.valueParser),
@@ -19,7 +19,7 @@ public class ConfigValueBuilder<V> {
return fromSection(ConfigValueParser.required(), ConfigDataFunction.required());
}
public @NotNull SectionValueBuilder<V> fromSection(@NotNull ConfigValueParser<ConfigurationWrapper, V> valueParser,
public @NotNull SectionValueBuilder<V> fromSection(@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> valueParser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> valueSerializer) {
return new SectionValueBuilder<>(this.valueClass, valueParser, valueSerializer);
}
@@ -15,11 +15,11 @@ public class SectionValueBuilder<V>
protected final @NotNull Class<V> valueClass;
protected @NotNull ConfigValueParser<ConfigurationWrapper, V> parser;
protected @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser;
protected @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
public SectionValueBuilder(@NotNull Class<V> valueClass,
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
this.valueClass = valueClass;
this.parser = parser;
@@ -32,7 +32,7 @@ public class SectionValueBuilder<V>
return this;
}
public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper, V> valueParser) {
public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper<?>, V> valueParser) {
this.parser = valueParser;
return this;
}
@@ -45,7 +45,8 @@ public class SectionValueBuilder<V>
@Override
public @NotNull ConfiguredSection<V> build() {
return new ConfiguredSection<>(
this.provider, this.path,this.buildComments(),
this.provider, this.path,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.parser, this.serializer
);
@@ -3,7 +3,6 @@ package cc.carm.lib.configuration.core.builder.value;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import org.jetbrains.annotations.NotNull;
@@ -58,7 +57,8 @@ public class SourceValueBuilder<S, V> extends CommonConfigBuilder<V, SourceValue
@Override
public @NotNull ConfiguredValue<V> build() {
return new ConfiguredValue<>(
this.provider, this.path, this.buildComments(),
this.provider, this.path,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.valueParser.compose(this.sourceParser),
this.valueSerializer.andThen(sourceSerializer)
@@ -50,6 +50,23 @@ public interface ConfigValueParser<T, R> {
@Contract(pure = true)
static <V> @NotNull ConfigValueParser<Object, V> castObject(Class<V> valueClass) {
return (input, defaultValue) -> {
if (Number.class.isAssignableFrom(valueClass)) {
if (Long.class.isAssignableFrom(valueClass)) {
input = longValue().parse(input, null);
} else if (Integer.class.isAssignableFrom(valueClass)) {
input = intValue().parse(input, null);
} else if (Float.class.isAssignableFrom(valueClass)) {
input = floatValue().parse(input, null);
} else if (Double.class.isAssignableFrom(valueClass)) {
input = doubleValue().parse(input, null);
} else if (Byte.class.isAssignableFrom(valueClass)) {
input = byteValue().parse(input, null);
} else if (Short.class.isAssignableFrom(valueClass)) {
input = shortValue().parse(input, null);
}
}
if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName());
};
@@ -1,80 +0,0 @@
package cc.carm.lib.configuration.core.source;
import cc.carm.lib.configuration.core.annotation.ConfigComment;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
public class ConfigCommentInfo {
public static @NotNull ConfigCommentInfo DEFAULT_INFO = of(new String[0], true, false);
protected final @NotNull String[] comments;
protected final boolean startWrap;
protected final boolean endWrap;
public ConfigCommentInfo(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
this.comments = comments;
this.startWrap = startWrap;
this.endWrap = endWrap;
}
public @NotNull String[] getComments() {
return comments;
}
public boolean endWrap() {
return endWrap;
}
public boolean startWrap() {
return startWrap;
}
public static @NotNull ConfigCommentInfo of(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
return new ConfigCommentInfo(comments, startWrap, endWrap);
}
public static @NotNull ConfigCommentInfo defaults() {
return DEFAULT_INFO;
}
@Contract("!null->!null")
public static @Nullable ConfigCommentInfo fromAnnotation(@Nullable ConfigComment comment) {
if (comment == null) return null;
else return new ConfigCommentInfo(comment.value(), comment.startWrap(), comment.endWrap());
}
public static @NotNull ConfigCommentInfo fromValue(@NotNull ConfigValue<?> value) {
return Optional.ofNullable(value.getComments()).orElse(defaults());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConfigCommentInfo that = (ConfigCommentInfo) o;
return startWrap == that.startWrap && endWrap == that.endWrap && Arrays.equals(getComments(), that.getComments());
}
@Override
public int hashCode() {
int result = Objects.hash(startWrap, endWrap);
result = 31 * result + Arrays.hashCode(getComments());
return result;
}
@Override
public String toString() {
return "ConfigCommentInfo{" +
"comments=" + Arrays.toString(comments) +
", startWrap=" + startWrap +
", endWrap=" + endWrap +
'}';
}
}
@@ -0,0 +1,51 @@
package cc.carm.lib.configuration.core.source;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.*;
public abstract class ConfigurationComments {
protected final @NotNull Map<String, List<String>> headerComments = new HashMap<>();
protected final @NotNull Map<String, String> inlineComments = new HashMap<>();
protected @NotNull Map<String, List<String>> getHeaderComments() {
return headerComments;
}
protected @NotNull Map<String, String> getInlineComments() {
return inlineComments;
}
public void setHeaderComments(@Nullable String path, @Nullable List<String> comments) {
if (comments == null) {
getHeaderComments().remove(path);
} else {
getHeaderComments().put(path, comments);
}
}
public void setInlineComment(@NotNull String path, @Nullable String comment) {
if (comment == null) {
getInlineComments().remove(path);
} else {
getInlineComments().put(path, comment);
}
}
@Nullable
@Unmodifiable
public List<String> getHeaderComment(@Nullable String path) {
return Optional.ofNullable(getHeaderComments().get(path)).map(Collections::unmodifiableList).orElse(null);
}
public @Nullable String getInlineComment(@NotNull String path) {
return getInlineComments().get(path);
}
}
@@ -4,8 +4,12 @@ import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.ConfigurationRoot;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
public abstract class ConfigurationProvider<W extends ConfigurationWrapper> {
import java.util.List;
import java.util.Optional;
public abstract class ConfigurationProvider<W extends ConfigurationWrapper<?>> {
protected long updateTime;
@@ -18,18 +22,41 @@ public abstract class ConfigurationProvider<W extends ConfigurationWrapper> {
}
public boolean isExpired(long time) {
return this.updateTime > time;
return getUpdateTime() > time;
}
public abstract @NotNull W getConfiguration();
public abstract void reload() throws Exception;
public void reload() throws Exception {
onReload(); // 调用重写的Reload方法
this.updateTime = System.currentTimeMillis();
}
public abstract void save() throws Exception;
public abstract void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment);
protected abstract void onReload() throws Exception;
public abstract @Nullable ConfigCommentInfo getComment(@Nullable String path);
public abstract @Nullable ConfigurationComments getComments();
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
if (getComments() == null) return;
getComments().setHeaderComments(path, comments);
}
public void setInlineComment(@NotNull String path, @Nullable String comment) {
if (getComments() == null) return;
getComments().setInlineComment(path, comment);
}
@Nullable
@Unmodifiable
public List<String> getHeaderComment(@Nullable String path) {
return Optional.ofNullable(getComments()).map(c -> c.getHeaderComment(path)).orElse(null);
}
public @Nullable String getInlineComment(@NotNull String path) {
return Optional.ofNullable(getComments()).map(c -> c.getInlineComment(path)).orElse(null);
}
public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();
@@ -4,10 +4,15 @@ import cc.carm.lib.configuration.core.function.ConfigValueParser;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
interface ConfigurationReader {
ConfigurationWrapper getWrapper();
ConfigurationWrapper<?> getWrapper();
default boolean isBoolean(@NotNull String path) {
return getWrapper().isType(path, Boolean.class);
@@ -26,7 +31,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Byte.class);
}
default @Nullable Byte getByte(@NotNull String path) {
return getByte(path, (byte) 0);
}
@@ -36,12 +40,10 @@ interface ConfigurationReader {
return getWrapper().get(path, def, ConfigValueParser.byteValue());
}
default boolean isShort(@NotNull String path) {
return getWrapper().isType(path, Short.class);
}
default @Nullable Short getShort(@NotNull String path) {
return getShort(path, (short) 0);
}
@@ -56,7 +58,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Integer.class);
}
default @Nullable Integer getInt(@NotNull String path) {
return getInt(path, 0);
}
@@ -71,7 +72,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Long.class);
}
default @Nullable Long getLong(@NotNull String path) {
return getLong(path, 0L);
}
@@ -86,7 +86,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Float.class);
}
default @Nullable Float getFloat(@NotNull String path) {
return getFloat(path, 0.0F);
}
@@ -101,7 +100,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Double.class);
}
default @Nullable Double getDouble(@NotNull String path) {
return getDouble(path, 0.0D);
}
@@ -116,7 +114,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Boolean.class);
}
default @Nullable Character getChar(@NotNull String path) {
return getChar(path, null);
}
@@ -140,5 +137,54 @@ interface ConfigurationReader {
return getWrapper().get(path, def, String.class);
}
@Unmodifiable
default @NotNull List<String> getStringList(@NotNull String path) {
return parseList(getWrapper().getList(path), ConfigValueParser.castToString());
}
@Unmodifiable
default @NotNull List<Integer> getIntegerList(@NotNull String path) {
return parseList(getWrapper().getList(path), ConfigValueParser.intValue());
}
@Unmodifiable
default @NotNull List<Long> getLongList(@NotNull String path) {
return parseList(getWrapper().getList(path), ConfigValueParser.longValue());
}
@Unmodifiable
default @NotNull List<Double> getDoubleList(@NotNull String path) {
return parseList(getWrapper().getList(path), ConfigValueParser.doubleValue());
}
@Unmodifiable
default @NotNull List<Float> getFloatList(@NotNull String path) {
return parseList(getWrapper().getList(path), ConfigValueParser.floatValue());
}
@Unmodifiable
default @NotNull List<Byte> getByteList(@NotNull String path) {
return parseList(getWrapper().getList(path), ConfigValueParser.byteValue());
}
@Unmodifiable
default @NotNull List<Character> getCharList(@NotNull String path) {
return parseList(getWrapper().getList(path), ConfigValueParser.castObject(Character.class));
}
@Unmodifiable
static <T> @NotNull List<T> parseList(@Nullable List<?> list, ConfigValueParser<Object, T> parser) {
if (list == null) return Collections.emptyList();
List<T> values = new ArrayList<>();
for (Object o : list) {
try {
T parsed = parser.parse(o, null);
if (parsed != null) values.add(parsed);
} catch (Exception ignored) {
}
}
return values;
}
}
@@ -9,13 +9,15 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
public interface ConfigurationWrapper extends ConfigurationReader {
public interface ConfigurationWrapper<S> extends ConfigurationReader {
@Override
default ConfigurationWrapper getWrapper() {
default ConfigurationWrapper<S> getWrapper() {
return this;
}
@NotNull S getSource();
@NotNull
Set<String> getKeys(boolean deep);
@@ -66,6 +68,6 @@ public interface ConfigurationWrapper extends ConfigurationReader {
boolean isConfigurationSection(@NotNull String path);
@Nullable
ConfigurationWrapper getConfigurationSection(@NotNull String path);
ConfigurationWrapper<S> getConfigurationSection(@NotNull String path);
}
@@ -10,7 +10,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
public abstract class FileConfigProvider<W extends ConfigurationWrapper> extends ConfigurationProvider<W> {
public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> extends ConfigurationProvider<W> {
protected final @NotNull File file;
@@ -1,12 +1,13 @@
package cc.carm.lib.configuration.core.value;
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
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.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@@ -18,26 +19,37 @@ public abstract class ConfigValue<T> {
protected @Nullable ConfigurationProvider<?> provider;
protected @Nullable String configPath;
protected @Nullable ConfigCommentInfo comments;
protected @Nullable List<String> headerComments;
protected @Nullable String inlineComments;
protected @Nullable T defaultValue;
public ConfigValue(@Nullable ConfigurationProvider<?> provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
public ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) {
this.provider = provider;
this.configPath = configPath;
this.comments = comments;
this.headerComments = headerComments;
this.inlineComments = inlineComments;
this.defaultValue = defaultValue;
}
public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault,
@NotNull String configPath, @Nullable ConfigCommentInfo comments) {
public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault, @NotNull String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments) {
if (this.provider == null) this.provider = provider;
if (this.configPath == null) this.configPath = configPath;
if (this.comments == null) this.comments = comments;
if (this.headerComments == null) this.headerComments = headerComments;
if (this.inlineComments == null) this.inlineComments = inlineComments;
if (saveDefault) setDefault();
if (getComments() != null) this.provider.setComment(getConfigPath(), getComments());
if (getHeaderComments() != null) {
this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
}
if (getInlineComments() != null) {
this.provider.setInlineComment(getConfigPath(), getInlineComments());
}
}
public @Nullable T getDefaultValue() {
@@ -124,7 +136,7 @@ public abstract class ConfigValue<T> {
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
}
public final @NotNull ConfigurationWrapper getConfiguration() {
public final @NotNull ConfigurationWrapper<?> getConfiguration() {
try {
return getProvider().getConfiguration();
} catch (Exception ex) {
@@ -145,8 +157,13 @@ public abstract class ConfigValue<T> {
getConfiguration().set(getConfigPath(), value);
}
public @Nullable ConfigCommentInfo getComments() {
return comments;
public @Nullable String getInlineComments() {
return inlineComments;
}
@Unmodifiable
public @Nullable List<String> getHeaderComments() {
return headerComments;
}
}
@@ -1,11 +1,11 @@
package cc.carm.lib.configuration.core.value.impl;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
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;
import java.util.List;
public abstract class CachedConfigValue<T> extends ConfigValue<T> {
@@ -13,8 +13,9 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
protected long parsedTime = -1;
public CachedConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable ConfigCommentInfo comments, @Nullable T defaultValue) {
super(provider, sectionPath, comments, defaultValue);
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
}
protected T updateCache(T value) {
@@ -1,8 +1,8 @@
package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull;
@@ -17,17 +17,18 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
return builder().asList(valueClass);
}
@HeaderComment
protected final @NotNull Class<V> valueClass;
protected final @NotNull ConfigDataFunction<Object, V> parser;
protected final @NotNull ConfigDataFunction<V, Object> serializer;
public ConfiguredList(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfiguredList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
@NotNull ConfigDataFunction<Object, V> parser,
@NotNull ConfigDataFunction<V, Object> serializer) {
super(provider, sectionPath, comments, defaultValue);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass;
this.parser = parser;
this.serializer = serializer;
@@ -2,7 +2,6 @@ package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
@@ -10,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
@@ -32,14 +32,14 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
protected final @NotNull ConfigDataFunction<K, String> keySerializer;
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
public ConfiguredMap(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfiguredMap(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier,
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
@NotNull ConfigDataFunction<K, String> keySerializer,
@NotNull ConfigDataFunction<V, Object> valueSerializer) {
super(provider, sectionPath, comments, defaultValue);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.supplier = supplier;
this.keyClass = keyClass;
this.valueClass = valueClass;
@@ -78,7 +78,7 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
if (isExpired()) { // 已过时的数据,需要重新解析一次。
Map<K, V> map = supplier.get();
ConfigurationWrapper section = getConfiguration().getConfigurationSection(getConfigPath());
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return useOrDefault(map);
Set<String> keys = section.getKeys(false);
@@ -3,13 +3,13 @@ package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -21,15 +21,15 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
protected final @NotNull Class<V> valueClass;
protected final @NotNull ConfigValueParser<ConfigurationWrapper, V> parser;
protected final @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser;
protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable V defaultValue,
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
super(provider, sectionPath, comments, defaultValue);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass;
this.parser = parser;
this.serializer = serializer;
@@ -39,7 +39,7 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
return valueClass;
}
public @NotNull ConfigValueParser<ConfigurationWrapper, V> getParser() {
public @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> getParser() {
return parser;
}
@@ -50,7 +50,7 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
@Override
public @Nullable V get() {
if (isExpired()) { // 已过时的数据,需要重新解析一次。
ConfigurationWrapper section = getConfiguration().getConfigurationSection(getConfigPath());
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return useDefault();
try {
// 若未出现错误,则直接更新缓存并返回。
@@ -3,12 +3,12 @@ package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class ConfiguredValue<V> extends CachedConfigValue<V> {
@@ -30,13 +30,13 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
protected final @NotNull ConfigValueParser<Object, V> parser;
protected final @NotNull ConfigDataFunction<V, Object> serializer;
public ConfiguredValue(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfiguredValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable V defaultValue,
@NotNull ConfigValueParser<Object, V> parser,
@NotNull ConfigDataFunction<V, Object> serializer) {
super(provider, sectionPath, comments, defaultValue);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass;
this.parser = parser;
this.serializer = serializer;
+4 -2
View File
@@ -5,13 +5,15 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>2.3.0</version>
<version>3.2.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyconfiguration-json</artifactId>
<packaging>jar</packaging>
@@ -28,7 +30,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<version>2.10</version>
<scope>compile</scope>
</dependency>
@@ -1,7 +1,7 @@
package cc.carm.lib.configuration.json;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import com.google.gson.Gson;
@@ -48,7 +48,6 @@ public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
if (map == null) map = new LinkedHashMap<>();
this.configuration = new JSONConfigWrapper(map);
this.initializer = new ConfigInitializer<>(this);
}
@Override
@@ -57,10 +56,16 @@ public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
}
@Override
public void reload() {
protected void onReload() throws Exception {
super.reload();
initializeConfig();
}
@Override
public @Nullable ConfigurationComments getComments() {
return null;
}
@Override
public void save() throws Exception {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
@@ -68,17 +73,6 @@ public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
}
}
@Override
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment) {
// JSON doesn't support comments;
}
@Override
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
return null;
}
@Override
public @NotNull ConfigInitializer<? extends ConfigurationProvider<JSONConfigWrapper>> getInitializer() {
return this.initializer;
@@ -14,7 +14,7 @@ import java.util.Set;
*
* @author md_5, CarmJos
*/
public class JSONConfigWrapper implements ConfigurationWrapper {
public class JSONConfigWrapper implements ConfigurationWrapper<Map<String, Object>> {
private static final char SEPARATOR = '.';
protected final Map<String, Object> data;
@@ -33,6 +33,10 @@ public class JSONConfigWrapper implements ConfigurationWrapper {
}
}
@Override
public @NotNull Map<String, Object> getSource() {
return this.data;
}
@Override
public @NotNull Set<String> getKeys(boolean deep) {
@@ -94,7 +98,7 @@ public class JSONConfigWrapper implements ConfigurationWrapper {
}
@Override
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
public @Nullable JSONConfigWrapper getConfigurationSection(@NotNull String path) {
Object val = get(path);
return (val instanceof JSONConfigWrapper) ? (JSONConfigWrapper) val : null;
}
@@ -1,4 +0,0 @@
package cc.carm.lib.configuration.json;
public abstract class JSONValue {
}
@@ -8,7 +8,7 @@ import java.util.Map;
public class SomeModel extends AbstractModel {
int num;
public final int num;
public SomeModel(@NotNull String name, int num) {
super(name);
@@ -33,7 +33,7 @@ public class TestModel extends AbstractModel {
return map;
}
public static TestModel deserialize(ConfigurationWrapper section) {
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");
@@ -1,12 +1,12 @@
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.HeaderComment;
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;
@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
@HeaderComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
public class DatabaseConfiguration extends ConfigurationRoot {
@ConfigPath("driver")
+33
View File
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>3.2.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<artifactId>easyconfiguration-sql</artifactId>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-core</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,6 @@
package cc.carm.lib.configuration;
public class EasyConfiguration {
}
@@ -0,0 +1,38 @@
package cc.carm.lib.configuration.sql;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SQLConfigProvider extends ConfigurationProvider<SQLSectionWrapper> {
@Override
public @NotNull SQLSectionWrapper getConfiguration() {
return null;
}
@Override
public void save() throws Exception {
}
@Override
protected void onReload() throws Exception {
}
@Override
public @Nullable ConfigurationComments getComments() {
return null;
}
@Override
public @NotNull ConfigInitializer<? extends ConfigurationProvider<SQLSectionWrapper>> getInitializer() {
return null;
}
}
@@ -0,0 +1,63 @@
package cc.carm.lib.configuration.sql;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SQLSectionWrapper implements ConfigurationWrapper<Map<String, Object>> {
@Override
public @NotNull Map<String, Object> getSource() {
return null;
}
@Override
public @NotNull Set<String> getKeys(boolean deep) {
return null;
}
@Override
public @NotNull Map<String, Object> getValues(boolean deep) {
return null;
}
@Override
public void set(@NotNull String path, @Nullable Object value) {
}
@Override
public boolean contains(@NotNull String path) {
return false;
}
@Override
public @Nullable Object get(@NotNull String path) {
return null;
}
@Override
public boolean isList(@NotNull String path) {
return false;
}
@Override
public @Nullable List<?> getList(@NotNull String path) {
return null;
}
@Override
public boolean isConfigurationSection(@NotNull String path) {
return false;
}
@Override
public @Nullable SQLSectionWrapper getConfigurationSection(@NotNull String path) {
return null;
}
}
@@ -0,0 +1,7 @@
package cc.carm.lib.configuration.sql;
public class SQLValueParser {
}
+14
View File
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS conf
(
`namespace` VARCHAR(255) NOT NULL, #
`section` VARCHAR(255) NOT NULL, # (ConfigPath)
`type` VARCHAR(255) NOT NULL, # (Integer/Byte/List/Map/...)
`value` MEDIUMTEXT, # (JSON格式)
`inline_comments` TINYTEXT, #
`header_comments` TEXT, #
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`namespace`, `section`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
+4 -2
View File
@@ -5,13 +5,15 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>2.3.0</version>
<version>3.2.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyconfiguration-yaml</artifactId>
@@ -29,7 +31,7 @@
<dependency>
<groupId>org.bspfsystems</groupId>
<artifactId>yamlconfiguration</artifactId>
<version>1.0.11</version>
<version>1.3.0</version>
<scope>compile</scope>
</dependency>
@@ -30,5 +30,4 @@ public class EasyConfiguration {
return from(new File(fileName), source);
}
}
@@ -1,6 +1,6 @@
package cc.carm.lib.configuration.yaml;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
import org.bspfsystems.yamlconfiguration.file.FileConfiguration;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
@@ -9,93 +9,90 @@ import org.jetbrains.annotations.Nullable;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static cc.carm.lib.configuration.yaml.YAMLConfigProvider.SEPARATOR;
public class YAMLComments {
@SuppressWarnings("SpellCheckingInspection")
public class YAMLComments extends ConfigurationComments {
Map<String, ConfigCommentInfo> comments = new HashMap<>();
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
List<String> comments = getHeaderComment(path);
if (comments == null || comments.size() == 0) return null;
protected Map<String, ConfigCommentInfo> getComments() {
return comments;
}
public void set(@Nullable String path, @Nullable ConfigCommentInfo comments) {
if (comments == null) {
getComments().remove(path);
} else {
getComments().put(path, comments);
}
}
public @NotNull ConfigCommentInfo get(@Nullable String path) {
return getComments().getOrDefault(path, ConfigCommentInfo.defaults());
}
public @Nullable String buildComments(@NotNull String indents, @Nullable String path) {
ConfigCommentInfo comments = get(path);
if (!String.join("", comments.getComments()).isEmpty()) {
String prefix = comments.startWrap() ? "\n" : "";
String suffix = comments.endWrap() ? "\n" : "";
StringJoiner joiner = new StringJoiner("\n", prefix, suffix);
for (String comment : comments.getComments()) {
if (comment.length() == 0) joiner.add(" ");
else joiner.add(indents + "# " + comment);
}
return joiner + "\n";
} else {
return comments.startWrap() || comments.endWrap() ? "\n" : null;
StringJoiner joiner = new StringJoiner("\n");
for (String comment : comments) {
if (comment.length() == 0) joiner.add(" ");
else joiner.add(indents + "# " + comment);
}
return joiner + "\n";
}
/**
* 从一个文件读取配置并写入注释到某个写入器中。
* 该方法的源代码自 tchristofferson/ConfigUpdater 项目。
* 该方法的部分源代码借鉴自 tchristofferson/ConfigUpdater 项目。
*
* @param source 源配置文件
* @param writer 配置写入器
* @throws IOException 当写入发生错误时抛出
*/
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
FileConfiguration parserConfig = new YamlConfiguration();
FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容
String configHeader = buildHeaderComments(null, "");
if (configHeader != null) writer.write(configHeader);
for (String fullKey : source.getKeys(true)) {
String indents = getIndents(fullKey);
String comment = buildComments(indents, fullKey);
if (comment != null) writer.write(comment);
Object currentValue = source.get(fullKey);
String indents = getIndents(fullKey);
String headerComments = buildHeaderComments(fullKey, indents);
String inlineComment = getInlineComment(fullKey);
if (headerComments != null) writer.write(headerComments);
String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]");
String trailingKey = splitFullKey[splitFullKey.length - 1];
if (currentValue instanceof ConfigurationSection) {
ConfigurationSection section = (ConfigurationSection) currentValue;
writer.write(indents + trailingKey + ":");
if (!((ConfigurationSection) currentValue).getKeys(false).isEmpty())
if (inlineComment != null && inlineComment.length() > 0) {
writer.write(" # " + inlineComment);
}
if (!section.getKeys(false).isEmpty()) {
writer.write("\n");
else
} else {
writer.write(" {}\n");
if (indents.length() == 0) writer.write("\n");
}
continue;
}
parserConfig.set(trailingKey, currentValue);
String yaml = parserConfig.saveToString();
yaml = yaml.substring(0, yaml.length() - 1).replace("\n", "\n" + indents);
String toWrite = indents + yaml + "\n";
parserConfig.set(trailingKey, null);
writer.write(toWrite);
temp.set(trailingKey, currentValue);
String yaml = temp.saveToString();
temp.set(trailingKey, null);
yaml = yaml.substring(0, yaml.length() - 1);
if (inlineComment != null && inlineComment.length() > 0) {
if (yaml.contains("\n")) {
// section为多行内容,需要 InlineComment 加在首行末尾
String[] splitLine = yaml.split("\n", 2);
yaml = splitLine[0] + " # " + inlineComment + "\n" + splitLine[1];
} else {
// 其他情况下就直接加载后面就好。
yaml += " # " + inlineComment;
}
}
writer.write(indents + yaml.replace("\n", "\n" + indents) + "\n");
if (indents.length() == 0) writer.write("\n");
}
String endComment = buildComments("", null);
if (endComment != null) writer.write(endComment);
writer.close();
}
@@ -1,7 +1,7 @@
package cc.carm.lib.configuration.yaml;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
@@ -37,15 +37,20 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
}
@Override
public void reload() throws Exception {
protected void onReload() throws Exception {
configuration.load(getFile());
}
@Override
public @Nullable ConfigurationComments getComments() {
return this.comments;
}
@Override
@SuppressWarnings("SpellCheckingInspection")
public void save() throws Exception {
configuration.save(getFile());
// tchristofferson/ConfigUpdater start
StringWriter writer = new StringWriter();
this.comments.writeComments(configuration, new BufferedWriter(writer));
@@ -60,16 +65,6 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
}
@Override
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comments) {
this.comments.set(path, comments);
}
@Override
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
return this.comments.get(path);
}
@Override
public @NotNull ConfigInitializer<YAMLConfigProvider> getInitializer() {
return this.initializer;
@@ -12,7 +12,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
public class YAMLSectionWrapper implements ConfigurationWrapper {
public class YAMLSectionWrapper implements ConfigurationWrapper<ConfigurationSection> {
private final ConfigurationSection section;
@@ -25,6 +25,11 @@ public class YAMLSectionWrapper implements ConfigurationWrapper {
return section == null ? null : new YAMLSectionWrapper(section);
}
@Override
public @NotNull ConfigurationSection getSource() {
return this.section;
}
@Override
public @NotNull Set<String> getKeys(boolean deep) {
return new LinkedHashSet<>(section.getKeys(deep));
@@ -66,7 +71,7 @@ public class YAMLSectionWrapper implements ConfigurationWrapper {
}
@Override
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
public @Nullable YAMLSectionWrapper getConfigurationSection(@NotNull String path) {
return of(this.section.getConfigurationSection(path));
}
@@ -80,5 +85,5 @@ public class YAMLSectionWrapper implements ConfigurationWrapper {
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T defaultValue) {
return this.section.getSerializable(path, clazz, defaultValue);
}
}
@@ -1,22 +1,23 @@
package cc.carm.lib.configuration.yaml;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public abstract class YAMLValue<T> extends CachedConfigValue<T> {
public static @NotNull YAMLConfigBuilder builder() {
return new YAMLConfigBuilder();
}
public YAMLValue(@Nullable YAMLConfigProvider provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
public YAMLValue(@Nullable YAMLConfigProvider provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) {
super(provider, configPath, comments, defaultValue);
super(provider, configPath, headerComments, inlineComments, defaultValue);
}
public YAMLConfigProvider getYAMLProvider() {
@@ -21,7 +21,7 @@ public class SerializableBuilder<T extends ConfigurationSerializable>
@Override
public @NotNull ConfiguredSerializable<T> build() {
return new ConfiguredSerializable<>(this.provider, this.path, buildComments(), this.valueClass, this.defaultValue);
return new ConfiguredSerializable<>(this.provider, this.path, this.headerComments, this.inlineComment, this.valueClass, this.defaultValue);
}
@@ -1,12 +1,12 @@
package cc.carm.lib.configuration.yaml.value;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
import cc.carm.lib.configuration.yaml.YAMLValue;
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> {
@@ -22,10 +22,10 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
protected final @NotNull Class<T> valueClass;
public ConfiguredSerializable(@Nullable YAMLConfigProvider provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
public ConfiguredSerializable(@Nullable YAMLConfigProvider provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
super(provider, configPath, comments, defaultValue);
super(provider, configPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass;
}
@@ -16,7 +16,7 @@ import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ConfigTester {
public class DemoConfigTest {
static {
ConfigurationSerialization.registerClass(TestModel.class);
@@ -44,6 +44,12 @@ public class ConfigTester {
public static void testDemo() {
System.out.println("----------------------------------------------------");
System.out.println("Test Number: ");
System.out.println("before: " + DemoConfiguration.TEST_NUMBER.get());
DemoConfiguration.TEST_NUMBER.set((long) (Long.MAX_VALUE * Math.random()));
System.out.println("after: " + DemoConfiguration.TEST_NUMBER.get());
System.out.println("> Test Value:");
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
@@ -1,26 +0,0 @@
package config;
import config.offset.FieldOffset;
import config.offset.OffsetUtil;
import config.source.DemoConfiguration;
import org.junit.Test;
import java.util.List;
public class OffsetTest {
@Test
public void test() {
//
// output(OffsetUtil.getClassMemberOffset(DemoConfiguration.class));
// output(OffsetUtil.getClassMemberOffset(DemoConfiguration.Sub.class));
}
protected static void output(List<FieldOffset> fieldOffsets) {
for (FieldOffset fieldOffset : fieldOffsets) {
System.out.println(fieldOffset.getOffsetValue() + " -> " + fieldOffset);
}
}
}
@@ -11,7 +11,7 @@ import java.util.Map;
@SerializableAs("SomeModel")
public class SomeModel extends AbstractModel implements ConfigurationSerializable {
int num;
public final int num;
public SomeModel(@NotNull String name, int num) {
super(name);
@@ -38,7 +38,7 @@ public class TestModel extends AbstractModel implements ConfigurationSerializabl
return map;
}
public static TestModel deserialize(ConfigurationWrapper section) {
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");
@@ -1,63 +1,63 @@
package config.offset;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* @author Chris2018998
*/
public class OffsetUtil {
private static Unsafe unsafe;
static {
try {
// 获取 Unsafe 内部的私有的实例化单例对象
Field field = Unsafe.class.getDeclaredField("theUnsafe");
// 无视权限
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
//package config.offset;
//
//
//import sun.misc.Unsafe;
//
//import java.lang.reflect.Field;
//import java.lang.reflect.Modifier;
//import java.util.Collections;
//import java.util.LinkedList;
//import java.util.List;
//
///**
// * @author Chris2018998
// */
//public class OffsetUtil {
// private static Unsafe unsafe;
//
// static {
// try {
// unsafe = AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
// Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
// theUnsafe.setAccessible(true);
// return (Unsafe) theUnsafe.get(null);
// });
// } catch (Throwable e) {
// System.err.println("Unable to load unsafe");
// // 获取 Unsafe 内部的私有的实例化单例对象
// Field field = Unsafe.class.getDeclaredField("theUnsafe");
// // 无视权限
// field.setAccessible(true);
// unsafe = (Unsafe) field.get(null);
// } catch (NoSuchFieldException | IllegalAccessException e) {
// e.printStackTrace();
// }
}
public static List<FieldOffset> getClassMemberOffset(Class<?> beanClass) {
List<FieldOffset> offsetsList = new LinkedList<>();
for (Field field : beanClass.getDeclaredFields()) {
FieldOffset fieldOffset = new FieldOffset(field);
offsetsList.add(fieldOffset);
if (Modifier.isStatic(field.getModifiers()))
fieldOffset.setOffsetValue(unsafe.staticFieldOffset(field));
else
fieldOffset.setOffsetValue(unsafe.objectFieldOffset(field));
Class<?> fieldType = field.getType();
if (!fieldType.getName().startsWith("java")) {
Field[] subfields = fieldType.getDeclaredFields();
if (subfields.length > 0) {
fieldOffset.setSubFieldOffsetList(getClassMemberOffset(fieldType));
}
}
}
Collections.sort(offsetsList);
return offsetsList;
}
}
//// try {
//// unsafe = AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
//// Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
//// theUnsafe.setAccessible(true);
//// return (Unsafe) theUnsafe.get(null);
//// });
//// } catch (Throwable e) {
//// System.err.println("Unable to load unsafe");
//// }
// }
//
// public static List<FieldOffset> getClassMemberOffset(Class<?> beanClass) {
// List<FieldOffset> offsetsList = new LinkedList<>();
// for (Field field : beanClass.getDeclaredFields()) {
// FieldOffset fieldOffset = new FieldOffset(field);
// offsetsList.add(fieldOffset);
// if (Modifier.isStatic(field.getModifiers()))
// fieldOffset.setOffsetValue(unsafe.staticFieldOffset(field));
// else
// fieldOffset.setOffsetValue(unsafe.objectFieldOffset(field));
// Class<?> fieldType = field.getType();
// if (!fieldType.getName().startsWith("java")) {
// Field[] subfields = fieldType.getDeclaredFields();
// if (subfields.length > 0) {
// fieldOffset.setSubFieldOffsetList(getClassMemberOffset(fieldType));
// }
// }
// }
//
// Collections.sort(offsetsList);
// return offsetsList;
// }
//
//
//}
//
@@ -1,35 +1,29 @@
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.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
@HeaderComment({"", "数据库配置", " 用于提供数据库连接,进行数据库操作。"})
public class DatabaseConfiguration extends ConfigurationRoot {
@ConfigPath("driver")
@ConfigComment(value = {
@HeaderComment({
"数据库驱动配置,请根据数据库类型设置。",
"- MySQL: com.mysql.cj.jdbc.Driver",
"- MariaDB(推荐): org.mariadb.jdbc.Driver",
}, startWrap = false)
})
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
String.class, "com.mysql.cj.jdbc.Driver"
);
@ConfigComment(startWrap = false)
protected static final ConfigValue<String> HOST = ConfiguredValue.of(String.class, "127.0.0.1");
@ConfigComment(startWrap = false)
protected static final ConfigValue<Integer> PORT = ConfiguredValue.of(Integer.class, 3306);
@ConfigComment(startWrap = false)
protected static final ConfigValue<String> DATABASE = ConfiguredValue.of(String.class, "minecraft");
@ConfigComment(startWrap = false)
protected static final ConfigValue<String> USERNAME = ConfiguredValue.of(String.class, "root");
@ConfigComment(startWrap = false)
protected static final ConfigValue<String> PASSWORD = ConfiguredValue.of(String.class, "password");
@ConfigComment(startWrap = false)
protected static final ConfigValue<String> EXTRA = ConfiguredValue.of(String.class, "?useSSL=false");
protected static String buildJDBC() {
@@ -2,8 +2,9 @@ package config.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
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.annotation.HeaderComment;
import cc.carm.lib.configuration.core.annotation.InlineComment;
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.ConfiguredMap;
@@ -17,22 +18,23 @@ import java.util.Objects;
import java.util.UUID;
@ConfigComment({"给根类添加的注释将显示在文件的末尾。"})
@HeaderComment({
"此处内容将显示在配置文件的最上方",
""/*添加一个空行与其他配置的注释分割*/
})
public class DemoConfiguration extends ConfigurationRoot {
@ConfigPath(root = true)
@ConfigComment(value = {
"有时候,需要在配置文件最上面显示点东西,",
"此时就推荐添加一个可以用到但并不重要的参数到最上面",
"并给他添加对应的注释。"
}, startWrap = false, endWrap = true)
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
@ConfigPath(root = true)
public static final ConfigValue<Long> TEST_NUMBER = ConfiguredValue.of(Long.class, 1000000L);
// 支持通过 Class<?> 变量标注子配置,一并注册。
// 注意: 若对应类也有注解,则优先使用类的注解。
@ConfigPath("impl-test") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
@ConfigComment("Something...") // 支持给子路径直接打注释
@HeaderComment({"", "Something..."}) // 支持给子路径直接打注释
@InlineComment("InlineComments for class path")
public static final Class<?> IMPL = ImplConfiguration.class;
// 子配置文件
@@ -40,14 +42,15 @@ public class DemoConfiguration extends ConfigurationRoot {
public static final Class<?> DB_CONFIG = DatabaseConfiguration.class;
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
@ConfigComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
@HeaderComment({"", "Section类型数据测试"}) // 通过注解给配置添加注释。
@InlineComment("Section数据也支持InlineComment注释")
public static final ConfigValue<TestModel> MODEL_TEST = ConfiguredSection
.builder(TestModel.class)
.defaults(new TestModel("Carm", UUID.randomUUID()))
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
.serializeValue(TestModel::serialize).build();
@ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"})
@HeaderComment({"", "[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"})
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
.builder(Integer.class, UUID.class).fromString()
.parseKey(Integer::parseInt)
@@ -62,6 +65,7 @@ public class DemoConfiguration extends ConfigurationRoot {
public static class Sub extends ConfigurationRoot {
@ConfigPath(value = "uuid-value", root = true)
@InlineComment("This is an inline comment")
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
.builder(UUID.class).fromString()
.parseValue((data, defaultValue) -> UUID.fromString(data))
@@ -10,7 +10,9 @@ import config.model.TestModel;
@ConfigPath(root = true)
public class ImplConfiguration extends ConfigurationRoot {
public static final ConfigValue<? extends AbstractModel> TEST = ConfiguredSerializable.of(TestModel.class, TestModel.random());
public static final ConfigValue<? extends AbstractModel> TEST = ConfiguredSerializable.of(
TestModel.class, TestModel.random()
);
}
+6 -5
View File
@@ -15,11 +15,12 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<packaging>pom</packaging>
<version>2.3.0</version>
<version>3.2.1</version>
<modules>
<module>core</module>
<module>impl/yaml</module>
<module>impl/json</module>
<module>impl/sql</module>
</modules>
<name>EasyConfiguration</name>
@@ -90,7 +91,7 @@
<downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl>
<site>
<id>javadoc</id>
<name>EasyConfiguration JavaDoc (on Github Pages)</name>
<name>EasyConfiguration JavaDoc (on GitHub Pages)</name>
<url>https://CarmJos.github.io/EasyConfiguration</url>
</site>
</distributionManagement>
@@ -164,7 +165,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
<configuration>
<classifier>javadoc</classifier>
<detectJavaApiLink>false</detectJavaApiLink>
@@ -203,7 +204,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<version>3.3.0</version>
</plugin>
<plugin>
@@ -223,7 +224,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>