1
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:
Carm Jos 2023-05-20 09:34:30 +08:00
parent 2e61e66cdb
commit 43b00f2b69
7 changed files with 91 additions and 82 deletions

View File

@ -1,5 +1,6 @@
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;
@ -13,6 +14,7 @@ import java.util.Optional;
* 配置值描述清单用于描述一个配置值的相关基础信息
*
* @param <T> 配置值类型
* @author CarmJos
*/
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 configPath 配置路径

View File

@ -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.ValueManifest;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -11,7 +12,7 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
protected @Nullable T cachedValue;
protected long parsedTime = -1;
public CachedConfigValue(@NotNull ValueManifest<T> manifest) {
protected CachedConfigValue(@NotNull ValueManifest<T> manifest) {
super(manifest);
}
@ -29,12 +30,19 @@ public abstract class CachedConfigValue<T> extends ConfigValue<T> {
return this.parsedTime <= 0 || getProvider().isExpired(this.parsedTime);
}
protected final T useDefault() {
return useOrDefault(null);
}
protected final T useOrDefault(@Nullable T value) {
protected final T getDefaultFirst(@Nullable T value) {
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;
}
}

View File

@ -42,23 +42,22 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
@Override
public @NotNull List<V> get() {
if (isExpired()) { // 已过时的数据需要重新解析一次
List<V> list = new ArrayList<>();
List<?> data = getConfiguration().contains(getConfigPath()) ?
getConfiguration().getList(getConfigPath()) : null;
if (data == null) return useOrDefault(list);
for (Object dataVal : data) {
if (dataVal == null) continue;
try {
list.add(parser.parse(dataVal));
} catch (Exception e) {
e.printStackTrace();
}
if (!isExpired()) return getCachedOrDefault(new ArrayList<>());
// 已过时的数据需要重新解析一次
List<V> list = new ArrayList<>();
List<?> data = getConfiguration().contains(getConfigPath()) ?
getConfiguration().getList(getConfigPath()) : null;
if (data == null) return getDefaultFirst(list);
for (Object dataVal : data) {
if (dataVal == null) continue;
try {
list.add(parser.parse(dataVal));
} catch (Exception e) {
e.printStackTrace();
}
return updateCache(list);
} else if (getCachedValue() != null) return getCachedValue();
else if (getDefaultValue() != null) return getDefaultValue();
else return new ArrayList<>();
}
return updateCache(list);
}
@Override

View File

@ -78,31 +78,30 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
@Override
public @NotNull Map<K, V> get() {
if (isExpired()) { // 已过时的数据需要重新解析一次
Map<K, V> map = supplier.get();
if (!isExpired()) return getCachedOrDefault(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);
if (keys.isEmpty()) return useOrDefault(map);
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return getDefaultFirst(map);
for (String dataKey : keys) {
Object dataVal = section.get(dataKey);
if (dataVal == null) continue;
try {
K key = keyParser.parse(dataKey);
V value = valueParser.parse(dataVal);
map.put(key, value);
} catch (Exception e) {
e.printStackTrace();
}
Set<String> keys = section.getKeys(false);
if (keys.isEmpty()) return getDefaultFirst(map);
for (String dataKey : keys) {
Object dataVal = section.get(dataKey);
if (dataVal == null) continue;
try {
K key = keyParser.parse(dataKey);
V value = valueParser.parse(dataVal);
map.put(key, value);
} catch (Exception e) {
e.printStackTrace();
}
}
return updateCache(map);
} else if (getCachedValue() != null) return getCachedValue();
else if (getDefaultValue() != null) return getDefaultValue();
else return supplier.get();
return updateCache(map);
}
@Override

View File

@ -10,7 +10,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Optional;
public class ConfiguredSection<V> extends CachedConfigValue<V> {
@ -46,18 +45,21 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
@Override
public @Nullable V get() {
if (isExpired()) { // 已过时的数据需要重新解析一次
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return useDefault();
try {
// 若未出现错误则直接更新缓存并返回
return updateCache(this.parser.parse(section, this.defaultValue));
} catch (Exception e) {
// 出现了解析错误提示并返回默认值
e.printStackTrace();
return useDefault();
}
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
if (!isExpired()) return getCachedOrDefault();
// 已过时的数据需要重新解析一次
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
if (section == null) return getDefaultValue();
try {
// 若未出现错误则直接更新缓存并返回
return updateCache(this.parser.parse(section, this.defaultValue));
} catch (Exception e) {
// 出现了解析错误提示并返回默认值
e.printStackTrace();
return getDefaultValue();
}
}
@Override

View File

@ -8,8 +8,6 @@ import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
public class ConfiguredValue<V> extends CachedConfigValue<V> {
public static <V> ConfigValueBuilder<V> builderOf(Class<V> valueClass) {
@ -48,18 +46,20 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
@Override
public V get() {
if (isExpired()) { // 已过时的数据需要重新解析一次
Object value = getValue();
if (value == null) return useDefault(); // 获取的值不存在直接使用默认值
try {
// 若未出现错误则直接更新缓存并返回
return updateCache(this.parser.parse(value, this.defaultValue));
} catch (Exception e) {
// 出现了解析错误提示并返回默认值
e.printStackTrace();
return useDefault();
}
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
if (!isExpired()) return getCachedOrDefault();
// 已过时的数据需要重新解析一次
Object value = getValue();
if (value == null) return getDefaultValue(); // 获取的值不存在直接使用默认值
try {
// 若未出现错误则直接更新缓存并返回
return updateCache(this.parser.parse(value, this.defaultValue));
} catch (Exception e) {
// 出现了解析错误提示并返回默认值
e.printStackTrace();
return getDefaultValue();
}
}
@Override

View File

@ -6,8 +6,6 @@ import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> {
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
@ -28,16 +26,17 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
@Override
public @Nullable T get() {
if (isExpired()) { // 已过时的数据需要重新解析一次
try {
// 若未出现错误则直接更新缓存并返回
return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
} catch (Exception e) {
// 出现了解析错误提示并返回默认值
e.printStackTrace();
return useDefault();
}
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
if (!isExpired()) return getCachedOrDefault();
try {
// 若未出现错误则直接更新缓存并返回
return updateCache(getYAMLConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
} catch (Exception e) {
// 出现了解析错误提示并返回默认值
e.printStackTrace();
return getDefaultValue();
}
}
@Override