mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2024-09-19 20:25:51 +00:00
refactor(api): 🤖 优化代码命名逻辑,补充部分Javadoc。
This commit is contained in:
parent
2e61e66cdb
commit
43b00f2b69
@ -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;
|
||||||
@ -13,6 +14,7 @@ import java.util.Optional;
|
|||||||
* 配置值描述清单。用于描述一个配置值的相关基础信息。
|
* 配置值描述清单。用于描述一个配置值的相关基础信息。
|
||||||
*
|
*
|
||||||
* @param <T> 配置值类型
|
* @param <T> 配置值类型
|
||||||
|
* @author CarmJos
|
||||||
*/
|
*/
|
||||||
public class ValueManifest<T> {
|
public class ValueManifest<T> {
|
||||||
|
|
||||||
@ -53,7 +55,7 @@ public class ValueManifest<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 此方法用于 {@link cc.carm.lib.configuration.core.ConfigInitializer<T>} 工具对配置值进行统一初始化操作。
|
* 此方法提供给 {@link ConfigInitializer},以便对配置值的相关信息进行统一初始化操作。
|
||||||
*
|
*
|
||||||
* @param provider 配置文件提供者
|
* @param provider 配置文件提供者
|
||||||
* @param configPath 配置路径
|
* @param configPath 配置路径
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,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 {
|
||||||
@ -56,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
|
||||||
|
@ -78,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);
|
||||||
@ -100,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,7 +10,6 @@ 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> {
|
||||||
|
|
||||||
@ -46,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,8 +8,6 @@ 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> builderOf(Class<V> valueClass) {
|
public static <V> ConfigValueBuilder<V> builderOf(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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user