From a440c050e56417f9fd4a89977c7e7ea129805c6d Mon Sep 17 00:00:00 2001 From: carm Date: Mon, 23 Jun 2025 15:27:52 +0800 Subject: [PATCH] feat(type): Add `ofMap(K,V)` method for ValueType --- core/pom.xml | 2 +- .../lib/configuration/adapter/ValueType.java | 61 +++++++++++++++++-- demo/pom.xml | 2 +- features/commentable/pom.xml | 2 +- features/file/pom.xml | 2 +- features/kotlin/pom.xml | 2 +- features/section/pom.xml | 2 +- features/text/pom.xml | 2 +- features/validators/pom.xml | 2 +- features/versioned/pom.xml | 2 +- pom.xml | 2 +- providers/gson/pom.xml | 2 +- providers/hocon/pom.xml | 2 +- providers/mongodb/pom.xml | 2 +- providers/sql/pom.xml | 2 +- providers/yaml/pom.xml | 2 +- 16 files changed, 71 insertions(+), 20 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 1acf913..75f007e 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -5,7 +5,7 @@ configured-parent cc.carm.lib - 4.1.5 + 4.1.6 4.0.0 diff --git a/core/src/main/java/cc/carm/lib/configuration/adapter/ValueType.java b/core/src/main/java/cc/carm/lib/configuration/adapter/ValueType.java index 6a97c38..1b8616f 100644 --- a/core/src/main/java/cc/carm/lib/configuration/adapter/ValueType.java +++ b/core/src/main/java/cc/carm/lib/configuration/adapter/ValueType.java @@ -1,14 +1,22 @@ package cc.carm.lib.configuration.adapter; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; +import java.util.Map; import java.util.Objects; /** - * Used to get the generic type. + * {@link ValueType} used to get the generic type of the value, + * It can be used to check if an object is an instance of a specific type, + * and to cast objects to the correct type. + *

+ * Java's type system is not capable of retaining generic type information at runtime. + * This class is used to represent a type with its generic parameters. + *

*/ public abstract class ValueType { @@ -43,7 +51,7 @@ public abstract class ValueType { @SuppressWarnings("unchecked") public static ValueType of(final Type type) { if (type == null) throw new NullPointerException("Type cannot be null"); - if (type instanceof Class) { // Try handle primitive types + if (type instanceof Class) { // Try to fast handle primitive types Class clazz = (Class) type; for (ValueType valueType : PRIMITIVE_TYPES) { if (valueType.getRawType() == clazz) { @@ -63,6 +71,18 @@ public abstract class ValueType { return of(List.class, paramType); } + public static ValueType> ofList(final @NotNull ValueType paramType) { + return of(List.class, paramType.getType()); + } + + public static ValueType> ofMap(final @NotNull Class keyType, final @NotNull Class valueType) { + return of(Map.class, keyType, valueType); + } + + public static ValueType> ofMap(final @NotNull ValueType keyType, final @NotNull ValueType valueType) { + return of(Map.class, keyType.getType(), valueType.getType()); + } + /** * Get the generic type of the complex type. * @@ -91,6 +111,7 @@ public abstract class ValueType { return of(parameterizedType); } + @ApiStatus.Internal private static ValueType ofPrimitiveType(Class clazz) { return new ValueType(clazz) { }; @@ -110,25 +131,43 @@ public abstract class ValueType { return type; } + /** + * Checks if this ValueType is a subtype of the given Class. + * + * @param target The target Class to check against + * @return true if this ValueType is a subtype of the target Class, false otherwise + */ public boolean isSubtypeOf(Class target) { Class rawType = getRawType(); return target.isAssignableFrom(rawType); } + /** + * Checks if this ValueType is a subtype of the given ValueType. + * + * @param target The target ValueType to check against + * @return true if this ValueType is a subtype of the target, false otherwise + */ public boolean isSubtypeOf(ValueType target) { return target.isSubtypeOf(getRawType()); } + /** + * Checks if the given object is an instance of the type represented by this ValueType. + * + * @param obj The object to check + * @return true if the object is an instance of the type, false otherwise + */ public boolean isInstance(Object obj) { return obj != null && getRawType().isInstance(obj); } /** - * 提取当前 ValueType 的原始类型(Class 对象)。 + * Extracts the raw type from the generic type. * - * @return 对应的 Class 对象 - * @throws IllegalStateException 如果无法提取出原始类型 + * @return The raw type of the generic type + * @throws IllegalStateException if the type is not a Class or ParameterizedType */ public Class getRawType() { if (type instanceof Class) { @@ -144,6 +183,12 @@ public abstract class ValueType { throw new IllegalStateException("Unsupported type: " + type); } + /** + * Casts the object to the type represented by this ValueType. + * + * @param obj The object to cast + * @return The object cast to the type represented by this ValueType + */ @SuppressWarnings("unchecked") public T cast(Object obj) { if (!isInstance(obj)) { @@ -152,6 +197,12 @@ public abstract class ValueType { return (T) obj; } + /** + * Returns a string representation of the type. + * Like "{@code java.util.List}" or "java.lang.Integer". + * + * @return String representation of the type + */ @Override public String toString() { if (type instanceof Class) { diff --git a/demo/pom.xml b/demo/pom.xml index 0b15618..1476e88 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -5,7 +5,7 @@ configured-parent cc.carm.lib - 4.1.5 + 4.1.6 4.0.0 diff --git a/features/commentable/pom.xml b/features/commentable/pom.xml index c7447c6..dc36123 100644 --- a/features/commentable/pom.xml +++ b/features/commentable/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/features/file/pom.xml b/features/file/pom.xml index 50bddc3..5e0c270 100644 --- a/features/file/pom.xml +++ b/features/file/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/features/kotlin/pom.xml b/features/kotlin/pom.xml index db7fc72..34bf9e2 100644 --- a/features/kotlin/pom.xml +++ b/features/kotlin/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/features/section/pom.xml b/features/section/pom.xml index d27a473..09442d5 100644 --- a/features/section/pom.xml +++ b/features/section/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/features/text/pom.xml b/features/text/pom.xml index 1a1856c..be48dca 100644 --- a/features/text/pom.xml +++ b/features/text/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/features/validators/pom.xml b/features/validators/pom.xml index 7775787..7e7e423 100644 --- a/features/validators/pom.xml +++ b/features/validators/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/features/versioned/pom.xml b/features/versioned/pom.xml index 3539622..878c8b6 100644 --- a/features/versioned/pom.xml +++ b/features/versioned/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/pom.xml b/pom.xml index 84f55f5..4fb824a 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ cc.carm.lib configured-parent pom - 4.1.5 + 4.1.6 core features/section diff --git a/providers/gson/pom.xml b/providers/gson/pom.xml index 2e72f24..adfd6c2 100644 --- a/providers/gson/pom.xml +++ b/providers/gson/pom.xml @@ -5,7 +5,7 @@ configured-parent cc.carm.lib - 4.1.5 + 4.1.6 ../../pom.xml 4.0.0 diff --git a/providers/hocon/pom.xml b/providers/hocon/pom.xml index 877afc3..7b068db 100644 --- a/providers/hocon/pom.xml +++ b/providers/hocon/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib configured-parent - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/providers/mongodb/pom.xml b/providers/mongodb/pom.xml index 41f6281..418cb9b 100644 --- a/providers/mongodb/pom.xml +++ b/providers/mongodb/pom.xml @@ -5,7 +5,7 @@ configured-parent cc.carm.lib - 4.1.5 + 4.1.6 ../../pom.xml 4.0.0 diff --git a/providers/sql/pom.xml b/providers/sql/pom.xml index dd72b16..a34fa5a 100644 --- a/providers/sql/pom.xml +++ b/providers/sql/pom.xml @@ -6,7 +6,7 @@ configured-parent cc.carm.lib - 4.1.5 + 4.1.6 ../../pom.xml diff --git a/providers/yaml/pom.xml b/providers/yaml/pom.xml index 7d52d17..9d0002d 100644 --- a/providers/yaml/pom.xml +++ b/providers/yaml/pom.xml @@ -6,7 +6,7 @@ configured-parent cc.carm.lib - 4.1.5 + 4.1.6 ../../pom.xml