From 502bcd12ef11fa7d2ae380993f6190d0d7983ede Mon Sep 17 00:00:00 2001 From: zhaoyong Date: Thu, 5 Aug 2021 19:37:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E8=A7=A3=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common-common/pom.xml | 2 +- .../common/annotation/EnableEnum.java | 46 +++++++++++++++++ .../common/annotation/ValidNumberValue.java | 49 +++++++++++++++++++ .../support/EnableEnumValidator.java | 49 +++++++++++++++++++ .../support/ValidNumberValueValidator.java | 38 ++++++++++++++ .../azxo/framework/common/model/EnumBase.java | 24 +++++++++ .../framework/common/utils/ArrayUtils.java | 22 +++++++++ 7 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/EnableEnum.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/ValidNumberValue.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/support/EnableEnumValidator.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidNumberValueValidator.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/model/EnumBase.java diff --git a/common-common/pom.xml b/common-common/pom.xml index d16171f..0d33660 100644 --- a/common-common/pom.xml +++ b/common-common/pom.xml @@ -6,7 +6,7 @@ cn.axzo.framework common-common - 1.0.6 + 1.0.7 UTF-8 diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/EnableEnum.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/EnableEnum.java new file mode 100644 index 0000000..4bbdd95 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/EnableEnum.java @@ -0,0 +1,46 @@ +package cn.azxo.framework.common.annotation; + +import cn.azxo.framework.common.annotation.support.EnableEnumValidator; +import cn.azxo.framework.common.model.EnumBase; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * 枚举验证类 + * + * @author zhaoyong + * @see EnableEnum + * @since 2021-06-22 14:53 + */ +@Target({ FIELD}) +@Retention(RUNTIME) +@Constraint(validatedBy = EnableEnumValidator.class) +@Documented +public @interface EnableEnum { + + Class value(); + + String message() default "invalid value"; + + Class[] groups() default { }; + + Class[] payload() default {}; + + @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) + @Retention(RUNTIME) + @Documented + @interface List { + EnableEnum[] value(); + } + +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/ValidNumberValue.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/ValidNumberValue.java new file mode 100644 index 0000000..9229695 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/ValidNumberValue.java @@ -0,0 +1,49 @@ +package cn.azxo.framework.common.annotation; + +import cn.azxo.framework.common.annotation.support.ValidNumberValueValidator; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * 有效的 int 值 + * + * @author zhaoyong + * @see ValidNumberValue + * @since 2021-08-05 19:28 + */ +@Target({ FIELD}) +@Retention(RUNTIME) +@Constraint(validatedBy = ValidNumberValueValidator.class) +@Documented +public @interface ValidNumberValue { + + /** + * 有效字符串集合类 + * @return + */ + int[] value() default {}; + + String message() default "invalid value"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) + @Retention(RUNTIME) + @Documented + @interface List { + ValidNumberValue[] value(); + } + +} \ No newline at end of file diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/support/EnableEnumValidator.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/EnableEnumValidator.java new file mode 100644 index 0000000..2fc45eb --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/EnableEnumValidator.java @@ -0,0 +1,49 @@ +package cn.azxo.framework.common.annotation.support; + +import cn.azxo.framework.common.annotation.EnableEnum; +import cn.azxo.framework.common.model.EnumBase; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.lang.reflect.Modifier; + +/** + * EnableEnum 注解实现类 + * + * @author zhaoyong + * @see EnableEnumValidator + * @since 2021-06-22 14:59 + */ +public class EnableEnumValidator implements ConstraintValidator { + + private Class enumBaseClazz; + + @Override + public void initialize(EnableEnum constraintAnnotation) { + enumBaseClazz = constraintAnnotation.value(); + } + + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { + if(enumBaseClazz == null) { + return false; + } + if(enumBaseClazz.isInterface() || Modifier.isAbstract(enumBaseClazz.getModifiers())){ + return false; + } + if(!enumBaseClazz.isEnum()){ + return false; + } + if(value == null){ + return false; + } + EnumBase[] enumConstants = enumBaseClazz.getEnumConstants(); + for (EnumBase enumConstant : enumConstants) { + if(enumConstant.getCode().equals(value)){ + return true; + } + } + return false; + } + +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidNumberValueValidator.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidNumberValueValidator.java new file mode 100644 index 0000000..7a217fb --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidNumberValueValidator.java @@ -0,0 +1,38 @@ +package cn.azxo.framework.common.annotation.support; + +import cn.azxo.framework.common.annotation.ValidNumberValue; +import cn.azxo.framework.common.utils.ArrayUtils; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.util.Objects; + +/** + * ValidValue 注解实现类 + * + * @author zhaoyong_sh + * @see ValidNumberValueValidator + * @since 2020-10-10 13:57 + */ +public class ValidNumberValueValidator implements ConstraintValidator { + + private int[] validValues; + + @Override + public void initialize(ValidNumberValue constraintAnnotation) { + validValues = constraintAnnotation.value(); + } + + @Override + public boolean isValid(Integer value, ConstraintValidatorContext context) { + if(ArrayUtils.isEmpty(validValues)){ + return false; + } + for (int validValue : validValues) { + if(Objects.equals(validValue, value)){ + return true; + } + } + return false; + } +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/model/EnumBase.java b/common-common/src/main/java/cn/azxo/framework/common/model/EnumBase.java new file mode 100644 index 0000000..0561067 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/model/EnumBase.java @@ -0,0 +1,24 @@ +package cn.azxo.framework.common.model; + +/** + * 枚举抽象类 + * + * @author zhaoyong + * @see EnumBase + * @since 2021-08-05 19:13 + */ +public interface EnumBase { + + /** + * 枚举 code + * @return + */ + T getCode(); + + /** + * 枚举描述信息 + * @return + */ + String getMessage(); + +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java b/common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java index e43e2dd..4f0f43d 100644 --- a/common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java +++ b/common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java @@ -51,4 +51,26 @@ public abstract class ArrayUtils { return Array.getLength(array); } + /** + *

Checks if an array of primitive ints is not empty and not {@code null}. + * + * @param array the array to test + * @return {@code true} if the array is not empty and not {@code null} + * @since 2.5 + */ + public static boolean isNotEmpty(final int[] array) { + return !isEmpty(array); + } + + /** + *

Checks if an array of primitive ints is empty or {@code null}. + * + * @param array the array to test + * @return {@code true} if the array is empty or {@code null} + * @since 2.1 + */ + public static boolean isEmpty(final int[] array) { + return getLength(array) == 0; + } + }