From c8305fb67e834c9a5d013ce35d3b81890be77612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Sun, 27 Feb 2022 12:49:41 +0800 Subject: [PATCH] feat($Enum): abstract common methods for enum --- .../service/impl/UserDomainServiceImpl.java | 3 +- .../maf/common/bean/EnumerationBase.java | 18 -- .../domain/authcenter/user/UserStatus.java | 23 +-- .../domain/authcenter/user/UserStatus2.java | 23 +-- .../enumeration/ValueDescriptionBaseEnum.java | 175 ++++++++++++++++++ .../quartz/constant/Concurrent.java | 11 +- .../quartz/constant/MisfirePolicy.java | 9 +- .../quartz/constant/QuartzJobStatus.java | 9 +- .../validation/annotation/ValidEnumValue.java | 2 +- .../validator/EnumValueValidator.java | 9 +- 10 files changed, 211 insertions(+), 71 deletions(-) delete mode 100644 common/src/main/java/com/jmsoftware/maf/common/bean/EnumerationBase.java create mode 100644 common/src/main/java/com/jmsoftware/maf/common/enumeration/ValueDescriptionBaseEnum.java diff --git a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserDomainServiceImpl.java b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserDomainServiceImpl.java index c92cc6d8..2eb48689 100644 --- a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserDomainServiceImpl.java +++ b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserDomainServiceImpl.java @@ -21,6 +21,7 @@ import com.jmsoftware.maf.authcenter.user.service.UserRoleDomainService; import com.jmsoftware.maf.common.bean.PageResponseBodyBean; import com.jmsoftware.maf.common.domain.authcenter.user.*; +import com.jmsoftware.maf.common.enumeration.ValueDescriptionBaseEnum; import com.jmsoftware.maf.common.exception.SecurityException; import com.jmsoftware.maf.springcloudstarter.property.MafConfigurationProperties; import com.jmsoftware.maf.springcloudstarter.property.MafProjectProperties; @@ -132,7 +133,7 @@ public boolean logout(HttpServletRequest request) throws SecurityException { @Override public String getUserStatus(@Valid @NotNull GetUserStatusPayload payload) { log.info("Current username: {}", UserUtil.getCurrentUsername()); - return UserStatus.ofValue(payload.getStatus()).getDescription(); + return ValueDescriptionBaseEnum.getDescriptionByValue(UserStatus.class, payload.getStatus()); } @Override diff --git a/common/src/main/java/com/jmsoftware/maf/common/bean/EnumerationBase.java b/common/src/main/java/com/jmsoftware/maf/common/bean/EnumerationBase.java deleted file mode 100644 index 4d56b063..00000000 --- a/common/src/main/java/com/jmsoftware/maf/common/bean/EnumerationBase.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jmsoftware.maf.common.bean; - -/** - *

EnumerationBase

- *

- * Change description here. - * - * @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com - * @date 6/6/21 5:38 PM - **/ -public interface EnumerationBase { - /** - * Gets value. - * - * @return the value - */ - T getValue(); -} diff --git a/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus.java b/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus.java index 6a802679..25c90fb6 100644 --- a/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus.java +++ b/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus.java @@ -1,7 +1,8 @@ package com.jmsoftware.maf.common.domain.authcenter.user; -import com.jmsoftware.maf.common.bean.EnumerationBase; +import com.jmsoftware.maf.common.enumeration.ValueDescriptionBaseEnum; import lombok.Getter; +import lombok.ToString; /** * Description: UserStatus, change description here. @@ -10,7 +11,8 @@ * @date 2019-03-23 18:48 **/ @Getter -public enum UserStatus implements EnumerationBase { +@ToString +public enum UserStatus implements ValueDescriptionBaseEnum { /** * Enabled user */ @@ -27,21 +29,4 @@ public enum UserStatus implements EnumerationBase { this.value = value; this.description = description; } - - /** - * Get user value enum by value value - * - * @param value value value - * @return user value enum - */ - public static UserStatus ofValue(Byte value) { - UserStatus result = UserStatus.DISABLED; - UserStatus[] userStatuses = UserStatus.values(); - for (UserStatus userStatus : userStatuses) { - if (userStatus.value.equals(value)) { - result = userStatus; - } - } - return result; - } } diff --git a/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus2.java b/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus2.java index e7ad7307..cd1b6795 100644 --- a/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus2.java +++ b/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/UserStatus2.java @@ -1,7 +1,8 @@ package com.jmsoftware.maf.common.domain.authcenter.user; -import com.jmsoftware.maf.common.bean.EnumerationBase; +import com.jmsoftware.maf.common.enumeration.ValueDescriptionBaseEnum; import lombok.Getter; +import lombok.ToString; /** * Description: UserStatus, change description here. @@ -10,7 +11,8 @@ * @date 2019-03-23 18:48 **/ @Getter -public enum UserStatus2 implements EnumerationBase { +@ToString +public enum UserStatus2 implements ValueDescriptionBaseEnum { /** * Enabled user */ @@ -27,21 +29,4 @@ public enum UserStatus2 implements EnumerationBase { this.value = value; this.description = description; } - - /** - * Get user value enum by value value - * - * @param value value value - * @return user value enum - */ - public static UserStatus2 ofValue(Byte value) { - UserStatus2 result = UserStatus2.DISABLED; - UserStatus2[] userStatuses = UserStatus2.values(); - for (UserStatus2 userStatus : userStatuses) { - if (userStatus.value.equals(value)) { - result = userStatus; - } - } - return result; - } } diff --git a/common/src/main/java/com/jmsoftware/maf/common/enumeration/ValueDescriptionBaseEnum.java b/common/src/main/java/com/jmsoftware/maf/common/enumeration/ValueDescriptionBaseEnum.java new file mode 100644 index 00000000..19d2662b --- /dev/null +++ b/common/src/main/java/com/jmsoftware/maf/common/enumeration/ValueDescriptionBaseEnum.java @@ -0,0 +1,175 @@ +package com.jmsoftware.maf.common.enumeration; + +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.EnumUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import lombok.NonNull; +import lombok.val; + +/** + *

ValueDescriptionBaseEnum

+ *

+ * Change description here. + * + * @param the type of value + * @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 2/27/22 11:32 AM + */ +@SuppressWarnings("unused") +public interface ValueDescriptionBaseEnum { + /** + * Gets value, which represents the value stored in database + * + * @return the value + */ + V getValue(); + + /** + * Gets description, which indicates what the value represents for. + * + * @return the description + */ + String getDescription(); + + /** + * Check if the value exists in the enums. + * + * @param the type parameter + * @param enums the enums + * @param value the value + * @return the boolean + */ + static boolean exists(@NonNull ValueDescriptionBaseEnum[] enums, T value) { + if (!EnumUtil.isEnum(enums[0])) { + throw new IllegalArgumentException("Not an enum type"); + } + if (value == null) { + return false; + } + if (ArrayUtil.isEmpty(enums)) { + return false; + } + for (ValueDescriptionBaseEnum e : enums) { + if (value.equals(e.getValue())) { + return true; + } + } + return false; + } + + /** + * Exists boolean. + * + * @param the type of enum + * @param the type of value + * @param enumClass the enum class + * @param value the value + * @return the boolean + */ + @SuppressWarnings("unchecked") + static >, V> boolean exists(@NonNull Class enumClass, V value) { + for (Enum> e : enumClass.getEnumConstants()) { + if (((ValueDescriptionBaseEnum) e).getValue().equals(value)) { + return true; + } + } + return false; + } + + /** + * Gets description by value. + * + * @param the type parameter + * @param the type parameter + * @param enumClass the enum class + * @param value the value + * @return the description by value + */ + @SuppressWarnings("unchecked") + static >, V> String getDescriptionByValue(@NonNull Class enumClass, V value) { + if (value == null) { + return null; + } + for (Enum> e : enumClass.getEnumConstants()) { + if (((ValueDescriptionBaseEnum) e).getValue().equals(value)) { + return ((ValueDescriptionBaseEnum) e).getDescription(); + } + } + return null; + } + + /** + * Gets by enum name. + * + * @param the type parameter + * @param the type parameter + * @param enumClass the enum class + * @param enumName the enum name + * @return the by enum name + */ + @SuppressWarnings("unchecked") + static >, V> E getByEnumName( + @NonNull Class enumClass, + String enumName + ) { + if (StrUtil.isEmpty(enumName)) { + return null; + } + for (Enum> e : enumClass.getEnumConstants()) { + if (StrUtil.equalsIgnoreCase(e.name(), enumName)) { + return (E) e; + } + } + return null; + } + + + /** + * Gets value by enum name. + * + * @param the type parameter + * @param the type parameter + * @param enumClass the enum class + * @param enumName the enum name + * @return the value by enum name + */ + @SuppressWarnings("unchecked") + static >, V> V getValueByEnumName( + @NonNull Class enumClass, + String enumName + ) { + if (StrUtil.isEmpty(enumName)) { + return null; + } + val e = ((ValueDescriptionBaseEnum) getByEnumName(enumClass, enumName)); + if (e != null) { + return e.getValue(); + } + return null; + } + + /** + * Gets by value. + * + * @param the type parameter + * @param the type parameter + * @param enumClass the enum class + * @param value the value + * @return the by value + */ + @SuppressWarnings("unchecked") + static >, V> E getByValue( + @NonNull Class enumClass, + V value + ) { + if (ObjectUtil.isNull(value)) { + return null; + } + for (Enum> e : enumClass.getEnumConstants()) { + if (((ValueDescriptionBaseEnum) e).getValue().equals(value)) { + return (E) e; + } + } + return null; + } +} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/Concurrent.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/Concurrent.java index c7e66557..e91ed83d 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/Concurrent.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/Concurrent.java @@ -1,7 +1,8 @@ package com.jmsoftware.maf.springcloudstarter.quartz.constant; -import com.jmsoftware.maf.common.bean.EnumerationBase; +import com.jmsoftware.maf.common.enumeration.ValueDescriptionBaseEnum; import lombok.Getter; +import lombok.ToString; /** * Description: Concurrent, change description here. @@ -9,7 +10,8 @@ * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/23/2021 2:04 PM **/ @Getter -public enum Concurrent implements EnumerationBase { +@ToString +public enum Concurrent implements ValueDescriptionBaseEnum { /** * Disallow Concurrent */ @@ -24,4 +26,9 @@ public enum Concurrent implements EnumerationBase { Concurrent(Byte value) { this.value = value; } + + @Override + public String getDescription() { + return null; + } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/MisfirePolicy.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/MisfirePolicy.java index 5408458e..579e18a1 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/MisfirePolicy.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/MisfirePolicy.java @@ -1,6 +1,6 @@ package com.jmsoftware.maf.springcloudstarter.quartz.constant; -import com.jmsoftware.maf.common.bean.EnumerationBase; +import com.jmsoftware.maf.common.enumeration.ValueDescriptionBaseEnum; import lombok.Getter; import lombok.ToString; import lombok.val; @@ -19,7 +19,7 @@ **/ @Getter @ToString -public enum MisfirePolicy implements EnumerationBase { +public enum MisfirePolicy implements ValueDescriptionBaseEnum { /** * Instructs the {@link Scheduler} that the * Trigger will never be evaluated for a misfire situation, @@ -82,4 +82,9 @@ public static MisfirePolicy getByValue(Byte value) { } return null; } + + @Override + public String getDescription() { + return null; + } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/QuartzJobStatus.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/QuartzJobStatus.java index 8c404fec..de577786 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/QuartzJobStatus.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/constant/QuartzJobStatus.java @@ -1,6 +1,6 @@ package com.jmsoftware.maf.springcloudstarter.quartz.constant; -import com.jmsoftware.maf.common.bean.EnumerationBase; +import com.jmsoftware.maf.common.enumeration.ValueDescriptionBaseEnum; import lombok.Getter; import lombok.ToString; @@ -11,7 +11,7 @@ **/ @Getter @ToString -public enum QuartzJobStatus implements EnumerationBase { +public enum QuartzJobStatus implements ValueDescriptionBaseEnum { /** * Normal */ @@ -27,4 +27,9 @@ public enum QuartzJobStatus implements EnumerationBase { QuartzJobStatus(Byte value) { this.value = value; } + + @Override + public String getDescription() { + return null; + } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/annotation/ValidEnumValue.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/annotation/ValidEnumValue.java index 8a7a92fe..3a240447 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/annotation/ValidEnumValue.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/annotation/ValidEnumValue.java @@ -31,7 +31,7 @@ Class[] payload() default {}; - Class targetEnum() default Class.class; + Class targetEnum() default Enum.class; boolean ignoreNull() default false; } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/validator/EnumValueValidator.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/validator/EnumValueValidator.java index 6d08dffe..ff62a0cb 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/validator/EnumValueValidator.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/validator/EnumValueValidator.java @@ -2,7 +2,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReflectUtil; -import com.jmsoftware.maf.common.bean.EnumerationBase; +import com.jmsoftware.maf.common.enumeration.ValueDescriptionBaseEnum; import com.jmsoftware.maf.springcloudstarter.validation.annotation.ValidEnumValue; import lombok.extern.slf4j.Slf4j; import lombok.val; @@ -33,8 +33,7 @@ public class EnumValueValidator implements ConstraintValidator