1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-05 02:58:20 +08:00

Compare commits

...

47 Commits

Author SHA1 Message Date
carm 6a007c5187 refactor(api): 🤖 修改部分构造方法名称(破坏性更新) 2023-05-20 09:37:15 +08:00
carm 43b00f2b69 refactor(api): 🤖 优化代码命名逻辑,补充部分Javadoc。 2023-05-20 09:34:30 +08:00
carm 2e61e66cdb refactor(api): 🤖 优化代码命名逻辑,补充部分Javadoc。 2023-05-20 09:21:49 +08:00
dependabot[bot] 39f946c28e chore(deps): Bump maven-gpg-plugin from 3.0.1 to 3.1.0 (#30)
Bumps [maven-gpg-plugin](https://github.com/apache/maven-gpg-plugin) from 3.0.1 to 3.1.0.
- [Commits](https://github.com/apache/maven-gpg-plugin/compare/maven-gpg-plugin-3.0.1...maven-gpg-plugin-3.1.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-05-09 00:24:43 +08:00
dependabot[bot] 25931ffd7e chore(deps): Bump maven-surefire-plugin from 3.0.0 to 3.1.0 (#29)
Bumps [maven-surefire-plugin](https://github.com/apache/maven-surefire) from 3.0.0 to 3.1.0.
- [Release notes](https://github.com/apache/maven-surefire/releases)
- [Commits](https://github.com/apache/maven-surefire/compare/surefire-3.0.0...surefire-3.1.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-05-09 00:12:17 +08:00
dependabot[bot] de103da879 chore(deps): Bump yamlconfiguration from 1.3.1 to 1.3.2 (#28)
Bumps [yamlconfiguration](https://github.com/bspfsystems/YamlConfiguration) from 1.3.1 to 1.3.2.
- [Release notes](https://github.com/bspfsystems/YamlConfiguration/releases)
- [Commits](https://github.com/bspfsystems/YamlConfiguration/compare/v1.3.1...v1.3.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>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-04-15 12:39:03 +08:00
dependabot[bot] 457c22d461 chore(deps): Bump maven-release-plugin from 2.5.3 to 3.0.0 (#27)
Bumps [maven-release-plugin](https://github.com/apache/maven-release) from 2.5.3 to 3.0.0.
- [Release notes](https://github.com/apache/maven-release/releases)
- [Commits](https://github.com/apache/maven-release/compare/maven-release-2.5.3...maven-release-3.0.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-22 11:57:50 +08:00
carm aa4225dbba build: 修改Java版本的标签名 2023-03-20 20:17:23 +08:00
carm ddd33154be chore(yaml): 独立 YAMLCommentWriter 项目 2023-03-20 20:11:23 +08:00
carm 727c26a2fb feat(value): 单独提出 ”ValueManifest“ 以解决每个实现类中初始参数复杂的问题,方便后续开发。 2023-03-15 22:24:08 +08:00
dependabot[bot] 9c95a16d90 chore(deps): Bump maven-surefire-plugin from 2.22.2 to 3.0.0 (#26)
Bumps [maven-surefire-plugin](https://github.com/apache/maven-surefire) from 2.22.2 to 3.0.0.
- [Release notes](https://github.com/apache/maven-surefire/releases)
- [Commits](https://github.com/apache/maven-surefire/compare/surefire-2.22.2...surefire-3.0.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-15 14:01:18 +08:00
dependabot[bot] 92c05f1a59 chore(deps): Bump annotations from 24.0.0 to 24.0.1 (#25)
Bumps [annotations](https://github.com/JetBrains/java-annotations) from 24.0.0 to 24.0.1.
- [Release notes](https://github.com/JetBrains/java-annotations/releases)
- [Changelog](https://github.com/JetBrains/java-annotations/blob/master/CHANGELOG.md)
- [Commits](https://github.com/JetBrains/java-annotations/commits)

---
updated-dependencies:
- dependency-name: org.jetbrains:annotations
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-05 12:09:12 +08:00
dependabot[bot] 739ed41885 chore(deps): Bump maven-compiler-plugin from 3.10.1 to 3.11.0 (#24)
Bumps [maven-compiler-plugin](https://github.com/apache/maven-compiler-plugin) from 3.10.1 to 3.11.0.
- [Release notes](https://github.com/apache/maven-compiler-plugin/releases)
- [Commits](https://github.com/apache/maven-compiler-plugin/compare/maven-compiler-plugin-3.10.1...maven-compiler-plugin-3.11.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-01 00:07:08 +08:00
carm a66da01996 style: improve code readability. 2023-02-19 19:54:08 +08:00
carm 6dc0447502 test(map): 添加新版ConfiguredMap测试 2023-02-19 17:19:39 +08:00
carm c49d904665 feat(value): 为ConfiguredList 与 ConfiguredMap 实现相关基础方法 2023-02-19 17:17:33 +08:00
冬花ice b756074ddc fix(list): 允许出现长度为0的List (#23) 2023-02-19 16:49:25 +08:00
dependabot[bot] 9e3dff3e95 chore(deps): Bump maven-javadoc-plugin from 3.4.1 to 3.5.0 (#22)
Bumps [maven-javadoc-plugin](https://github.com/apache/maven-javadoc-plugin) from 3.4.1 to 3.5.0.
- [Release notes](https://github.com/apache/maven-javadoc-plugin/releases)
- [Commits](https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.4.1...maven-javadoc-plugin-3.5.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-02-16 23:59:23 +08:00
carm fd01d9b7ef feat(builder): 为 ConfiguredMap 提供更便利的defaults方法。 2023-02-11 21:45:00 +08:00
carm 0f8383bbf3 Merge remote-tracking branch 'origin/master' 2023-01-16 19:26:13 +08:00
carm 1232c7c4da feat(builder): 提供Functional形式提供defaultValue的方法。 2023-01-16 19:26:07 +08:00
dependabot[bot] 7ac39da4e9 chore(deps): Bump annotations from 23.1.0 to 24.0.0 (#21)
Bumps [annotations](https://github.com/JetBrains/java-annotations) from 23.1.0 to 24.0.0.
- [Release notes](https://github.com/JetBrains/java-annotations/releases)
- [Changelog](https://github.com/JetBrains/java-annotations/blob/master/CHANGELOG.md)
- [Commits](https://github.com/JetBrains/java-annotations/compare/23.1.0...24.0.0)

---
updated-dependencies:
- dependency-name: org.jetbrains:annotations
  dependency-type: direct:production
  update-type: version-update:semver-major
...

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>
2023-01-12 22:16:19 +08:00
dependabot[bot] bf89f583db chore(deps): Bump gson from 2.10 to 2.10.1 (#20)
Bumps [gson](https://github.com/google/gson) from 2.10 to 2.10.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.10...gson-parent-2.10.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>
2023-01-10 01:02:32 +08:00
dependabot[bot] 03c69ba3a2 chore(deps): Bump yamlconfiguration from 1.3.0 to 1.3.1 (#19)
Bumps [yamlconfiguration](https://github.com/bspfsystems/YamlConfiguration) from 1.3.0 to 1.3.1.
- [Release notes](https://github.com/bspfsystems/YamlConfiguration/releases)
- [Commits](https://github.com/bspfsystems/YamlConfiguration/compare/v1.3.0...v1.3.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-12-30 23:07:40 +08:00
dependabot[bot] d9cbd1a283 chore(deps): Bump annotations from 23.0.0 to 23.1.0 (#18)
Bumps [annotations](https://github.com/JetBrains/java-annotations) from 23.0.0 to 23.1.0.
- [Release notes](https://github.com/JetBrains/java-annotations/releases)
- [Changelog](https://github.com/JetBrains/java-annotations/blob/master/CHANGELOG.md)
- [Commits](https://github.com/JetBrains/java-annotations/compare/23.0.0...23.1.0)

---
updated-dependencies:
- dependency-name: org.jetbrains:annotations
  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-12-12 22:49:54 +08:00
carm 96e90dd71b chore 2022-12-11 19:10:50 +08:00
carm a9f3d829bd build(javadoc): 修复Demo项目缺失的javadoc 2022-12-11 02:30:47 +08:00
carm 8eefba5159 feat(init): 支持针对于实体类的初始化 #9 2022-12-11 02:17:30 +08:00
carm 01e20df559 feat(init): 支持针对于实体类的初始化 #9 2022-12-11 02:04:23 +08:00
carm 35398ab741 feat(init): 支持针对于实体类的初始化 #9 2022-12-11 02:02:16 +08:00
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
68 changed files with 1639 additions and 1071 deletions
+1 -1
View File
@@ -30,7 +30,7 @@
### 示例代码 ### 示例代码
您可以 [点击这里](impl/yaml/src/test/java/config/source/DemoConfiguration.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。 您可以 [点击这里](demo/src/main/java/cc/carm/lib/configuration/demo/DatabaseConfiguration.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。
### 依赖方式 ### 依赖方式
+5 -3
View File
@@ -5,12 +5,14 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.1.0</version> <version>3.6.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties> </properties>
<artifactId>easyconfiguration-core</artifactId> <artifactId>easyconfiguration-core</artifactId>
@@ -46,7 +46,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
*/ */
public void initialize(@NotNull Class<? extends ConfigurationRoot> clazz, public void initialize(@NotNull Class<? extends ConfigurationRoot> clazz,
boolean saveDefaults, boolean loadSubClasses) { boolean saveDefaults, boolean loadSubClasses) {
initializeClass( initializeStaticClass(
clazz, null, null, clazz, null, null,
null, null, null, null, null, null,
saveDefaults, loadSubClasses saveDefaults, loadSubClasses
@@ -60,12 +60,61 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
} }
} }
protected void initializeClass(@NotNull Class<?> clazz, /**
@Nullable String parentPath, @Nullable String fieldName, * 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
@Nullable ConfigPath fieldPath, *
@Nullable HeaderComment fieldHeaderComments, * @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
@Nullable InlineComment fieldInlineComments, */
boolean saveDefaults, boolean loadSubClasses) { public void initialize(@NotNull ConfigurationRoot config) {
initialize(config, true);
}
/**
* 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
*
* @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
* @param saveDefaults 是否写入默认值(默认为 true)。
*/
public void initialize(@NotNull ConfigurationRoot config, boolean saveDefaults) {
initializeInstance(
config, null, null,
null, null, null,
saveDefaults
);
if (saveDefaults) {
try {
provider.save();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 针对实例类的初始化方法
private void initializeInstance(@NotNull ConfigurationRoot root,
@Nullable String parentPath, @Nullable String fieldName,
@Nullable ConfigPath fieldPath,
@Nullable HeaderComment fieldHeaderComments,
@Nullable InlineComment fieldInlineComments,
boolean saveDefaults) {
String path = getClassPath(root.getClass(), parentPath, fieldName, fieldPath);
this.provider.setHeaderComment(path, getClassHeaderComments(root.getClass(), fieldHeaderComments));
if (path != null) this.provider.setInlineComment(path, readInlineComments(fieldInlineComments));
for (Field field : root.getClass().getDeclaredFields()) {
initializeField(root, field, path, saveDefaults, false);
}
}
// 针对静态类的初始化方法
private void initializeStaticClass(@NotNull Class<?> clazz,
@Nullable String parentPath, @Nullable String fieldName,
@Nullable ConfigPath fieldPath,
@Nullable HeaderComment fieldHeaderComments,
@Nullable InlineComment fieldInlineComments,
boolean saveDefaults, boolean loadSubClasses) {
if (!ConfigurationRoot.class.isAssignableFrom(clazz)) return; // 只解析继承了 ConfigurationRoot 的类
String path = getClassPath(clazz, parentPath, fieldName, fieldPath); String path = getClassPath(clazz, parentPath, fieldName, fieldPath);
this.provider.setHeaderComment(path, getClassHeaderComments(clazz, fieldHeaderComments)); this.provider.setHeaderComment(path, getClassHeaderComments(clazz, fieldHeaderComments));
if (path != null) this.provider.setInlineComment(path, readInlineComments(fieldInlineComments)); if (path != null) this.provider.setInlineComment(path, readInlineComments(fieldInlineComments));
@@ -77,7 +126,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
if (!loadSubClasses) return; if (!loadSubClasses) return;
Class<?>[] classes = clazz.getDeclaredClasses(); Class<?>[] classes = clazz.getDeclaredClasses();
for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。 for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。
initializeClass( initializeStaticClass(
classes[i], path, classes[i].getSimpleName(), classes[i], path, classes[i].getSimpleName(),
null, null, null, null, null, null,
saveDefaults, true saveDefaults, true
@@ -85,11 +134,12 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
} }
} }
private void initializeField(@NotNull Class<?> source, @NotNull Field field, @Nullable String parent, private void initializeField(@NotNull Object source, @NotNull Field field,
boolean saveDefaults, boolean loadSubClasses) { @Nullable String parent, boolean saveDefaults, boolean loadSubClasses) {
try { try {
field.setAccessible(true); field.setAccessible(true);
Object object = field.get(source); Object object = field.get(source);
if (object instanceof ConfigValue<?>) { if (object instanceof ConfigValue<?>) {
initializeValue( initializeValue(
(ConfigValue<?>) object, getFieldPath(field, parent), (ConfigValue<?>) object, getFieldPath(field, parent),
@@ -97,8 +147,18 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
field.getAnnotation(InlineComment.class), field.getAnnotation(InlineComment.class),
saveDefaults saveDefaults
); );
} else if (object instanceof Class<?>) { } else if (source instanceof ConfigurationRoot && object instanceof ConfigurationRoot) {
initializeClass( // 当且仅当 源字段与字段 均为ConfigurationRoot实例时,才对目标字段进行下一步初始化加载。
initializeInstance(
(ConfigurationRoot) object, parent, field.getName(),
field.getAnnotation(ConfigPath.class),
field.getAnnotation(HeaderComment.class),
field.getAnnotation(InlineComment.class),
saveDefaults
);
} else if (source instanceof Class<?> && object instanceof Class<?>) {
// 当且仅当 源字段与字段 均为静态类时,才对目标字段进行下一步初始化加载。
initializeStaticClass(
(Class<?>) object, parent, field.getName(), (Class<?>) object, parent, field.getName(),
field.getAnnotation(ConfigPath.class), field.getAnnotation(ConfigPath.class),
field.getAnnotation(HeaderComment.class), field.getAnnotation(HeaderComment.class),
@@ -106,6 +166,11 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
saveDefaults, loadSubClasses saveDefaults, loadSubClasses
); );
} }
// 以上判断实现以下规范:
// - 实例类中仅加载 ConfigValue实例 与 ConfigurationRoot实例
// - 静态类中仅加载 静态ConfigValue实例 与 静态ConfigurationRoot类
} catch (IllegalAccessException ignored) { } catch (IllegalAccessException ignored) {
} }
} }
@@ -142,7 +207,7 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
return value.length() > 0 ? value : null; return value.length() > 0 ? value : null;
} }
protected static @Nullable String getClassPath(@NotNull Class<?> clazz, protected static @Nullable String getClassPath(@Nullable Class<?> clazz,
@Nullable String parentPath, @Nullable String parentPath,
@Nullable String filedName, @Nullable String filedName,
@Nullable ConfigPath fieldAnnotation) { @Nullable ConfigPath fieldAnnotation) {
@@ -150,11 +215,13 @@ public class ConfigInitializer<T extends ConfigurationProvider<?>> {
boolean fromRoot = false; boolean fromRoot = false;
// 先获取 Class 对应的路径注解 其优先度最高。 // 先获取 Class 对应的路径注解 其优先度最高。
ConfigPath clazzAnnotation = clazz.getAnnotation(ConfigPath.class); if (clazz != null) {
if (clazzAnnotation != null) { ConfigPath clazzAnnotation = clazz.getAnnotation(ConfigPath.class);
fromRoot = clazzAnnotation.root(); if (clazzAnnotation != null) {
if (clazzAnnotation.value().length() > 0) { fromRoot = clazzAnnotation.root();
return (fromRoot ? "" : parent) + clazzAnnotation.value(); if (clazzAnnotation.value().length() > 0) {
return (fromRoot ? "" : parent) + clazzAnnotation.value();
}
} }
} }
@@ -1,12 +1,14 @@
package cc.carm.lib.configuration.core.builder; package cc.carm.lib.configuration.core.builder;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.ConfigValue; 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;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T, B, P>, P extends ConfigurationProvider<?>> { public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T, B, P>, P extends ConfigurationProvider<?>> {
@@ -61,4 +63,15 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
return getThis(); return getThis();
} }
public @NotNull B defaults(@NotNull Supplier<@Nullable T> defaultValueSupplier) {
return defaults(defaultValueSupplier.get());
}
protected @NotNull ValueManifest<T> buildManifest() {
return ValueManifest.of(
this.provider, this.path,
this.headerComments, this.inlineComment, this.defaultValue
);
}
} }
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.core.builder;
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder; import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder; import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
import cc.carm.lib.configuration.core.builder.map.ConfigMapCreator;
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder; import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -19,24 +20,24 @@ public class ConfigBuilder {
return new ConfigListBuilder<>(valueClass); return new ConfigListBuilder<>(valueClass);
} }
public <K, V> @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> asMap(@NotNull Class<K> keyClass, public <K, V> @NotNull ConfigMapCreator<K, V> asMap(@NotNull Class<K> keyClass,
@NotNull Class<V> valueClass) { @NotNull Class<V> valueClass) {
return new ConfigMapBuilder<>(LinkedHashMap::new, keyClass, valueClass); return new ConfigMapCreator<>(keyClass, valueClass);
} }
public <K, V> @NotNull ConfigMapBuilder<HashMap<K, V>, K, V> asHashMap(@NotNull Class<K> keyClass, public <K, V> @NotNull ConfigMapBuilder<HashMap<K, V>, K, V> asHashMap(@NotNull Class<K> keyClass,
@NotNull Class<V> valueClass) { @NotNull Class<V> valueClass) {
return asMap(keyClass, valueClass).supplier(HashMap::new); return asMap(keyClass, valueClass).asHashMap();
} }
public <K, V> @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> asLinkedMap(@NotNull Class<K> keyClass, public <K, V> @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> asLinkedMap(@NotNull Class<K> keyClass,
@NotNull Class<V> valueClass) { @NotNull Class<V> valueClass) {
return asMap(keyClass, valueClass); return asMap(keyClass, valueClass).asLinkedMap();
} }
public <K extends Comparable<K>, V> @NotNull ConfigMapBuilder<TreeMap<K, V>, K, V> asTreeMap(@NotNull Class<K> keyClass, public <K extends Comparable<K>, V> @NotNull ConfigMapBuilder<TreeMap<K, V>, K, V> asTreeMap(@NotNull Class<K> keyClass,
@NotNull Class<V> valueClass) { @NotNull Class<V> valueClass) {
return asMap(keyClass, valueClass).supplier(TreeMap::new); return asMap(keyClass, valueClass).asTreeMap();
} }
} }
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> { public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> {
@@ -37,6 +38,10 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
return defaults(new ArrayList<>(Arrays.asList(values))); return defaults(new ArrayList<>(Arrays.asList(values)));
} }
public final @NotNull SourceListBuilder<S, V> defaults(@NotNull Collection<V> values) {
return defaults(new ArrayList<>(values));
}
public @NotNull SourceListBuilder<S, V> parseSource(ConfigDataFunction<Object, S> sourceParser) { public @NotNull SourceListBuilder<S, V> parseSource(ConfigDataFunction<Object, S> sourceParser) {
this.sourceParser = sourceParser; this.sourceParser = sourceParser;
return this; return this;
@@ -65,9 +70,7 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
@Override @Override
public @NotNull ConfiguredList<V> build() { public @NotNull ConfiguredList<V> build() {
return new ConfiguredList<>( return new ConfiguredList<>(
this.provider, this.path, buildManifest(), this.valueClass,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.sourceParser.andThen(this.valueParser), this.sourceParser.andThen(this.valueParser),
this.valueSerializer.andThen(sourceSerializer) this.valueSerializer.andThen(sourceSerializer)
); );
@@ -0,0 +1,37 @@
package cc.carm.lib.configuration.core.builder.map;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;
public class ConfigMapCreator<K, V> {
protected final @NotNull Class<K> keyClass;
protected final @NotNull Class<V> valueClass;
public ConfigMapCreator(@NotNull Class<K> keyClass, @NotNull Class<V> valueClass) {
this.keyClass = keyClass;
this.valueClass = valueClass;
}
public <M extends Map<K, V>> @NotNull ConfigMapBuilder<M, K, V> asMap(Supplier<M> mapSuppler) {
return new ConfigMapBuilder<>(mapSuppler, keyClass, valueClass);
}
public @NotNull ConfigMapBuilder<HashMap<K, V>, K, V> asHashMap() {
return asMap(HashMap::new);
}
public @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> asLinkedMap() {
return asMap(LinkedHashMap::new);
}
public @NotNull ConfigMapBuilder<TreeMap<K, V>, K, V> asTreeMap() {
return asMap(TreeMap::new);
}
}
@@ -2,10 +2,12 @@ package cc.carm.lib.configuration.core.builder.map;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder; 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.ValueManifest;
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;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfigBuilder<M, SourceMapBuilder<M, S, K, V>> { public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfigBuilder<M, SourceMapBuilder<M, S, K, V>> {
@@ -51,6 +53,12 @@ public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfig
); );
} }
public @NotNull SourceMapBuilder<M, S, K, V> defaults(@NotNull Consumer<M> factory) {
M map = supplier.get();
factory.accept(map);
return defaults(map);
}
public @NotNull SourceMapBuilder<M, S, K, V> parseKey(@NotNull ConfigDataFunction<String, K> parser) { public @NotNull SourceMapBuilder<M, S, K, V> parseKey(@NotNull ConfigDataFunction<String, K> parser) {
this.keyParser = parser; this.keyParser = parser;
return this; return this;
@@ -89,10 +97,8 @@ public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfig
@Override @Override
public @NotNull ConfiguredMap<K, V> build() { public @NotNull ConfiguredMap<K, V> build() {
return new ConfiguredMap<>( return new ConfiguredMap<>(
this.provider, this.path, new ValueManifest<>(provider, path, headerComments, inlineComment, defaultValue),
this.headerComments, this.inlineComment, this.supplier, this.keyClass, this.keyParser,
this.defaultValue, this.supplier,
this.keyClass, this.keyParser,
this.valueClass, this.sourceParser.andThen(this.valueParser), this.valueClass, this.sourceParser.andThen(this.valueParser),
this.keySerializer, this.valueSerializer.andThen(this.sourceSerializer) this.keySerializer, this.valueSerializer.andThen(this.sourceSerializer)
); );
@@ -19,7 +19,7 @@ public class ConfigValueBuilder<V> {
return fromSection(ConfigValueParser.required(), ConfigDataFunction.required()); 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) { @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> valueSerializer) {
return new SectionValueBuilder<>(this.valueClass, valueParser, valueSerializer); return new SectionValueBuilder<>(this.valueClass, valueParser, valueSerializer);
} }
@@ -15,11 +15,11 @@ public class SectionValueBuilder<V>
protected final @NotNull Class<V> valueClass; 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; protected @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
public SectionValueBuilder(@NotNull Class<V> valueClass, public SectionValueBuilder(@NotNull Class<V> valueClass,
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser, @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) { @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
this.valueClass = valueClass; this.valueClass = valueClass;
this.parser = parser; this.parser = parser;
@@ -32,7 +32,7 @@ public class SectionValueBuilder<V>
return this; return this;
} }
public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper, V> valueParser) { public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper<?>, V> valueParser) {
this.parser = valueParser; this.parser = valueParser;
return this; return this;
} }
@@ -44,12 +44,7 @@ public class SectionValueBuilder<V>
@Override @Override
public @NotNull ConfiguredSection<V> build() { public @NotNull ConfiguredSection<V> build() {
return new ConfiguredSection<>( return new ConfiguredSection<>(buildManifest(), this.valueClass, this.parser, this.serializer);
this.provider, this.path,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.parser, this.serializer
);
} }
} }
@@ -57,9 +57,7 @@ public class SourceValueBuilder<S, V> extends CommonConfigBuilder<V, SourceValue
@Override @Override
public @NotNull ConfiguredValue<V> build() { public @NotNull ConfiguredValue<V> build() {
return new ConfiguredValue<>( return new ConfiguredValue<>(
this.provider, this.path, buildManifest(), this.valueClass,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.valueParser.compose(this.sourceParser), this.valueParser.compose(this.sourceParser),
this.valueSerializer.andThen(sourceSerializer) this.valueSerializer.andThen(sourceSerializer)
); );
@@ -62,6 +62,94 @@ public interface ConfigDataFunction<T, R> {
}; };
} }
@Contract(pure = true)
static <V> @NotNull ConfigDataFunction<String, V> parseString(Class<V> valueClass) {
return (input) -> {
if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Integer> intValue() {
return (input) -> {
if (input instanceof Integer) {
return (Integer) input;
} else if (input instanceof Number) {
return ((Number) input).intValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Integer.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Short> shortValue() {
return (input) -> {
if (input instanceof Short) {
return (Short) input;
} else if (input instanceof Number) {
return ((Number) input).shortValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Short.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Double> doubleValue() {
return (input) -> {
if (input instanceof Double) {
return (Double) input;
} else if (input instanceof Number) {
return ((Number) input).doubleValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Double.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Byte> byteValue() {
return (input) -> {
if (input instanceof Byte) {
return (Byte) input;
} else if (input instanceof Number) {
return ((Number) input).byteValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Byte.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Float> floatValue() {
return (input) -> {
if (input instanceof Float) {
return (Float) input;
} else if (input instanceof Number) {
return ((Number) input).floatValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Float.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Long> longValue() {
return (input) -> {
if (input instanceof Long) {
return (Long) input;
} else if (input instanceof Number) {
return ((Number) input).longValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Long.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Boolean> booleanValue() {
return (input) -> {
if (input instanceof Boolean) {
return (Boolean) input;
} else if (input instanceof String) {
String s = (String) input;
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
} else if (input instanceof Integer) {
return ((Integer) input) == 1;
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
};
}
} }
@@ -50,6 +50,25 @@ public interface ConfigValueParser<T, R> {
@Contract(pure = true) @Contract(pure = true)
static <V> @NotNull ConfigValueParser<Object, V> castObject(Class<V> valueClass) { static <V> @NotNull ConfigValueParser<Object, V> castObject(Class<V> valueClass) {
return (input, defaultValue) -> { return (input, defaultValue) -> {
if (Number.class.isAssignableFrom(valueClass)) {
if (Long.class.isAssignableFrom(valueClass)) {
input = longValue().parse(input, (Long) defaultValue);
} else if (Integer.class.isAssignableFrom(valueClass)) {
input = intValue().parse(input, (Integer) defaultValue);
} else if (Float.class.isAssignableFrom(valueClass)) {
input = floatValue().parse(input, (Float) defaultValue);
} else if (Double.class.isAssignableFrom(valueClass)) {
input = doubleValue().parse(input, (Double) defaultValue);
} else if (Byte.class.isAssignableFrom(valueClass)) {
input = byteValue().parse(input, (Byte) defaultValue);
} else if (Short.class.isAssignableFrom(valueClass)) {
input = shortValue().parse(input, (Short) defaultValue);
}
} else if (Boolean.class.isAssignableFrom(valueClass)) {
input = booleanValue().parse(input, (Boolean) defaultValue);
}
if (valueClass.isInstance(input)) return valueClass.cast(input); if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName()); else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName());
}; };
@@ -71,92 +90,39 @@ public interface ConfigValueParser<T, R> {
}; };
} }
@Contract(pure = true)
static @NotNull <T> ConfigValueParser<T, String> castToString(Class<T> clazz) {
return (input, defaultValue) -> {
if (input instanceof String) return (String) input;
else return input.toString();
};
}
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Integer> intValue() { static @NotNull ConfigValueParser<Object, Integer> intValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.intValue().parse(input);
if (input instanceof Integer) {
return (Integer) input;
} else if (input instanceof Number) {
return ((Number) input).intValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Integer.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Short> shortValue() { static @NotNull ConfigValueParser<Object, Short> shortValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.shortValue().parse(input);
if (input instanceof Short) {
return (Short) input;
} else if (input instanceof Number) {
return ((Number) input).shortValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Short.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Double> doubleValue() { static @NotNull ConfigValueParser<Object, Double> doubleValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.doubleValue().parse(input);
if (input instanceof Double) {
return (Double) input;
} else if (input instanceof Number) {
return ((Number) input).doubleValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Double.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Byte> byteValue() { static @NotNull ConfigValueParser<Object, Byte> byteValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.byteValue().parse(input);
if (input instanceof Byte) {
return (Byte) input;
} else if (input instanceof Number) {
return ((Number) input).byteValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Byte.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Float> floatValue() { static @NotNull ConfigValueParser<Object, Float> floatValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.floatValue().parse(input);
if (input instanceof Float) {
return (Float) input;
} else if (input instanceof Number) {
return ((Number) input).floatValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Float.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Long> longValue() { static @NotNull ConfigValueParser<Object, Long> longValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.longValue().parse(input);
if (input instanceof Long) {
return (Long) input;
} else if (input instanceof Number) {
return ((Number) input).longValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Long.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Boolean> booleanValue() { static @NotNull ConfigValueParser<Object, Boolean> booleanValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.booleanValue().parse(input);
if (input instanceof Boolean) {
return (Boolean) input;
} else if (input instanceof String) {
String s = (String) input;
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
} else if (input instanceof Integer) {
return ((Integer) input) == 1;
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
};
} }
} }
@@ -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 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);
}
}
@@ -2,61 +2,149 @@ package cc.carm.lib.configuration.core.source;
import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.value.ConfigValue;
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;
import org.jetbrains.annotations.Unmodifiable; import org.jetbrains.annotations.Unmodifiable;
import java.util.List; import java.util.List;
import java.util.Optional;
public abstract class ConfigurationProvider<W extends ConfigurationWrapper> { /**
* 配置文件提供者,用于为 {@link ConfigValue} 提供配置文件的源,以便实现读取、保存等操作。
*
* @param <W> 配置文件的原生功能类
*/
public abstract class ConfigurationProvider<W extends ConfigurationWrapper<?>> {
protected long updateTime; protected long updateTime;
public ConfigurationProvider() { protected ConfigurationProvider() {
this.updateTime = System.currentTimeMillis(); this.updateTime = System.currentTimeMillis();
} }
/**
* 得到配置文件的更新(最后加载)时间。
*
* @return 更新时间
*/
public long getUpdateTime() { public long getUpdateTime() {
return updateTime; return updateTime;
} }
/**
* 用于 {@link CachedConfigValue} 判断缓存值是否过期(即缓存的时间早于配置文件的最后加载时间)。
*
* @param time 缓存值时的时间戳
* @return 缓存值是否过期
*/
public boolean isExpired(long time) { public boolean isExpired(long time) {
return this.updateTime > time; return getUpdateTime() > time;
} }
/**
* 得到配置文件的原生功能类。
*
* @return 原生类
*/
public abstract @NotNull W getConfiguration(); public abstract @NotNull W getConfiguration();
/**
* 重载当前配置文件。(将不会保存已修改的内容)
*
* @throws Exception 当重载出现错误时抛出
*/
public void reload() throws Exception { public void reload() throws Exception {
onReload(); // 调用重写的Reload方法 onReload(); // 调用重写的Reload方法
this.updateTime = System.currentTimeMillis(); this.updateTime = System.currentTimeMillis();
} }
/**
* 将当前对配置文件的更改进行保存。
*
* @throws Exception 当保存出现错误时抛出
*/
public abstract void save() throws Exception; public abstract void save() throws Exception;
/**
* 针对于不同配置文件类型所执行的重载操作。
*
* @throws Exception 当操作出现错误时抛出。
*/
protected abstract void onReload() throws Exception; protected abstract void onReload() throws Exception;
public abstract void setHeaderComment(@Nullable String path, @Nullable List<String> comments); public abstract @Nullable ConfigurationComments getComments();
public abstract void setInlineComment(@NotNull String path, @Nullable String comment); 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 @Nullable
@Unmodifiable @Unmodifiable
public abstract List<String> getHeaderComment(@Nullable String path); public List<String> getHeaderComment(@Nullable String path) {
return Optional.ofNullable(getComments()).map(c -> c.getHeaderComment(path)).orElse(null);
}
public abstract @Nullable String getInlineComment(@NotNull String path); 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(); public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();
/**
* 初始化指定类以及其子类的所有 {@link ConfigValue} 对象。
*
* @param configClazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
*/
public void initialize(Class<? extends ConfigurationRoot> configClazz) { public void initialize(Class<? extends ConfigurationRoot> configClazz) {
initialize(configClazz, true); initialize(configClazz, true);
} }
/**
* 初始化指定类以及其子类的所有 {@link ConfigValue} 对象。
*
* @param configClazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
* @param saveDefaults 是否写入默认值(默认为 true)。
*/
public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults) { public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults) {
getInitializer().initialize(configClazz, saveDefaults); this.getInitializer().initialize(configClazz, saveDefaults);
} }
/**
* 初始化指定类的所有 {@link ConfigValue} 对象。
*
* @param configClazz 配置文件类,须继承于 {@link ConfigurationRoot} 。
* @param saveDefaults 是否写入默认值(默认为 true)。
* @param loadSubClasses 是否加载内部子类(默认为 true)。
*/
public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults, boolean loadSubClasses) { public void initialize(Class<? extends ConfigurationRoot> configClazz, boolean saveDefaults, boolean loadSubClasses) {
getInitializer().initialize(configClazz, saveDefaults, loadSubClasses); this.getInitializer().initialize(configClazz, saveDefaults, loadSubClasses);
}
/**
* 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
*
* @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
*/
public void initialize(@NotNull ConfigurationRoot config) {
this.getInitializer().initialize(config, true);
}
/**
* 初始化指定实例的所有 {@link ConfigValue} 与内部 {@link ConfigurationRoot} 对象。
*
* @param config 配置文件实例类,须实现 {@link ConfigurationRoot} 。
* @param saveDefaults 是否写入默认值(默认为 true)。
*/
public void initialize(@NotNull ConfigurationRoot config, boolean saveDefaults) {
this.getInitializer().initialize(config, saveDefaults);
} }
} }
@@ -12,7 +12,7 @@ import java.util.List;
interface ConfigurationReader { interface ConfigurationReader {
ConfigurationWrapper getWrapper(); ConfigurationWrapper<?> getWrapper();
default boolean isBoolean(@NotNull String path) { default boolean isBoolean(@NotNull String path) {
return getWrapper().isType(path, Boolean.class); return getWrapper().isType(path, Boolean.class);
@@ -9,13 +9,15 @@ 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<S> extends ConfigurationReader {
@Override @Override
default ConfigurationWrapper getWrapper() { default ConfigurationWrapper<S> getWrapper() {
return this; return this;
} }
@NotNull S getSource();
@NotNull @NotNull
Set<String> getKeys(boolean deep); Set<String> getKeys(boolean deep);
@@ -66,6 +68,6 @@ public interface ConfigurationWrapper extends ConfigurationReader {
boolean isConfigurationSection(@NotNull String path); boolean isConfigurationSection(@NotNull String path);
@Nullable @Nullable
ConfigurationWrapper getConfigurationSection(@NotNull String path); ConfigurationWrapper<S> getConfigurationSection(@NotNull String path);
} }
@@ -10,11 +10,11 @@ import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Objects; 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; protected final @NotNull File file;
public FileConfigProvider(@NotNull File file) { protected FileConfigProvider(@NotNull File file) {
this.file = file; this.file = file;
} }
@@ -2,62 +2,42 @@ package cc.carm.lib.configuration.core.value;
import cc.carm.lib.configuration.core.builder.ConfigBuilder; import cc.carm.lib.configuration.core.builder.ConfigBuilder;
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;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
public abstract class ConfigValue<T> { public abstract class ConfigValue<T> extends ValueManifest<T> {
public static @NotNull ConfigBuilder builder() { public static @NotNull ConfigBuilder builder() {
return new ConfigBuilder(); return new ConfigBuilder();
} }
protected @Nullable ConfigurationProvider<?> provider; protected ConfigValue(@NotNull ValueManifest<T> manifest) {
protected @Nullable String configPath; super(manifest.provider, manifest.configPath, manifest.headerComments, manifest.inlineComment, manifest.defaultValue);
}
protected @Nullable List<String> headerComments; /**
protected @Nullable String inlineComments; * @param provider 配置文件提供者
* @param configPath 配置路径
protected @Nullable T defaultValue; * @param headerComments 头部注释内容
* @param inlineComments 行内注释内容
public ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath, * @param defaultValue 默认参数值
@Nullable List<String> headerComments, @Nullable String inlineComments, * @deprecated 请使用 {@link #ConfigValue(ValueManifest)} 构造器。
@Nullable T defaultValue) { */
this.provider = provider; @Deprecated
this.configPath = configPath; protected ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
this.headerComments = headerComments; @Nullable List<String> headerComments, @Nullable String inlineComments,
this.inlineComments = inlineComments; @Nullable T defaultValue) {
this.defaultValue = defaultValue; super(provider, configPath, headerComments, inlineComments, defaultValue);
} }
public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault, @NotNull String configPath, public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault, @NotNull String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments) { @Nullable List<String> headerComments, @Nullable String inlineComments) {
if (this.provider == null) this.provider = provider; this.initialize(provider, configPath, headerComments, inlineComments);
if (this.configPath == null) this.configPath = configPath;
if (this.headerComments == null) this.headerComments = headerComments;
if (this.inlineComments == null) this.inlineComments = inlineComments;
if (saveDefault) setDefault(); if (saveDefault) setDefault();
if (getHeaderComments() != null) {
this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
}
if (getInlineComments() != null) {
this.provider.setInlineComment(getConfigPath(), getInlineComments());
}
}
public @Nullable T getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
} }
/** /**
@@ -131,39 +111,4 @@ public abstract class ConfigValue<T> {
else return false; else return false;
} }
public @NotNull ConfigurationProvider<?> getProvider() {
return Optional.ofNullable(this.provider)
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
}
public final @NotNull ConfigurationWrapper getConfiguration() {
try {
return getProvider().getConfiguration();
} catch (Exception ex) {
throw new IllegalStateException("Value(" + configPath + ") has not been initialized", ex);
}
}
public @NotNull String getConfigPath() {
return Optional.ofNullable(this.configPath)
.orElseThrow(() -> new IllegalStateException("No section path provided."));
}
protected Object getValue() {
return getConfiguration().get(getConfigPath());
}
protected void setValue(@Nullable Object value) {
getConfiguration().set(getConfigPath(), value);
}
public @Nullable String getInlineComments() {
return inlineComments;
}
@Unmodifiable
public @Nullable List<String> getHeaderComments() {
return headerComments;
}
} }
@@ -0,0 +1,125 @@
package cc.carm.lib.configuration.core.value;
import cc.carm.lib.configuration.core.ConfigInitializer;
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.Optional;
/**
* 配置值描述清单。用于描述一个配置值的相关基础信息。
*
* @param <T> 配置值类型
* @author CarmJos
*/
public class ValueManifest<T> {
public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments) {
return new ValueManifest<>(provider, configPath, headerComments, inlineComments, null);
}
public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable V defaultValue) {
return new ValueManifest<>(provider, configPath, headerComments, inlineComments, defaultValue);
}
protected @Nullable ConfigurationProvider<?> provider;
protected @Nullable String configPath;
protected @Nullable List<String> headerComments;
protected @Nullable String inlineComment;
protected @Nullable T defaultValue;
/**
* @param provider 配置文件提供者
* @param configPath 配置路径
* @param headerComments 头部注释内容
* @param inlineComment 行内注释内容
* @param defaultValue 默认参数值
*/
public ValueManifest(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComment,
@Nullable T defaultValue) {
this.provider = provider;
this.configPath = configPath;
this.headerComments = headerComments;
this.inlineComment = inlineComment;
this.defaultValue = defaultValue;
}
/**
* 此方法提供给 {@link ConfigInitializer},以便对配置值的相关信息进行统一初始化操作。
*
* @param provider 配置文件提供者
* @param configPath 配置路径
* @param headerComments 头部注释内容
* @param inlineComment 行内注释内容
*/
protected void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComment) {
if (this.provider == null) this.provider = provider;
if (this.configPath == null) this.configPath = configPath;
if (this.headerComments == null) this.headerComments = headerComments;
if (this.inlineComment == null) this.inlineComment = inlineComment;
if (getHeaderComments() != null) {
this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
}
if (getInlineComment() != null) {
this.provider.setInlineComment(getConfigPath(), getInlineComment());
}
}
public @Nullable T getDefaultValue() {
return this.defaultValue;
}
public void setDefaultValue(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
}
public @NotNull ConfigurationProvider<?> getProvider() {
return Optional.ofNullable(this.provider)
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
}
public final @NotNull ConfigurationWrapper<?> getConfiguration() {
try {
return getProvider().getConfiguration();
} catch (Exception ex) {
throw new IllegalStateException("Value(" + configPath + ") has not been initialized", ex);
}
}
public @NotNull String getConfigPath() {
return Optional.ofNullable(this.configPath)
.orElseThrow(() -> new IllegalStateException("No section path provided."));
}
protected Object getValue() {
String path = getConfigPath(); // 当未指定路径时,优先抛出异常
return getConfiguration().get(path);
}
protected void setValue(@Nullable Object value) {
getConfiguration().set(getConfigPath(), value);
}
public @Nullable String getInlineComment() {
return inlineComment;
}
@Unmodifiable
public @Nullable List<String> getHeaderComments() {
return headerComments;
}
}
@@ -1,21 +1,19 @@
package cc.carm.lib.configuration.core.value.impl; package cc.carm.lib.configuration.core.value.impl;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.ValueManifest;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
public abstract class CachedConfigValue<T> extends ConfigValue<T> { 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, protected CachedConfigValue(@NotNull ValueManifest<T> manifest) {
@Nullable List<String> headerComments, @Nullable String inlineComments, super(manifest);
@Nullable T defaultValue) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
} }
protected T updateCache(T value) { protected T updateCache(T value) {
@@ -32,12 +30,19 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
return this.parsedTime <= 0 || getProvider().isExpired(this.parsedTime); return this.parsedTime <= 0 || getProvider().isExpired(this.parsedTime);
} }
protected final T useDefault() { protected final T getDefaultFirst(@Nullable T value) {
return useOrDefault(null);
}
protected final T useOrDefault(@Nullable T value) {
return updateCache(this.defaultValue == null ? value : this.defaultValue); return updateCache(this.defaultValue == null ? value : this.defaultValue);
} }
protected @Nullable T getCachedOrDefault() {
return getCachedOrDefault(null);
}
@Contract("!null->!null")
protected T getCachedOrDefault(@Nullable T emptyValue) {
if (getCachedValue() != null) return getCachedValue();
else if (getDefaultValue() != null) return getDefaultValue();
else return emptyValue;
}
} }
@@ -1,34 +1,40 @@
package cc.carm.lib.configuration.core.value.type; 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.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.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.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;
import java.util.ArrayList; import java.util.*;
import java.util.List; import java.util.function.Consumer;
import java.util.function.Function;
public class ConfiguredList<V> extends CachedConfigValue<List<V>> { public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements List<V> {
public static <V> @NotNull ConfigListBuilder<V> builder(@NotNull Class<V> valueClass) { public static <V> @NotNull ConfigListBuilder<V> builderOf(@NotNull Class<V> valueClass) {
return builder().asList(valueClass); return builder().asList(valueClass);
} }
@HeaderComment public static <V> @NotNull ConfiguredList<V> of(@NotNull Class<V> valueClass, @NotNull Collection<V> defaults) {
return builderOf(valueClass).fromObject().defaults(defaults).build();
}
@SafeVarargs
public static <V> @NotNull ConfiguredList<V> of(@NotNull Class<V> valueClass, @NotNull V... defaults) {
return builderOf(valueClass).fromObject().defaults(defaults).build();
}
protected final @NotNull Class<V> valueClass; protected final @NotNull Class<V> valueClass;
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, @Nullable String sectionPath, public ConfiguredList(@NotNull ValueManifest<List<V>> manifest, @NotNull Class<V> valueClass,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
@NotNull ConfigDataFunction<Object, V> parser, @NotNull ConfigDataFunction<Object, V> parser,
@NotNull ConfigDataFunction<V, Object> serializer) { @NotNull ConfigDataFunction<V, Object> serializer) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue); super(manifest);
this.valueClass = valueClass; this.valueClass = valueClass;
this.parser = parser; this.parser = parser;
this.serializer = serializer; this.serializer = serializer;
@@ -36,22 +42,41 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
@Override @Override
public @NotNull List<V> get() { public @NotNull List<V> get() {
if (isExpired()) { // 已过时的数据,需要重新解析一次。 if (!isExpired()) return getCachedOrDefault(new ArrayList<>());
List<V> list = new ArrayList<>();
List<?> data = getConfiguration().getList(getConfigPath()); // 已过时的数据,需要重新解析一次。
if (data == null || data.isEmpty()) return useOrDefault(list); List<V> list = new ArrayList<>();
for (Object dataVal : data) { List<?> data = getConfiguration().contains(getConfigPath()) ?
if (dataVal == null) continue; getConfiguration().getList(getConfigPath()) : null;
try { if (data == null) return getDefaultFirst(list);
list.add(parser.parse(dataVal)); for (Object dataVal : data) {
} catch (Exception e) { if (dataVal == null) continue;
e.printStackTrace(); try {
} list.add(parser.parse(dataVal));
} catch (Exception e) {
e.printStackTrace();
} }
return updateCache(list); }
} else if (getCachedValue() != null) return getCachedValue(); return updateCache(list);
else if (getDefaultValue() != null) return getDefaultValue(); }
else return new ArrayList<>();
@Override
public V get(int index) {
return get().get(index);
}
public <T> @NotNull T handle(Function<List<V>, T> function) {
List<V> list = get();
T result = function.apply(list);
set(list);
return result;
}
public @NotNull List<V> modify(Consumer<List<V>> consumer) {
List<V> list = get();
consumer.accept(list);
set(list);
return list;
} }
@Override @Override
@@ -72,5 +97,121 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> {
} }
} }
@Override
public V set(int index, V element) {
return handle(list -> list.set(index, element));
}
@Override
public int size() {
return get().size();
}
@Override
public boolean isEmpty() {
return get().isEmpty();
}
@Override
public boolean contains(Object o) {
return get().contains(o);
}
@NotNull
@Override
public Iterator<V> iterator() {
return get().iterator();
}
@NotNull
@Override
public Object @NotNull [] toArray() {
return get().toArray();
}
@NotNull
@Override
public <T> T @NotNull [] toArray(@NotNull T[] a) {
return get().toArray(a);
}
@Override
public boolean containsAll(@NotNull Collection<?> c) {
return new HashSet<>(get()).containsAll(c);
}
@Override
public boolean add(V v) {
handle(list -> list.add(v));
return true;
}
@Override
public void add(int index, V element) {
modify(list -> list.add(index, element));
}
@Override
public boolean addAll(@NotNull Collection<? extends V> c) {
return handle(list -> list.addAll(c));
}
@Override
public boolean addAll(int index, @NotNull Collection<? extends V> c) {
return handle(list -> list.addAll(index, c));
}
@Override
public boolean remove(Object o) {
return handle(list -> list.remove(o));
}
@Override
public V remove(int index) {
return handle(list -> list.remove(index));
}
@Override
public boolean removeAll(@NotNull Collection<?> c) {
return handle(list -> list.removeAll(c));
}
@Override
public boolean retainAll(@NotNull Collection<?> c) {
return handle(list -> list.retainAll(c));
}
@Override
public void clear() {
modify(List::clear);
}
@Override
public int indexOf(Object o) {
return get().indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return get().lastIndexOf(o);
}
@NotNull
@Override
public ListIterator<V> listIterator() {
return get().listIterator();
}
@NotNull
@Override
public ListIterator<V> listIterator(int index) {
return get().listIterator(index);
}
@NotNull
@Override
public List<V> subList(int fromIndex, int toIndex) {
return get().subList(fromIndex, toIndex);
}
} }
@@ -1,23 +1,26 @@
package cc.carm.lib.configuration.core.value.type; package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder; import cc.carm.lib.configuration.core.builder.map.ConfigMapCreator;
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.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.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;
import org.jetbrains.annotations.Unmodifiable;
import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> { public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements Map<K, V> {
public static <K, V> @NotNull ConfigMapBuilder<LinkedHashMap<K, V>, K, V> builder(@NotNull Class<K> keyClass, public static <K, V> @NotNull ConfigMapCreator<K, V> builderOf(@NotNull Class<K> keyClass,
@NotNull Class<V> valueClass) { @NotNull Class<V> valueClass) {
return builder().asMap(keyClass, valueClass); return builder().asMap(keyClass, valueClass);
} }
@@ -32,15 +35,15 @@ 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, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments, public ConfiguredMap(@NotNull ValueManifest<Map<K, V>> manifest,
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier, @NotNull Supplier<? extends Map<K, V>> mapObjSupplier,
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser, @NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser, @NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
@NotNull ConfigDataFunction<K, String> keySerializer, @NotNull ConfigDataFunction<K, String> keySerializer,
@NotNull ConfigDataFunction<V, Object> valueSerializer) { @NotNull ConfigDataFunction<V, Object> valueSerializer) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue); super(manifest);
this.supplier = supplier; this.supplier = mapObjSupplier;
this.keyClass = keyClass; this.keyClass = keyClass;
this.valueClass = valueClass; this.valueClass = valueClass;
this.keyParser = keyParser; this.keyParser = keyParser;
@@ -75,35 +78,39 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
@Override @Override
public @NotNull Map<K, V> get() { public @NotNull Map<K, V> get() {
if (isExpired()) { // 已过时的数据,需要重新解析一次。 if (!isExpired()) return getCachedOrDefault(supplier.get());
Map<K, V> map = supplier.get();
ConfigurationWrapper section = getConfiguration().getConfigurationSection(getConfigPath()); // 已过时的数据,需要重新解析一次。
if (section == null) return useOrDefault(map); Map<K, V> map = supplier.get();
Set<String> keys = section.getKeys(false); ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (keys.isEmpty()) return useOrDefault(map); if (section == null) return getDefaultFirst(map);
for (String dataKey : keys) { Set<String> keys = section.getKeys(false);
Object dataVal = section.get(dataKey); if (keys.isEmpty()) return getDefaultFirst(map);
if (dataVal == null) continue;
try { for (String dataKey : keys) {
K key = keyParser.parse(dataKey); Object dataVal = section.get(dataKey);
V value = valueParser.parse(dataVal); if (dataVal == null) continue;
map.put(key, value); try {
} catch (Exception e) { K key = keyParser.parse(dataKey);
e.printStackTrace(); V value = valueParser.parse(dataVal);
} map.put(key, value);
} catch (Exception e) {
e.printStackTrace();
} }
}
return updateCache(map); return updateCache(map);
} else if (getCachedValue() != null) return getCachedValue();
else if (getDefaultValue() != null) return getDefaultValue();
else return supplier.get();
} }
@Override @Override
public void set(Map<K, V> value) { public V get(Object key) {
return get().get(key);
}
@Override
public void set(@Nullable Map<K, V> value) {
updateCache(value); updateCache(value);
if (value == null) setValue(null); if (value == null) setValue(null);
else { else {
@@ -122,5 +129,78 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> {
} }
} }
public <T> @NotNull T modifyValue(Function<Map<K, V>, T> function) {
Map<K, V> m = get();
T result = function.apply(m);
set(m);
return result;
}
public @NotNull Map<K, V> modifyMap(Consumer<Map<K, V>> consumer) {
Map<K, V> m = get();
consumer.accept(m);
set(m);
return m;
}
@Override
public int size() {
return get().size();
}
@Override
public boolean isEmpty() {
return get().isEmpty();
}
@Override
public boolean containsKey(Object key) {
return get().containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return get().containsValue(value);
}
@Nullable
@Override
public V put(K key, V value) {
return modifyValue(m -> m.put(key, value));
}
@Override
public V remove(Object key) {
return modifyValue(m -> m.remove(key));
}
@Override
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
modifyMap(map -> map.putAll(m));
}
@Override
public void clear() {
modifyMap(Map::clear);
}
@NotNull
@Override
public Set<K> keySet() {
return get().keySet();
}
@NotNull
@Override
public Collection<V> values() {
return get().values();
}
@NotNull
@Override
@Unmodifiable
public Set<Entry<K, V>> entrySet() {
return get().entrySet();
}
} }
@@ -3,33 +3,29 @@ package cc.carm.lib.configuration.core.value.type;
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.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.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;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
public class ConfiguredSection<V> extends CachedConfigValue<V> { public class ConfiguredSection<V> extends CachedConfigValue<V> {
public static <V> @NotNull SectionValueBuilder<V> builder(@NotNull Class<V> valueClass) { public static <V> @NotNull SectionValueBuilder<V> builderOf(@NotNull Class<V> valueClass) {
return builder().asValue(valueClass).fromSection(); return builder().asValue(valueClass).fromSection();
} }
protected final @NotNull Class<V> valueClass; 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; protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath, public ConfiguredSection(@NotNull ValueManifest<V> manifest, @NotNull Class<V> valueClass,
@Nullable List<String> headerComments, @Nullable String inlineComments, @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
@NotNull Class<V> valueClass, @Nullable V defaultValue,
@NotNull ConfigValueParser<ConfigurationWrapper, V> parser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) { @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue); super(manifest);
this.valueClass = valueClass; this.valueClass = valueClass;
this.parser = parser; this.parser = parser;
this.serializer = serializer; this.serializer = serializer;
@@ -39,7 +35,7 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
return valueClass; return valueClass;
} }
public @NotNull ConfigValueParser<ConfigurationWrapper, V> getParser() { public @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> getParser() {
return parser; return parser;
} }
@@ -49,18 +45,21 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
@Override @Override
public @Nullable V get() { public @Nullable V get() {
if (isExpired()) { // 已过时的数据,需要重新解析一次。 if (!isExpired()) return getCachedOrDefault();
ConfigurationWrapper section = getConfiguration().getConfigurationSection(getConfigPath()); // 已过时的数据,需要重新解析一次。
if (section == null) return useDefault();
try { ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
// 若未出现错误,则直接更新缓存并返回。 if (section == null) return getDefaultValue();
return updateCache(this.parser.parse(section, this.defaultValue));
} catch (Exception e) { try {
// 出现了解析错误,提示并返回默认值 // 若未出现错误,则直接更新缓存并返回
e.printStackTrace(); return updateCache(this.parser.parse(section, this.defaultValue));
return useDefault(); } catch (Exception e) {
} // 出现了解析错误,提示并返回默认值。
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue); e.printStackTrace();
return getDefaultValue();
}
} }
@Override @Override
@@ -3,17 +3,14 @@ package cc.carm.lib.configuration.core.value.type;
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.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.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;
import java.util.List;
import java.util.Optional;
public class ConfiguredValue<V> extends CachedConfigValue<V> { public class ConfiguredValue<V> extends CachedConfigValue<V> {
public static <V> ConfigValueBuilder<V> builder(Class<V> valueClass) { public static <V> ConfigValueBuilder<V> builderOf(Class<V> valueClass) {
return builder().asValue(valueClass); return builder().asValue(valueClass);
} }
@@ -22,7 +19,7 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
} }
public static <V> ConfiguredValue<V> of(Class<V> valueClass, @Nullable V defaultValue) { public static <V> ConfiguredValue<V> of(Class<V> valueClass, @Nullable V defaultValue) {
return builder(valueClass).fromObject().defaults(defaultValue).build(); return builderOf(valueClass).fromObject().defaults(defaultValue).build();
} }
protected final @NotNull Class<V> valueClass; protected final @NotNull Class<V> valueClass;
@@ -30,13 +27,10 @@ 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, @Nullable String sectionPath, public ConfiguredValue(@NotNull ValueManifest<V> manifest, @NotNull Class<V> valueClass,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable V defaultValue,
@NotNull ConfigValueParser<Object, V> parser, @NotNull ConfigValueParser<Object, V> parser,
@NotNull ConfigDataFunction<V, Object> serializer) { @NotNull ConfigDataFunction<V, Object> serializer) {
super(manifest);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass; this.valueClass = valueClass;
this.parser = parser; this.parser = parser;
this.serializer = serializer; this.serializer = serializer;
@@ -52,18 +46,20 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
@Override @Override
public V get() { public V get() {
if (isExpired()) { // 已过时的数据,需要重新解析一次。 if (!isExpired()) return getCachedOrDefault();
Object value = getValue(); // 已过时的数据,需要重新解析一次。
if (value == null) return useDefault(); // 获取的值不存在,直接使用默认值。
try { Object value = getValue();
// 若未出现错误,则直接更新缓存并返回 if (value == null) return getDefaultValue(); // 获取的值不存在,直接使用默认值
return updateCache(this.parser.parse(value, this.defaultValue)); try {
} catch (Exception e) { // 若未出现错误,则直接更新缓存并返回。
// 出现了解析错误,提示并返回默认值。 return updateCache(this.parser.parse(value, this.defaultValue));
e.printStackTrace(); } catch (Exception e) {
return useDefault(); // 出现了解析错误,提示并返回默认值。
} e.printStackTrace();
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue); return getDefaultValue();
}
} }
@Override @Override
@@ -1,4 +1,4 @@
package config.offset; package cc.carm.test.config.offset;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -0,0 +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();
// }
//// 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;
// }
//
//
//}
//
+52
View File
@@ -0,0 +1,52 @@
<?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.6.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyconfiguration-demo</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-core</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -1,4 +1,4 @@
package config.source; package cc.carm.lib.configuration.demo;
import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.annotation.ConfigPath; import cc.carm.lib.configuration.core.annotation.ConfigPath;
@@ -12,7 +12,8 @@ public class DatabaseConfiguration extends ConfigurationRoot {
@ConfigPath("driver") @ConfigPath("driver")
@HeaderComment({ @HeaderComment({
"数据库驱动配置,请根据数据库类型设置。", "数据库驱动配置,请根据数据库类型设置。",
"- MySQL: com.mysql.cj.jdbc.Driver", "- MySQL(旧): com.mysql.jdbc.Driver",
"- MySQL(新): com.mysql.cj.jdbc.Driver",
"- MariaDB(推荐): org.mariadb.jdbc.Driver", "- MariaDB(推荐): org.mariadb.jdbc.Driver",
}) })
protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of( protected static final ConfigValue<String> DRIVER_NAME = ConfiguredValue.of(
@@ -0,0 +1,89 @@
package cc.carm.lib.configuration.demo.tests;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.demo.tests.conf.DemoConfiguration;
import cc.carm.lib.configuration.demo.tests.conf.TestConfiguration;
import cc.carm.lib.configuration.demo.tests.model.TestModel;
import org.jetbrains.annotations.TestOnly;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ConfigurationTest {
@TestOnly
public static void testDemo(ConfigurationProvider<?> provider) {
provider.initialize(DemoConfiguration.class);
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());
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
System.out.println("> Test List:");
System.out.println(" Before:");
DemoConfiguration.Sub.That.OPERATORS.forEach(System.out::println);
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
DemoConfiguration.Sub.That.OPERATORS.set(operators);
System.out.println(" After:");
DemoConfiguration.Sub.That.OPERATORS.forEach(System.out::println);
System.out.println("> Clear List:");
System.out.println(" Before: size :" + DemoConfiguration.Sub.That.OPERATORS.size());
DemoConfiguration.Sub.That.OPERATORS.modify(List::clear);
System.out.println(" After size :" + DemoConfiguration.Sub.That.OPERATORS.size());
System.out.println("> Test Section:");
System.out.println(DemoConfiguration.MODEL_TEST.get());
DemoConfiguration.MODEL_TEST.set(TestModel.random());
System.out.println("> Test Maps:");
DemoConfiguration.USERS.forEach((k, v) -> System.out.println(k + ": " + v));
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
for (int i = 1; i <= 5; i++) {
data.put(i, UUID.randomUUID());
}
DemoConfiguration.USERS.set(data);
System.out.println("----------------------------------------------------");
}
public static void testInner(ConfigurationProvider<?> provider) {
TestConfiguration TEST = new TestConfiguration();
provider.initialize(TEST, true);
System.out.println("> Test Inner value before:");
System.out.println(TEST.INNER.INNER_VALUE.getNotNull());
double after = Math.random() * 200D;
System.out.println("> Test Inner value -> " + after);
TEST.INNER.INNER_VALUE.set(after);
System.out.println("> Test Inner value after:");
System.out.println(TEST.INNER.INNER_VALUE.getNotNull());
}
public static void save(ConfigurationProvider<?> provider) {
try {
provider.save();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -1,4 +1,4 @@
package config.source; package cc.carm.lib.configuration.demo.tests.conf;
import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.core.ConfigurationRoot;
@@ -10,46 +10,41 @@ 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.model.TestModel; import cc.carm.lib.configuration.demo.tests.model.TestModel;
import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@HeaderComment({ @HeaderComment({"此处内容将显示在配置文件的最上方"})
"此处内容将显示在配置文件的最上方",
""/*添加一个空行与其他配置的注释分割*/
})
public class DemoConfiguration extends ConfigurationRoot { public class DemoConfiguration extends ConfigurationRoot {
@ConfigPath(root = true) @ConfigPath(root = true)
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D); 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<?> 变量标注子配置一并注册 // 支持通过 Class<?> 变量标注子配置一并注册
// 注意 若对应类也有注解则优先使用类的注解 // 注意 若对应类也有注解则优先使用类的注解
@ConfigPath("impl-test") //支持通过注解修改子配置的主路径若不修改则以变量名自动生成 @ConfigPath("other-class-config") //支持通过注解修改子配置的主路径若不修改则以变量名自动生成
@HeaderComment({"", "Something..."}) // 支持给子路径直接打注释 @HeaderComment({"", "Something..."}) // 支持给子路径直接打注释
@InlineComment("InlineComments for class path") @InlineComment("InlineComments for class path")
public static final Class<?> IMPL = ImplConfiguration.class; public static final Class<?> OTHER = OtherConfiguration.class;
// 子配置文件
@ConfigPath("database")
public static final Class<?> DB_CONFIG = DatabaseConfiguration.class;
@ConfigPath("user") // 通过注解规定配置文件中的路径若不进行注解则以变量名自动生成 @ConfigPath("user") // 通过注解规定配置文件中的路径若不进行注解则以变量名自动生成
@HeaderComment({"", "Section类型数据测试"}) // 通过注解给配置添加注释 @HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释
@InlineComment("Section数据也支持InlineComment注释") @InlineComment("Section数据也支持InlineComment注释")
public static final ConfigValue<TestModel> MODEL_TEST = ConfiguredSection public static final ConfigValue<TestModel> MODEL_TEST = ConfiguredSection
.builder(TestModel.class) .builderOf(TestModel.class)
.defaults(new TestModel("Carm", UUID.randomUUID())) .defaults(new TestModel("Carm", UUID.randomUUID()))
.parseValue((section, defaultValue) -> TestModel.deserialize(section)) .parseValue((section, defaultValue) -> TestModel.deserialize(section))
.serializeValue(TestModel::serialize).build(); .serializeValue(TestModel::serialize).build();
@HeaderComment({"", "[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"}) @HeaderComment({"[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"})
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap public static final ConfiguredMap<Integer, UUID> USERS = ConfiguredMap
.builder(Integer.class, UUID.class).fromString() .builderOf(Integer.class, UUID.class)
.asLinkedMap().fromString()
.parseKey(Integer::parseInt) .parseKey(Integer::parseInt)
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v))) .parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
.build(); .build();
@@ -64,19 +59,18 @@ public class DemoConfiguration extends ConfigurationRoot {
@ConfigPath(value = "uuid-value", root = true) @ConfigPath(value = "uuid-value", root = true)
@InlineComment("This is an inline comment") @InlineComment("This is an inline comment")
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
.builder(UUID.class).fromString() .builderOf(UUID.class).fromString()
.parseValue((data, defaultValue) -> UUID.fromString(data)) .parseValue((data, defaultValue) -> UUID.fromString(data))
.build(); .build();
public static class That extends ConfigurationRoot { public static class That extends ConfigurationRoot {
public static final ConfigValue<List<UUID>> OPERATORS = ConfiguredList public static final ConfiguredList<UUID> OPERATORS = ConfiguredList
.builder(UUID.class).fromString() .builderOf(UUID.class).fromString()
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s))) .parseValue(s -> Objects.requireNonNull(UUID.fromString(s)))
.build(); .build();
} }
} }
@@ -0,0 +1,4 @@
package cc.carm.lib.configuration.demo.tests.conf;
public class OtherConfiguration {
}
@@ -0,0 +1,30 @@
package cc.carm.lib.configuration.demo.tests.conf;
import cc.carm.lib.configuration.core.ConfigurationRoot;
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.ConfiguredSection;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.configuration.demo.tests.model.TestModel;
import java.util.UUID;
public class TestConfiguration extends ConfigurationRoot {
public final TestInnerConfiguration INNER = new TestInnerConfiguration();
public final ConfigValue<Double> CLASS_VALUE = ConfiguredValue.of(Double.class, 1.0D);
@ConfigPath("test.user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
@HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
@InlineComment("Section数据也支持InlineComment注释")
public final ConfigValue<TestModel> TEST_MODEL = ConfiguredSection
.builderOf(TestModel.class)
.defaults(new TestModel("Carm", UUID.randomUUID()))
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
.serializeValue(TestModel::serialize).build();
}
@@ -0,0 +1,13 @@
package cc.carm.lib.configuration.demo.tests.conf;
import cc.carm.lib.configuration.core.ConfigurationRoot;
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;
@HeaderComment("Inner Test")
public class TestInnerConfiguration extends ConfigurationRoot {
public final ConfigValue<Double> INNER_VALUE = ConfiguredValue.of(Double.class, 1.0D);
}
@@ -1,4 +1,4 @@
package config.model; package cc.carm.lib.configuration.demo.tests.model;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -1,4 +1,4 @@
package config.model; package cc.carm.lib.configuration.demo.tests.model;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -33,7 +33,7 @@ public class TestModel extends AbstractModel {
return map; return map;
} }
public static TestModel deserialize(ConfigurationWrapper section) { public static TestModel deserialize(ConfigurationWrapper<?> section) {
String name = section.getString("name"); String name = section.getString("name");
if (name == null) throw new NullPointerException("name is null"); if (name == null) throw new NullPointerException("name is null");
String uuidString = section.getString("info.uuid"); String uuidString = section.getString("info.uuid");
+13 -4
View File
@@ -5,13 +5,15 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.1.0</version> <version>3.6.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties> </properties>
<artifactId>easyconfiguration-json</artifactId> <artifactId>easyconfiguration-json</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
@@ -25,10 +27,17 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-demo</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.9.0</version> <version>2.10.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
@@ -1,6 +1,7 @@
package cc.carm.lib.configuration.json; package cc.carm.lib.configuration.json;
import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider; import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import com.google.gson.Gson; import com.google.gson.Gson;
@@ -8,12 +9,10 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
/** /**
* Some code comes from BungeeCord's implementation of the JsonConfiguration. * Some code comes from BungeeCord's implementation of the JsonConfiguration.
@@ -62,6 +61,11 @@ public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
initializeConfig(); initializeConfig();
} }
@Override
public @Nullable ConfigurationComments getComments() {
return null;
}
@Override @Override
public void save() throws Exception { public void save() throws Exception {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) { try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
@@ -69,26 +73,6 @@ public class JSONConfigProvider extends FileConfigProvider<JSONConfigWrapper> {
} }
} }
@Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
// JSON doesn't support comments;
}
@Override
public void setInlineComment(@NotNull String path, @Nullable String comment) {
// JSON doesn't support comments;
}
@Override
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
return null;
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return null;
}
@Override @Override
public @NotNull ConfigInitializer<? extends ConfigurationProvider<JSONConfigWrapper>> getInitializer() { public @NotNull ConfigInitializer<? extends ConfigurationProvider<JSONConfigWrapper>> getInitializer() {
return this.initializer; return this.initializer;
@@ -14,7 +14,7 @@ import java.util.Set;
* *
* @author md_5, CarmJos * @author md_5, CarmJos
*/ */
public class JSONConfigWrapper implements ConfigurationWrapper { public class JSONConfigWrapper implements ConfigurationWrapper<Map<String, Object>> {
private static final char SEPARATOR = '.'; private static final char SEPARATOR = '.';
protected final Map<String, Object> data; 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 @Override
public @NotNull Set<String> getKeys(boolean deep) { public @NotNull Set<String> getKeys(boolean deep) {
@@ -94,7 +98,7 @@ public class JSONConfigWrapper implements ConfigurationWrapper {
} }
@Override @Override
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) { public @Nullable JSONConfigWrapper getConfigurationSection(@NotNull String path) {
Object val = get(path); Object val = get(path);
return (val instanceof JSONConfigWrapper) ? (JSONConfigWrapper) val : null; return (val instanceof JSONConfigWrapper) ? (JSONConfigWrapper) val : null;
} }
@@ -1,67 +1,28 @@
package config; package config;
import cc.carm.lib.configuration.EasyConfiguration; import cc.carm.lib.configuration.EasyConfiguration;
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
import cc.carm.lib.configuration.json.JSONConfigProvider; import cc.carm.lib.configuration.json.JSONConfigProvider;
import config.model.TestModel;
import config.source.DemoConfiguration;
import org.junit.Test; import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class JSONConfigTest { public class JSONConfigTest {
protected final JSONConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml"); protected final JSONConfigProvider provider = EasyConfiguration.from("target/config.json", "config.json");
@Test @Test
public void onTest() { public void onTest() {
provider.initialize(DemoConfiguration.class); ConfigurationTest.testDemo(this.provider);
ConfigurationTest.testInner(this.provider);
testDemo();
System.out.println("----------------------------------------------------"); System.out.println("----------------------------------------------------");
provider.getConfiguration().getValues(true).forEach((k, v) -> System.out.println(k + ": " + v)); provider.getConfiguration().getValues(true).forEach((k, v) -> System.out.println(k + ": " + v));
System.out.println("----------------------------------------------------"); System.out.println("----------------------------------------------------");
provider.getConfiguration().getValues(false).forEach((k, v) -> System.out.println(k + ": " + v)); provider.getConfiguration().getValues(false).forEach((k, v) -> System.out.println(k + ": " + v));
System.out.println("----------------------------------------------------"); System.out.println("----------------------------------------------------");
try { ConfigurationTest.save(this.provider);
provider.save();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testDemo() {
System.out.println("----------------------------------------------------");
System.out.println("> Test Value:");
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
System.out.println("> Test List:");
DemoConfiguration.Sub.That.OPERATORS.getNotNull().forEach(System.out::println);
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
DemoConfiguration.Sub.That.OPERATORS.set(operators);
System.out.println("> Test Section:");
System.out.println(DemoConfiguration.MODEL_TEST.get());
DemoConfiguration.MODEL_TEST.set(TestModel.random());
System.out.println("> Test Maps:");
DemoConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
for (int i = 1; i <= 5; i++) {
data.put(i, UUID.randomUUID());
}
DemoConfiguration.USERS.set(data);
System.out.println("----------------------------------------------------");
} }
@@ -1,39 +0,0 @@
package config.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import java.util.HashMap;
import java.util.Map;
public class SomeModel extends AbstractModel {
int num;
public SomeModel(@NotNull String name, int num) {
super(name);
this.num = num;
}
@Override
public String toString() {
return "SomeModel{" +
"name='" + name + '\'' +
", num=" + num +
'}';
}
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("name", name);
map.put("num", num);
return map;
}
@TestOnly
public static SomeModel deserialize(Map<String, ?> args) {
return new SomeModel((String) args.get("name"), (Integer) args.get("num"));
}
}
@@ -1,30 +0,0 @@
package config.source;
import cc.carm.lib.configuration.core.ConfigurationRoot;
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;
@HeaderComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"})
public class DatabaseConfiguration extends ConfigurationRoot {
@ConfigPath("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());
}
}
@@ -1,65 +0,0 @@
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.core.value.type.ConfiguredList;
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.ConfiguredValue;
import config.model.TestModel;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
public class DemoConfiguration extends ConfigurationRoot {
@ConfigPath(root = true)
protected static final ConfigValue<Double> VERSION = ConfiguredValue.of(Double.class, 1.0D);
// 可以直接写静态内部类,并通过 Class<?> 声明。
public static final Class<?> SUB_TEST = Sub.class;
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
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();
// 子配置文件
@ConfigPath("database")
public static final Class<?> DB_CONFIG = DatabaseConfiguration.class;
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
.builder(Integer.class, UUID.class).fromString()
.parseKey(Integer::parseInt)
.parseValue(v -> Objects.requireNonNull(UUID.fromString(v)))
.build();
public static class Sub extends ConfigurationRoot {
@ConfigPath(value = "uuid-value", root = true)
public static final ConfigValue<UUID> UUID_CONFIG_VALUE = ConfiguredValue
.builder(UUID.class).fromString()
.parseValue((data, defaultValue) -> UUID.fromString(data))
.build();
public static final Class<?> NOTHING = That.class;
public static class That extends ConfigurationRoot {
public static final ConfigValue<List<UUID>> OPERATORS = ConfiguredList
.builder(UUID.class).fromString()
.parseValue(s -> Objects.requireNonNull(UUID.fromString(s)))
.build();
}
}
}
+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.6.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${project.jdk.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;
+19 -4
View File
@@ -5,13 +5,15 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.1.0</version> <version>3.6.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties> </properties>
<artifactId>easyconfiguration-yaml</artifactId> <artifactId>easyconfiguration-yaml</artifactId>
@@ -26,10 +28,23 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>yamlcommentwriter</artifactId>
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-demo</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.bspfsystems</groupId> <groupId>org.bspfsystems</groupId>
<artifactId>yamlconfiguration</artifactId> <artifactId>yamlconfiguration</artifactId>
<version>1.2.0</version> <version>1.3.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
@@ -1,148 +0,0 @@
package cc.carm.lib.configuration.yaml;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
import org.bspfsystems.yamlconfiguration.file.FileConfiguration;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static cc.carm.lib.configuration.yaml.YAMLConfigProvider.SEPARATOR;
public class YAMLComments {
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);
}
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
List<String> comments = getHeaderComment(path);
if (comments == null || comments.size() == 0) return 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 项目。
*
* @param source 源配置文件
* @param writer 配置写入器
* @throws IOException 当写入发生错误时抛出
*/
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容
String configHeader = buildHeaderComments(null, "");
if (configHeader != null) writer.write(configHeader);
for (String fullKey : source.getKeys(true)) {
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 (inlineComment != null && inlineComment.length() > 0) {
writer.write(" # " + inlineComment);
}
if (!section.getKeys(false).isEmpty()) {
writer.write("\n");
} else {
writer.write(" {}\n");
if (indents.length() == 0) writer.write("\n");
}
continue;
}
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");
}
writer.close();
}
/**
* 得到一个键的缩进。
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
*
* @param key 键
* @return 该键的缩进文本
*/
protected static String getIndents(String key) {
String[] splitKey = key.split("[" + YAMLConfigProvider.SEPARATOR + "]");
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
}
}
@@ -1,25 +1,23 @@
package cc.carm.lib.configuration.yaml; package cc.carm.lib.configuration.yaml;
import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider; import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import cc.carm.lib.yamlcommentupdater.CommentedYAML;
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
import org.bspfsystems.yamlconfiguration.file.FileConfiguration;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration; import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Set;
public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> { public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> implements CommentedYAML {
protected static final char SEPARATOR = '.'; protected final @NotNull ConfigurationComments comments = new ConfigurationComments();
protected final @NotNull YAMLComments comments = new YAMLComments();
protected YamlConfiguration configuration; protected YamlConfiguration configuration;
protected ConfigInitializer<YAMLConfigProvider> initializer; protected ConfigInitializer<YAMLConfigProvider> initializer;
@@ -42,44 +40,19 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
configuration.load(getFile()); configuration.load(getFile());
} }
@Override
public @NotNull ConfigurationComments getComments() {
return this.comments;
}
@Override @Override
public void save() throws Exception { public void save() throws Exception {
configuration.save(getFile()); try {
CommentedYAMLWriter.writeWithComments(this, this.file);
// tchristofferson/ConfigUpdater start } catch (Exception ex) {
StringWriter writer = new StringWriter(); configuration.save(file);
this.comments.writeComments(configuration, new BufferedWriter(writer)); throw ex;
String value = writer.toString(); // config contents
Path toUpdatePath = getFile().toPath();
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), StandardCharsets.UTF_8))) {
// if updated contents are not the same as current file contents, update
Files.write(toUpdatePath, value.getBytes(StandardCharsets.UTF_8));
} }
// tchristofferson/ConfigUpdater end
}
@Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
this.comments.setHeaderComments(path, comments);
}
@Override
public void setInlineComment(@NotNull String path, @Nullable String comment) {
this.comments.setInlineComment(path, comment);
}
@Override
@Nullable
@Unmodifiable
public List<String> getHeaderComment(@Nullable String path) {
return this.comments.getHeaderComment(path);
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return this.comments.getInlineComment(path);
} }
@Override @Override
@@ -87,4 +60,31 @@ public class YAMLConfigProvider extends FileConfigProvider<YAMLSectionWrapper> {
return this.initializer; return this.initializer;
} }
@Override
public String serializeValue(@NotNull String key, @NotNull Object value) {
FileConfiguration temp = new YamlConfiguration();
temp.set(key, value);
return temp.saveToString();
}
@Override
public Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
if (sectionKey == null) return configuration.getKeys(deep);
ConfigurationSection section = configuration.getConfigurationSection(sectionKey);
if (section == null) return null;
return section.getKeys(deep);
}
@Override
public @Nullable Object getValue(@NotNull String key) {
return configuration.get(key);
}
@Override
public @Nullable List<String> getHeaderComments(@Nullable String key) {
return comments.getHeaderComment(key);
}
} }
@@ -12,7 +12,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
public class YAMLSectionWrapper implements ConfigurationWrapper { public class YAMLSectionWrapper implements ConfigurationWrapper<ConfigurationSection> {
private final ConfigurationSection section; private final ConfigurationSection section;
@@ -25,6 +25,11 @@ public class YAMLSectionWrapper implements ConfigurationWrapper {
return section == null ? null : new YAMLSectionWrapper(section); return section == null ? null : new YAMLSectionWrapper(section);
} }
@Override
public @NotNull ConfigurationSection getSource() {
return this.section;
}
@Override @Override
public @NotNull Set<String> getKeys(boolean deep) { public @NotNull Set<String> getKeys(boolean deep) {
return new LinkedHashSet<>(section.getKeys(deep)); return new LinkedHashSet<>(section.getKeys(deep));
@@ -66,7 +71,7 @@ public class YAMLSectionWrapper implements ConfigurationWrapper {
} }
@Override @Override
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) { public @Nullable YAMLSectionWrapper getConfigurationSection(@NotNull String path) {
return of(this.section.getConfigurationSection(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) { public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T defaultValue) {
return this.section.getSerializable(path, clazz, defaultValue); return this.section.getSerializable(path, clazz, defaultValue);
} }
} }
@@ -1,12 +1,10 @@
package cc.carm.lib.configuration.yaml; package cc.carm.lib.configuration.yaml;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder; import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public abstract class YAMLValue<T> extends CachedConfigValue<T> { public abstract class YAMLValue<T> extends CachedConfigValue<T> {
@@ -14,10 +12,8 @@ public abstract class YAMLValue<T> extends CachedConfigValue<T> {
return new YAMLConfigBuilder(); return new YAMLConfigBuilder();
} }
public YAMLValue(@Nullable YAMLConfigProvider provider, @Nullable String configPath, public YAMLValue(@NotNull ValueManifest<T> manifest) {
@Nullable List<String> headerComments, @Nullable String inlineComments, super(manifest);
@Nullable T defaultValue) {
super(provider, configPath, headerComments, inlineComments, defaultValue);
} }
public YAMLConfigProvider getYAMLProvider() { public YAMLConfigProvider getYAMLProvider() {
@@ -21,7 +21,7 @@ public class SerializableBuilder<T extends ConfigurationSerializable>
@Override @Override
public @NotNull ConfiguredSerializable<T> build() { public @NotNull ConfiguredSerializable<T> build() {
return new ConfiguredSerializable<>(this.provider, this.path, this.headerComments, this.inlineComment, this.valueClass, this.defaultValue); return new ConfiguredSerializable<>(buildManifest(), this.valueClass);
} }
@@ -1,14 +1,11 @@
package cc.carm.lib.configuration.yaml.value; package cc.carm.lib.configuration.yaml.value;
import cc.carm.lib.configuration.yaml.YAMLConfigProvider; import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.yaml.YAMLValue; import cc.carm.lib.configuration.yaml.YAMLValue;
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable; import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> { public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> {
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) { public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
@@ -22,25 +19,24 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
protected final @NotNull Class<T> valueClass; protected final @NotNull Class<T> valueClass;
public ConfiguredSerializable(@Nullable YAMLConfigProvider provider, @Nullable String configPath, public ConfiguredSerializable(@NotNull ValueManifest<T> manifest, @NotNull Class<T> valueClass) {
@Nullable List<String> headerComments, @Nullable String inlineComments, super(manifest);
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
super(provider, configPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass; this.valueClass = valueClass;
} }
@Override @Override
public @Nullable T get() { public @Nullable T get() {
if (isExpired()) { // 已过时的数据,需要重新解析一次。 if (!isExpired()) return getCachedOrDefault();
try {
// 若未出现错误,则直接更新缓存并返回。 try {
return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue())); // 若未出现错误,则直接更新缓存并返回。
} catch (Exception e) { return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
// 出现了解析错误,提示并返回默认值。 } catch (Exception e) {
e.printStackTrace(); // 出现了解析错误,提示并返回默认值。
return useDefault(); e.printStackTrace();
} return getDefaultValue();
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue); }
} }
@Override @Override
@@ -1,81 +0,0 @@
package config;
import cc.carm.lib.configuration.EasyConfiguration;
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
import config.model.AbstractModel;
import config.model.SomeModel;
import config.model.TestModel;
import config.source.DemoConfiguration;
import config.source.ImplConfiguration;
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ConfigTester {
static {
ConfigurationSerialization.registerClass(TestModel.class);
ConfigurationSerialization.registerClass(SomeModel.class);
}
protected final YAMLConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
@Test
public void onTest() {
provider.initialize(DemoConfiguration.class);
testDemo();
testSerialization();
try {
provider.save();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testDemo() {
System.out.println("----------------------------------------------------");
System.out.println("> Test Value:");
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
System.out.println("> Test List:");
DemoConfiguration.Sub.That.OPERATORS.getNotNull().forEach(System.out::println);
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
DemoConfiguration.Sub.That.OPERATORS.set(operators);
System.out.println("> Test Section:");
System.out.println(DemoConfiguration.MODEL_TEST.get());
DemoConfiguration.MODEL_TEST.set(TestModel.random());
System.out.println("> Test Maps:");
DemoConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
for (int i = 1; i <= 5; i++) {
data.put(i, UUID.randomUUID());
}
DemoConfiguration.USERS.set(data);
System.out.println("----------------------------------------------------");
}
public static void testSerialization() {
System.out.println("----------------------------------------------------");
AbstractModel model = ImplConfiguration.TEST.get();
if (model != null) {
System.out.println(model.getName());
}
System.out.println("----------------------------------------------------");
}
}
@@ -0,0 +1,50 @@
package config;
import cc.carm.lib.configuration.EasyConfiguration;
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
import config.model.AnyModel;
import config.model.SomeModel;
import config.source.ModelConfiguration;
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
import org.junit.Test;
public class DemoConfigTest {
static {
ConfigurationSerialization.registerClass(SomeModel.class);
ConfigurationSerialization.registerClass(AnyModel.class);
}
protected final YAMLConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
@Test
public void onTest() {
ConfigurationTest.testDemo(this.provider);
ConfigurationTest.testInner(this.provider);
testSerialization(this.provider);
ConfigurationTest.save(this.provider);
}
public static void testSerialization(YAMLConfigProvider provider) {
provider.initialize(ModelConfiguration.class);
System.out.println("----------------------------------------------------");
AbstractModel someModel = ModelConfiguration.SOME_MODEL.get();
if (someModel != null) System.out.println(someModel.getName());
AbstractModel anyModel = ModelConfiguration.ANY_MODEL.get();
if (anyModel != null) System.out.println(anyModel.getName());
ModelConfiguration.MODELS.forEach(System.out::println);
System.out.println("----------------------------------------------------");
}
}
@@ -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);
}
}
}
@@ -1,17 +0,0 @@
package config.model;
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
public abstract class AbstractModel implements ConfigurationSerializable {
protected final @NotNull String name;
public AbstractModel(@NotNull String name) {
this.name = name;
}
public @NotNull String getName() {
return name;
}
}
@@ -0,0 +1,50 @@
package config.model;
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
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("AnyModel")
public class AnyModel extends AbstractModel implements ConfigurationSerializable {
public final boolean bool;
public AnyModel(@NotNull String name, boolean bool) {
super(name);
this.bool = bool;
}
@Override
public String toString() {
return "AnyModel{" +
"name='" + name + '\'' +
", bool=" + bool +
'}';
}
@Override
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("name", name);
map.put("state", bool);
return map;
}
public static AnyModel random() {
return new AnyModel(UUID.randomUUID().toString().substring(0, 5), Math.random() > 0.5);
}
@TestOnly
public static AnyModel deserialize(Map<String, ?> args) {
return new AnyModel((String) args.get("name"), (Boolean) args.get("state"));
}
}
@@ -1,5 +1,6 @@
package config.model; package config.model;
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable; import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
import org.bspfsystems.yamlconfiguration.serialization.SerializableAs; import org.bspfsystems.yamlconfiguration.serialization.SerializableAs;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -7,11 +8,12 @@ import org.jetbrains.annotations.TestOnly;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
@SerializableAs("SomeModel") @SerializableAs("SomeModel")
public class SomeModel extends AbstractModel implements ConfigurationSerializable { public class SomeModel extends AbstractModel implements ConfigurationSerializable {
int num; public final int num;
public SomeModel(@NotNull String name, int num) { public SomeModel(@NotNull String name, int num) {
super(name); super(name);
@@ -34,6 +36,11 @@ public class SomeModel extends AbstractModel implements ConfigurationSerializabl
return map; return map;
} }
public static SomeModel random() {
return new SomeModel(UUID.randomUUID().toString().substring(0, 5), (int) (Math.random() * 1000));
}
@TestOnly @TestOnly
public static SomeModel deserialize(Map<String, ?> args) { public static SomeModel deserialize(Map<String, ?> args) {
return new SomeModel((String) args.get("name"), (Integer) args.get("num")); return new SomeModel((String) args.get("name"), (Integer) args.get("num"));
@@ -1,72 +0,0 @@
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 extends AbstractModel implements ConfigurationSerializable {
public UUID uuid;
public TestModel(String name, UUID uuid) {
super(name);
this.uuid = uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
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 +
'}';
}
}
@@ -1,63 +0,0 @@
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();
}
// 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,18 +0,0 @@
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.AbstractModel;
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()
);
}
@@ -0,0 +1,34 @@
package config.source;
import cc.carm.lib.configuration.core.ConfigurationRoot;
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.ConfiguredList;
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
import config.model.AnyModel;
import config.model.SomeModel;
import java.util.Map;
@HeaderComment("以下内容用于测试序列化")
@ConfigPath("model-test")
public class ModelConfiguration extends ConfigurationRoot {
public static final ConfigValue<? extends AbstractModel> SOME_MODEL = ConfiguredSerializable.of(
SomeModel.class, SomeModel.random()
);
public static final ConfigValue<? extends AbstractModel> ANY_MODEL = ConfiguredSerializable.of(
AnyModel.class, AnyModel.random()
);
public static final ConfiguredList<AnyModel> MODELS = ConfiguredList.builderOf(AnyModel.class)
.fromObject()
.parseValue((section) -> AnyModel.deserialize((Map<String, ?>) section))
.serializeValue(AnyModel::serialize)
.defaults(AnyModel.random(), AnyModel.random(), AnyModel.random())
.build();
}
-1
View File
@@ -1,2 +1 @@
# Test Header
version: 1.0 version: 1.0
+17 -15
View File
@@ -5,9 +5,9 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
<java.version>1.8</java.version> <project.jdk.version>1.8</project.jdk.version>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties> </properties>
@@ -15,11 +15,13 @@
<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>3.1.0</version> <version>3.6.0</version>
<modules> <modules>
<module>core</module> <module>core</module>
<module>demo</module>
<module>impl/yaml</module> <module>impl/yaml</module>
<module>impl/json</module> <module>impl/json</module>
<module>impl/sql</module>
</modules> </modules>
<name>EasyConfiguration</name> <name>EasyConfiguration</name>
@@ -90,7 +92,7 @@
<downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl> <downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl>
<site> <site>
<id>javadoc</id> <id>javadoc</id>
<name>EasyConfiguration JavaDoc (on Github Pages)</name> <name>EasyConfiguration JavaDoc (on GitHub Pages)</name>
<url>https://CarmJos.github.io/EasyConfiguration</url> <url>https://CarmJos.github.io/EasyConfiguration</url>
</site> </site>
</distributionManagement> </distributionManagement>
@@ -107,7 +109,7 @@
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>23.0.0</version> <version>24.0.1</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
@@ -120,7 +122,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <version>3.1.0</version>
<configuration> <configuration>
<useSystemClassLoader>false</useSystemClassLoader> <useSystemClassLoader>false</useSystemClassLoader>
</configuration> </configuration>
@@ -128,7 +130,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId> <artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version> <version>3.1.0</version>
<executions> <executions>
<execution> <execution>
<id>sign-artifacts</id> <id>sign-artifacts</id>
@@ -148,7 +150,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId> <artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version> <version>3.0.0</version>
<configuration> <configuration>
<autoVersionSubmodules>true</autoVersionSubmodules> <autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile> <useReleaseProfile>false</useReleaseProfile>
@@ -164,7 +166,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.0</version> <version>3.5.0</version>
<configuration> <configuration>
<classifier>javadoc</classifier> <classifier>javadoc</classifier>
<detectJavaApiLink>false</detectJavaApiLink> <detectJavaApiLink>false</detectJavaApiLink>
@@ -191,10 +193,10 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version> <version>3.11.0</version>
<configuration> <configuration>
<source>${java.version}</source> <source>${project.jdk.version}</source>
<target>${java.version}</target> <target>${project.jdk.version}</target>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<compilerArgument>-parameters</compilerArgument> <compilerArgument>-parameters</compilerArgument>
</configuration> </configuration>
@@ -203,7 +205,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version> <version>3.3.0</version>
</plugin> </plugin>
<plugin> <plugin>
@@ -223,7 +225,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version> <version>3.4.1</version>
<executions> <executions>
<execution> <execution>
<phase>package</phase> <phase>package</phase>