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

Compare commits

...

7 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
24 changed files with 242 additions and 138 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.5.1</version> <version>3.6.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
@@ -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;
@@ -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);
}
}
@@ -11,11 +11,16 @@ import org.jetbrains.annotations.Unmodifiable;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
/**
* 配置文件提供者,用于为 {@link ConfigValue} 提供配置文件的源,以便实现读取、保存等操作。
*
* @param <W> 配置文件的原生功能类
*/
public abstract class ConfigurationProvider<W extends ConfigurationWrapper<?>> { 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();
} }
@@ -14,7 +14,7 @@ public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> exte
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;
} }
@@ -15,12 +15,20 @@ public abstract class ConfigValue<T> extends ValueManifest<T> {
return new ConfigBuilder(); return new ConfigBuilder();
} }
public ConfigValue(@NotNull ValueManifest<T> manifest) { protected ConfigValue(@NotNull ValueManifest<T> manifest) {
super(manifest.provider, manifest.configPath, manifest.headerComments, manifest.inlineComment, manifest.defaultValue); super(manifest.provider, manifest.configPath, manifest.headerComments, manifest.inlineComment, manifest.defaultValue);
} }
/**
* @param provider 配置文件提供者
* @param configPath 配置路径
* @param headerComments 头部注释内容
* @param inlineComments 行内注释内容
* @param defaultValue 默认参数值
* @deprecated 请使用 {@link #ConfigValue(ValueManifest)} 构造器。
*/
@Deprecated @Deprecated
public ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath, protected ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments, @Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) { @Nullable T defaultValue) {
super(provider, configPath, headerComments, inlineComments, defaultValue); super(provider, configPath, headerComments, inlineComments, defaultValue);
@@ -1,5 +1,6 @@
package cc.carm.lib.configuration.core.value; 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.ConfigurationProvider;
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;
@@ -9,6 +10,12 @@ import org.jetbrains.annotations.Unmodifiable;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
/**
* 配置值描述清单。用于描述一个配置值的相关基础信息。
*
* @param <T> 配置值类型
* @author CarmJos
*/
public class ValueManifest<T> { public class ValueManifest<T> {
public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath, public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@@ -30,6 +37,13 @@ public class ValueManifest<T> {
protected @Nullable T defaultValue; protected @Nullable T defaultValue;
/**
* @param provider 配置文件提供者
* @param configPath 配置路径
* @param headerComments 头部注释内容
* @param inlineComment 行内注释内容
* @param defaultValue 默认参数值
*/
public ValueManifest(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath, public ValueManifest(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComment, @Nullable List<String> headerComments, @Nullable String inlineComment,
@Nullable T defaultValue) { @Nullable T defaultValue) {
@@ -40,12 +54,20 @@ public class ValueManifest<T> {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
/**
* 此方法提供给 {@link ConfigInitializer},以便对配置值的相关信息进行统一初始化操作。
*
* @param provider 配置文件提供者
* @param configPath 配置路径
* @param headerComments 头部注释内容
* @param inlineComment 行内注释内容
*/
protected void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath, protected void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments) { @Nullable List<String> headerComments, @Nullable String inlineComment) {
if (this.provider == null) this.provider = provider; if (this.provider == null) this.provider = provider;
if (this.configPath == null) this.configPath = configPath; if (this.configPath == null) this.configPath = configPath;
if (this.headerComments == null) this.headerComments = headerComments; if (this.headerComments == null) this.headerComments = headerComments;
if (this.inlineComment == null) this.inlineComment = inlineComments; if (this.inlineComment == null) this.inlineComment = inlineComment;
if (getHeaderComments() != null) { if (getHeaderComments() != null) {
this.provider.setHeaderComment(getConfigPath(), getHeaderComments()); this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.core.value.impl;
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 cc.carm.lib.configuration.core.value.ValueManifest;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -11,7 +12,7 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
protected @Nullable T cachedValue; protected @Nullable T cachedValue;
protected long parsedTime = -1; protected long parsedTime = -1;
public CachedConfigValue(@NotNull ValueManifest<T> manifest) { protected CachedConfigValue(@NotNull ValueManifest<T> manifest) {
super(manifest); super(manifest);
} }
@@ -29,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;
}
} }
@@ -13,10 +13,19 @@ import java.util.function.Function;
public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements 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);
} }
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;
@@ -33,11 +42,13 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
@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<V> list = new ArrayList<>();
List<?> data = getConfiguration().contains(getConfigPath()) ? List<?> data = getConfiguration().contains(getConfigPath()) ?
getConfiguration().getList(getConfigPath()) : null; getConfiguration().getList(getConfigPath()) : null;
if (data == null) return useOrDefault(list); if (data == null) return getDefaultFirst(list);
for (Object dataVal : data) { for (Object dataVal : data) {
if (dataVal == null) continue; if (dataVal == null) continue;
try { try {
@@ -47,9 +58,6 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
} }
} }
return updateCache(list); return updateCache(list);
} else if (getCachedValue() != null) return getCachedValue();
else if (getDefaultValue() != null) return getDefaultValue();
else return new ArrayList<>();
} }
@Override @Override
@@ -57,14 +65,14 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
return get().get(index); return get().get(index);
} }
public <T> @NotNull T modifyValue(Function<List<V>, T> function) { public <T> @NotNull T handle(Function<List<V>, T> function) {
List<V> list = get(); List<V> list = get();
T result = function.apply(list); T result = function.apply(list);
set(list); set(list);
return result; return result;
} }
public @NotNull List<V> modifyList(Consumer<List<V>> consumer) { public @NotNull List<V> modify(Consumer<List<V>> consumer) {
List<V> list = get(); List<V> list = get();
consumer.accept(list); consumer.accept(list);
set(list); set(list);
@@ -91,7 +99,7 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
@Override @Override
public V set(int index, V element) { public V set(int index, V element) {
return modifyValue(list -> list.set(index, element)); return handle(list -> list.set(index, element));
} }
@Override @Override
@@ -134,48 +142,48 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
@Override @Override
public boolean add(V v) { public boolean add(V v) {
modifyValue(list -> list.add(v)); handle(list -> list.add(v));
return true; return true;
} }
@Override @Override
public void add(int index, V element) { public void add(int index, V element) {
modifyList(list -> list.add(index, element)); modify(list -> list.add(index, element));
} }
@Override @Override
public boolean addAll(@NotNull Collection<? extends V> c) { public boolean addAll(@NotNull Collection<? extends V> c) {
return modifyValue(list -> list.addAll(c)); return handle(list -> list.addAll(c));
} }
@Override @Override
public boolean addAll(int index, @NotNull Collection<? extends V> c) { public boolean addAll(int index, @NotNull Collection<? extends V> c) {
return modifyValue(list -> list.addAll(index, c)); return handle(list -> list.addAll(index, c));
} }
@Override @Override
public boolean remove(Object o) { public boolean remove(Object o) {
return modifyValue(list -> list.remove(o)); return handle(list -> list.remove(o));
} }
@Override @Override
public V remove(int index) { public V remove(int index) {
return modifyValue(list -> list.remove(index)); return handle(list -> list.remove(index));
} }
@Override @Override
public boolean removeAll(@NotNull Collection<?> c) { public boolean removeAll(@NotNull Collection<?> c) {
return modifyValue(list -> list.removeAll(c)); return handle(list -> list.removeAll(c));
} }
@Override @Override
public boolean retainAll(@NotNull Collection<?> c) { public boolean retainAll(@NotNull Collection<?> c) {
return modifyValue(list -> list.retainAll(c)); return handle(list -> list.retainAll(c));
} }
@Override @Override
public void clear() { public void clear() {
modifyList(List::clear); modify(List::clear);
} }
@Override @Override
@@ -1,6 +1,6 @@
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.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.core.value.ValueManifest;
@@ -19,7 +19,7 @@ import java.util.function.Supplier;
public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements 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);
} }
@@ -36,15 +36,13 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer; protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
public ConfiguredMap(@NotNull ValueManifest<Map<K, V>> manifest public ConfiguredMap(@NotNull ValueManifest<Map<K, V>> manifest,
,
@NotNull Supplier<? extends Map<K, V>> mapObjSupplier, @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(manifest super(manifest);
);
this.supplier = mapObjSupplier; this.supplier = mapObjSupplier;
this.keyClass = keyClass; this.keyClass = keyClass;
this.valueClass = valueClass; this.valueClass = valueClass;
@@ -80,14 +78,16 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
@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(); Map<K, V> map = supplier.get();
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath()); ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return useOrDefault(map); if (section == null) return getDefaultFirst(map);
Set<String> keys = section.getKeys(false); Set<String> keys = section.getKeys(false);
if (keys.isEmpty()) return useOrDefault(map); if (keys.isEmpty()) return getDefaultFirst(map);
for (String dataKey : keys) { for (String dataKey : keys) {
Object dataVal = section.get(dataKey); Object dataVal = section.get(dataKey);
@@ -102,9 +102,6 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
} }
return updateCache(map); return updateCache(map);
} else if (getCachedValue() != null) return getCachedValue();
else if (getDefaultValue() != null) return getDefaultValue();
else return supplier.get();
} }
@Override @Override
@@ -10,12 +10,10 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
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> builderOf(@NotNull Class<V> valueClass) {
public static <V> @NotNull SectionValueBuilder<V> builder(@NotNull Class<V> valueClass) {
return builder().asValue(valueClass).fromSection(); return builder().asValue(valueClass).fromSection();
} }
@@ -47,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()); ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return useDefault(); if (section == null) return getDefaultValue();
try { try {
// 若未出现错误,则直接更新缓存并返回。 // 若未出现错误,则直接更新缓存并返回。
return updateCache(this.parser.parse(section, this.defaultValue)); return updateCache(this.parser.parse(section, this.defaultValue));
} catch (Exception e) { } catch (Exception e) {
// 出现了解析错误,提示并返回默认值。 // 出现了解析错误,提示并返回默认值。
e.printStackTrace(); e.printStackTrace();
return useDefault(); return getDefaultValue();
} }
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
} }
@Override @Override
@@ -8,11 +8,9 @@ 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.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);
} }
@@ -21,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;
@@ -48,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(); Object value = getValue();
if (value == null) return useDefault(); // 获取的值不存在,直接使用默认值。 if (value == null) return getDefaultValue(); // 获取的值不存在,直接使用默认值。
try { try {
// 若未出现错误,则直接更新缓存并返回。 // 若未出现错误,则直接更新缓存并返回。
return updateCache(this.parser.parse(value, this.defaultValue)); return updateCache(this.parser.parse(value, this.defaultValue));
} catch (Exception e) { } catch (Exception e) {
// 出现了解析错误,提示并返回默认值。 // 出现了解析错误,提示并返回默认值。
e.printStackTrace(); e.printStackTrace();
return useDefault(); return getDefaultValue();
} }
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
} }
@Override @Override
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.5.1</version> <version>3.6.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
@@ -42,7 +42,7 @@ public class ConfigurationTest {
System.out.println("> Clear List:"); System.out.println("> Clear List:");
System.out.println(" Before: size :" + DemoConfiguration.Sub.That.OPERATORS.size()); System.out.println(" Before: size :" + DemoConfiguration.Sub.That.OPERATORS.size());
DemoConfiguration.Sub.That.OPERATORS.modifyList(List::clear); DemoConfiguration.Sub.That.OPERATORS.modify(List::clear);
System.out.println(" After size :" + DemoConfiguration.Sub.That.OPERATORS.size()); System.out.println(" After size :" + DemoConfiguration.Sub.That.OPERATORS.size());
System.out.println("> Test Section:"); System.out.println("> Test Section:");
@@ -36,14 +36,15 @@ public class DemoConfiguration extends ConfigurationRoot {
@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 ConfiguredMap<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();
@@ -58,14 +59,14 @@ 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 ConfiguredList<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();
@@ -21,7 +21,7 @@ public class TestConfiguration extends ConfigurationRoot {
@HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释。 @HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
@InlineComment("Section数据也支持InlineComment注释") @InlineComment("Section数据也支持InlineComment注释")
public final ConfigValue<TestModel> TEST_MODEL = ConfiguredSection public final ConfigValue<TestModel> TEST_MODEL = 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();
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.5.1</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>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.5.1</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>
+2 -2
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.5.1</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>
@@ -44,7 +44,7 @@
<dependency> <dependency>
<groupId>org.bspfsystems</groupId> <groupId>org.bspfsystems</groupId>
<artifactId>yamlconfiguration</artifactId> <artifactId>yamlconfiguration</artifactId>
<version>1.3.1</version> <version>1.3.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
@@ -6,8 +6,6 @@ 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.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) {
@@ -28,16 +26,17 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
@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())); return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
} catch (Exception e) { } catch (Exception e) {
// 出现了解析错误,提示并返回默认值。 // 出现了解析错误,提示并返回默认值。
e.printStackTrace(); e.printStackTrace();
return useDefault(); return getDefaultValue();
} }
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
} }
@Override @Override
@@ -41,6 +41,8 @@ public class DemoConfigTest {
AbstractModel anyModel = ModelConfiguration.ANY_MODEL.get(); AbstractModel anyModel = ModelConfiguration.ANY_MODEL.get();
if (anyModel != null) System.out.println(anyModel.getName()); if (anyModel != null) System.out.println(anyModel.getName());
ModelConfiguration.MODELS.forEach(System.out::println);
System.out.println("----------------------------------------------------"); System.out.println("----------------------------------------------------");
} }
@@ -4,11 +4,14 @@ import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.annotation.ConfigPath; import cc.carm.lib.configuration.core.annotation.ConfigPath;
import cc.carm.lib.configuration.core.annotation.HeaderComment; import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.demo.tests.model.AbstractModel; import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable; import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
import config.model.AnyModel; import config.model.AnyModel;
import config.model.SomeModel; import config.model.SomeModel;
import java.util.Map;
@HeaderComment("以下内容用于测试序列化") @HeaderComment("以下内容用于测试序列化")
@ConfigPath("model-test") @ConfigPath("model-test")
public class ModelConfiguration extends ConfigurationRoot { public class ModelConfiguration extends ConfigurationRoot {
@@ -21,4 +24,11 @@ public class ModelConfiguration extends ConfigurationRoot {
AnyModel.class, AnyModel.random() 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();
} }
+4 -4
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>3.5.1</version> <version>3.6.0</version>
<modules> <modules>
<module>core</module> <module>core</module>
<module>demo</module> <module>demo</module>
@@ -122,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>3.0.0</version> <version>3.1.0</version>
<configuration> <configuration>
<useSystemClassLoader>false</useSystemClassLoader> <useSystemClassLoader>false</useSystemClassLoader>
</configuration> </configuration>
@@ -130,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>
@@ -150,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>