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; 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 配置路径

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.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;
}
} }

View File

@ -42,23 +42,22 @@ 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<?> data = getConfiguration().contains(getConfigPath()) ? // 已过时的数据需要重新解析一次
getConfiguration().getList(getConfigPath()) : null; List<V> list = new ArrayList<>();
if (data == null) return useOrDefault(list); List<?> data = getConfiguration().contains(getConfigPath()) ?
for (Object dataVal : data) { getConfiguration().getList(getConfigPath()) : null;
if (dataVal == null) continue; if (data == null) return getDefaultFirst(list);
try { for (Object dataVal : data) {
list.add(parser.parse(dataVal)); if (dataVal == null) continue;
} catch (Exception e) { try {
e.printStackTrace(); 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 @Override

View File

@ -78,31 +78,30 @@ 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();
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

View File

@ -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()); // 已过时的数据需要重新解析一次
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

View File

@ -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(); // 已过时的数据需要重新解析一次
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

View File

@ -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())); // 若未出现错误则直接更新缓存并返回
} 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