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

chore: optimize some codes

This commit is contained in:
2024-01-04 22:08:12 +08:00
parent 34bc7601ee
commit 3769cf4438
8 changed files with 72 additions and 64 deletions
@@ -8,26 +8,26 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* 用于标记对应类或参数的配置路径 * The configuration path used to mark the corresponding class or parameter.
*/ */
@Target({ElementType.TYPE, ElementType.FIELD}) @Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface ConfigPath { public @interface ConfigPath {
/** /**
* 指定路径的值。 * The path value of the current configuration.
* 若不指定,则会通过 {@link ConfigInitializer#getPathFromName(String)} 自动生成当前路径的值。 * If not set,will generate the path by {@link ConfigInitializer#getPathFromName(String)}.
* *
* @return 路径的值 * @return The path value of the current configuration
*/ */
String value() default ""; String value() default "";
/** /**
* 是否从根节点开始。 * Whether to start with the root node.
* <br>若为 false,则会自动添加上一个路径(如果有)到本节点的路径。 * <br>If false, the previous path (if any) to the node is automatically added.
* <br>若为 true,则会从根节点开始直接设置本路径。 * <br>If true, the path will be set directly from the root node.
* *
* @return 是否不继承上一路径,直接从根路径为开始 * @return Whether to start directly from the root path without inheriting the previous path
*/ */
boolean root() default false; boolean root() default false;
@@ -8,11 +8,12 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* 顶部注释,用于给对应配置的顶部添加注释,便于使用者阅读查看。 * Header Comments.
* <p>如: * Add a comment to the top of the corresponding configuration for easy reading and viewing.
* <p>e.g.
* <blockquote><pre> * <blockquote><pre>
* # 注释第一行 * # The first line of the comment
* # 注释第二行 * # The second line of the comment
* foo: "bar" * foo: "bar"
* </pre></blockquote> * </pre></blockquote>
*/ */
@@ -21,9 +22,9 @@ import java.lang.annotation.Target;
public @interface HeaderComment { public @interface HeaderComment {
/** /**
* 注释内容,若内容长度为0则会视为一个空行。 * If the content of the note is 0, it will be treated as a blank line.
* <p> <b>{"foo","","bar"}</b> * <p> e.g. <b>{"foo","","bar"}</b>
* 会被添加为 * Will be set as
* <blockquote><pre> * <blockquote><pre>
* # foo * # foo
* *
@@ -31,7 +32,7 @@ public @interface HeaderComment {
* foo: "bar" * foo: "bar"
* </pre></blockquote> * </pre></blockquote>
* *
* @return 注释内容 * @return The content of this comment
*/ */
@NotNull @NotNull
String[] value() default ""; String[] value() default "";
@@ -8,10 +8,11 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* 行内注释,用于给对应配置的所在行添加注释,便于使用者阅读查看。 * Inline comments,
* 如: * add comments to the rows of the corresponding configurations for easy reading and viewing.
* e.g.
* <blockquote><pre> * <blockquote><pre>
* foo: "bar" # 注释内容 * foo: "bar" # Comment Contents
* </pre></blockquote> * </pre></blockquote>
*/ */
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@@ -19,13 +20,13 @@ import java.lang.annotation.Target;
public @interface InlineComment { public @interface InlineComment {
/** /**
* 注释内容,若内容长度为0则不会添加注释 * If the content length is 0, the comment will not be added.
* <p> <b>"foobar"</b> 将被设定为 * <p> e.g. <b>"foobar"</b> will be set
* <blockquote><pre> * <blockquote><pre>
* foo: "bar" # foobar * foo: "bar" # foobar
* </pre></blockquote> * </pre></blockquote>
* *
* @return 注释内容 * @return The content of this comment
*/ */
@NotNull @NotNull
String value() default ""; String value() default "";
@@ -1,8 +1,8 @@
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 cc.carm.lib.configuration.core.value.ValueManifest;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -22,7 +22,7 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
protected @Nullable T defaultValue; protected @Nullable T defaultValue;
public AbstractConfigBuilder(Class<? super P> providerClass) { protected AbstractConfigBuilder(Class<? super P> providerClass) {
this.providerClass = providerClass; this.providerClass = providerClass;
} }
@@ -5,7 +5,7 @@ import cc.carm.lib.configuration.core.source.ConfigurationProvider;
public abstract class CommonConfigBuilder<T, B extends CommonConfigBuilder<T, B>> public abstract class CommonConfigBuilder<T, B extends CommonConfigBuilder<T, B>>
extends AbstractConfigBuilder<T, B, ConfigurationProvider<?>> { extends AbstractConfigBuilder<T, B, ConfigurationProvider<?>> {
public CommonConfigBuilder() { protected CommonConfigBuilder() {
super(ConfigurationProvider.class); super(ConfigurationProvider.class);
} }
@@ -50,22 +50,9 @@ 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) -> { if (Number.class.isAssignableFrom(valueClass)) return castNumber(valueClass);
if (Number.class.isAssignableFrom(valueClass)) { else return (input, defaultValue) -> {
if (Long.class.isAssignableFrom(valueClass) || long.class.isAssignableFrom(valueClass)) { if (Boolean.class.isAssignableFrom(valueClass) || boolean.class.isAssignableFrom(valueClass)) {
input = longValue().parse(input, (Long) defaultValue);
} else if (Integer.class.isAssignableFrom(valueClass) || int.class.isAssignableFrom(valueClass)) {
input = intValue().parse(input, (Integer) defaultValue);
} else if (Float.class.isAssignableFrom(valueClass) || float.class.isAssignableFrom(valueClass)) {
input = floatValue().parse(input, (Float) defaultValue);
} else if (Double.class.isAssignableFrom(valueClass) || double.class.isAssignableFrom(valueClass)) {
input = doubleValue().parse(input, (Double) defaultValue);
} else if (Byte.class.isAssignableFrom(valueClass) || byte.class.isAssignableFrom(valueClass)) {
input = byteValue().parse(input, (Byte) defaultValue);
} else if (Short.class.isAssignableFrom(valueClass) || short.class.isAssignableFrom(valueClass)) {
input = shortValue().parse(input, (Short) defaultValue);
}
} else if (Boolean.class.isAssignableFrom(valueClass) || boolean.class.isAssignableFrom(valueClass)) {
input = booleanValue().parse(input, (Boolean) defaultValue); input = booleanValue().parse(input, (Boolean) defaultValue);
} else if (Enum.class.isAssignableFrom(valueClass) && input instanceof String) { } else if (Enum.class.isAssignableFrom(valueClass) && input instanceof String) {
String enumName = (String) input; String enumName = (String) input;
@@ -79,6 +66,28 @@ public interface ConfigValueParser<T, R> {
}; };
} }
@Contract(pure = true)
static <V> @NotNull ConfigValueParser<Object, V> castNumber(Class<V> valueClass) {
return (input, defaultValue) -> {
if (Long.class.isAssignableFrom(valueClass) || long.class.isAssignableFrom(valueClass)) {
input = longValue().parse(input, (Long) defaultValue);
} else if (Integer.class.isAssignableFrom(valueClass) || int.class.isAssignableFrom(valueClass)) {
input = intValue().parse(input, (Integer) defaultValue);
} else if (Float.class.isAssignableFrom(valueClass) || float.class.isAssignableFrom(valueClass)) {
input = floatValue().parse(input, (Float) defaultValue);
} else if (Double.class.isAssignableFrom(valueClass) || double.class.isAssignableFrom(valueClass)) {
input = doubleValue().parse(input, (Double) defaultValue);
} else if (Byte.class.isAssignableFrom(valueClass) || byte.class.isAssignableFrom(valueClass)) {
input = byteValue().parse(input, (Byte) defaultValue);
} else if (Short.class.isAssignableFrom(valueClass) || short.class.isAssignableFrom(valueClass)) {
input = shortValue().parse(input, (Short) defaultValue);
}
if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName());
};
}
@Contract(pure = true) @Contract(pure = true)
static <V> @NotNull ConfigValueParser<String, V> parseString(Class<V> valueClass) { static <V> @NotNull ConfigValueParser<String, V> parseString(Class<V> valueClass) {
return (input, defaultValue) -> { return (input, defaultValue) -> {
@@ -20,12 +20,12 @@ public abstract class ConfigValue<T> extends ValueManifest<T> {
} }
/** /**
* @param provider 配置文件提供者 * @param provider Provider of config files {@link ConfigurationProvider}
* @param configPath 配置路径 * @param configPath Config path of this value
* @param headerComments 头部注释内容 * @param headerComments Header comment contents
* @param inlineComments 行内注释内容 * @param inlineComments Inline comment contents
* @param defaultValue 默认参数值 * @param defaultValue The default value
* @deprecated 请使用 {@link #ConfigValue(ValueManifest)} 构造器。 * @deprecated Please use {@link #ConfigValue(ValueManifest)} instead.
*/ */
@Deprecated @Deprecated
protected ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath, protected ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@@ -104,11 +104,7 @@ public abstract class ConfigValue<T> extends ValueManifest<T> {
* @return 获取当前值是否为默认值。 * @return 获取当前值是否为默认值。
*/ */
public boolean isDefault() { public boolean isDefault() {
T defaultValue = getDefaultValue(); return Objects.equals(getDefaultValue(), get());
T value = get();
if (defaultValue == null && value == null) return true;
else if (defaultValue != null && value != null) return defaultValue.equals(value);
else return false;
} }
} }
@@ -11,9 +11,10 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
/** /**
* 配置值描述清单。用于描述一个配置值的相关基础信息。 * ConfigValue Manifests.
* The basic information that describes a configuration value.
* *
* @param <T> 配置值类型 * @param <T> Value type
* @author CarmJos * @author CarmJos
*/ */
public class ValueManifest<T> { public class ValueManifest<T> {
@@ -38,11 +39,11 @@ public class ValueManifest<T> {
protected @Nullable T defaultValue; protected @Nullable T defaultValue;
/** /**
* @param provider 配置文件提供者 * @param provider Provider of config files {@link ConfigurationProvider}
* @param configPath 配置路径 * @param configPath Config path of this value
* @param headerComments 头部注释内容 * @param headerComments Header comment contents
* @param inlineComment 行内注释内容 * @param inlineComment Inline comment content
* @param defaultValue 默认参数值 * @param defaultValue The default value
*/ */
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,
@@ -55,12 +56,12 @@ public class ValueManifest<T> {
} }
/** /**
* 此方法提供给 {@link ConfigInitializer},以便对配置值的相关信息进行统一初始化操作。 * The initialize method for {@link ConfigInitializer}, which is used to initialize the value.
* *
* @param provider 配置文件提供者 * @param provider Provider of config files {@link ConfigurationProvider}
* @param configPath 配置路径 * @param configPath Config path of this value
* @param headerComments 头部注释内容 * @param headerComments Header comment contents
* @param inlineComment 行内注释内容 * @param inlineComment Inline comment content
*/ */
protected void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath, protected void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComment) { @Nullable List<String> headerComments, @Nullable String inlineComment) {