为enum添加默认的int到enum的转换方法

pull/31/head
kyw7 2 years ago
parent 70ba17de2a
commit 0a5c793bf4

@ -25,6 +25,7 @@ public class AnchorInfo {
/** /**
* *
* @see com.java3y.austin.common.enums.AnchorState
*/ */
private int state; private int state;

@ -8,7 +8,18 @@ import lombok.NoArgsConstructor;
* *
* <p> * <p>
* *
* {"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"AKIDhDxxxxxxxx1WljQq","secretKey":"B4hwww39yxxxrrrrgxyi","smsSdkAppId":"1423123125","templateId":"1182097","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云","scriptName":"TencentSmsScript"} * {
* "url": "sms.tencentcloudapi.com",
* "region": "ap-guangzhou",
* "secretId": "AKIDhDxxxxxxxx1WljQq",
* "secretKey": "B4hwww39yxxxrrrrgxyi",
* "smsSdkAppId": "1423123125",
* "templateId": "1182097",
* "signName": "Java3y公众号",
* "supplierId": 10,
* "supplierName": "腾讯云",
* "scriptName": "TencentSmsScript"
* }
* *
* @author 3y * @author 3y
*/ */

@ -13,7 +13,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum AnchorState { public enum AnchorState implements PowerfulEnum {
/** /**
* *
@ -56,26 +56,10 @@ public enum AnchorState {
/** /**
* *
*/ */
CLICK(0100, "消息被点击"), CLICK(64, "消息被点击"),
; ;
private final Integer code; private final Integer code;
private final String description; private final String description;
/**
* code
*
* @param code
* @return
*/
public static String getDescriptionByCode(Integer code) {
for (AnchorState anchorState : AnchorState.values()) {
if (anchorState.getCode().equals(code)) {
return anchorState.getDescription();
}
}
return "未知点位";
}
} }

@ -12,7 +12,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum AuditStatus { public enum AuditStatus implements PowerfulEnum {
/** /**
* 10. * 10.

@ -6,6 +6,9 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import java.util.Arrays;
import java.util.Objects;
/** /**
* *
* *
@ -14,7 +17,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum ChannelType { public enum ChannelType implements PowerfulEnum {
/** /**
@ -94,28 +97,8 @@ public enum ChannelType {
* @return * @return
*/ */
public static Class<? extends ContentModel> getChanelModelClassByCode(Integer code) { public static Class<? extends ContentModel> getChanelModelClassByCode(Integer code) {
ChannelType[] values = values(); return Arrays.stream(values()).filter(channelType -> Objects.equals(code, channelType.getCode()))
for (ChannelType value : values) { .map(ChannelType::getContentModelClass)
if (value.getCode().equals(code)) { .findFirst().orElse(null);
return value.getContentModelClass();
}
}
return null;
}
/**
* codeenum
*
* @param code
* @return
*/
public static ChannelType getEnumByCode(Integer code) {
ChannelType[] values = values();
for (ChannelType value : values) {
if (value.getCode().equals(code)) {
return value;
}
}
return null;
} }
} }

@ -5,7 +5,9 @@ import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* *
@ -15,7 +17,7 @@ import java.util.List;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum DeduplicationType { public enum DeduplicationType implements PowerfulEnum {
/** /**
* *
@ -29,18 +31,4 @@ public enum DeduplicationType {
; ;
private final Integer code; private final Integer code;
private final String description; private final String description;
/**
*
*
* @return
*/
public static List<Integer> getDeduplicationList() {
ArrayList<Integer> result = new ArrayList<>();
for (DeduplicationType value : DeduplicationType.values()) {
result.add(value.getCode());
}
return result;
}
} }

@ -0,0 +1,30 @@
package com.java3y.austin.common.enums;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class EnumUtil {
private EnumUtil() {
}
public static <T extends PowerfulEnum> String getDescriptionByCode(Integer code, Class<T> enumClass) {
return Arrays.stream(enumClass.getEnumConstants())
.filter(e -> Objects.equals(e.getCode(), code))
.findFirst().map(PowerfulEnum::getDescription).orElse("");
}
public static <T extends PowerfulEnum> T getEnumByCode(Integer code, Class<T> enumClass) {
return Arrays.stream(enumClass.getEnumConstants())
.filter(e -> Objects.equals(e.getCode(), code))
.findFirst().orElse(null);
}
public static <T extends PowerfulEnum> List<Integer> getCodeList(Class<T> enumClass) {
return Arrays.stream(enumClass.getEnumConstants())
.map(PowerfulEnum::getCode)
.collect(Collectors.toList());
}
}

@ -13,33 +13,25 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum FileType { public enum FileType implements PowerfulEnum {
/** /**
* *
*/ */
IMAGE("10", "image"), IMAGE(10, "image"),
/** /**
* *
*/ */
VOICE("20", "voice"), VOICE(20, "voice"),
/** /**
* *
*/ */
COMMON_FILE("30", "file"), COMMON_FILE(30, "file"),
/** /**
* *
*/ */
VIDEO("40", "video"), VIDEO(40, "video"),
; ;
private final String code; private final Integer code;
private final String name; private final String description;
public static String getNameByCode(String code) {
for (FileType fileType : FileType.values()) {
if (fileType.getCode().equals(code)) {
return fileType.getName();
}
}
return null;
}
} }

@ -13,7 +13,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum IdType { public enum IdType implements PowerfulEnum {
/** /**
* userId * userId
*/ */

@ -11,7 +11,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum MessageStatus { public enum MessageStatus implements PowerfulEnum {
/** /**
* 10. * 10.

@ -4,6 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import java.util.Arrays;
import java.util.Objects;
/** /**
* *
* *
@ -12,7 +15,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum MessageType { public enum MessageType implements PowerfulEnum {
/** /**
* *
@ -44,21 +47,4 @@ public enum MessageType {
private final String codeEn; private final String codeEn;
/**
* codeenum
*
* @param code
* @return
*/
public static MessageType getEnumByCode(Integer code) {
MessageType[] values = values();
for (MessageType value : values) {
if (value.getCode().equals(code)) {
return value;
}
}
return null;
}
} }

@ -0,0 +1,9 @@
package com.java3y.austin.common.enums;
public interface PowerfulEnum {
Integer getCode();
String getDescription();
}

@ -13,7 +13,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum RespStatusEnum { public enum RespStatusEnum {
/** /**
* *

@ -12,7 +12,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum ShieldType { public enum ShieldType implements PowerfulEnum {
/** /**
@ -30,6 +30,4 @@ public enum ShieldType {
private final Integer code; private final Integer code;
private final String description; private final String description;
} }

@ -12,7 +12,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum SmsStatus { public enum SmsStatus implements PowerfulEnum{
/** /**
* *
@ -33,22 +33,4 @@ public enum SmsStatus {
private final Integer code; private final Integer code;
private final String description; private final String description;
/**
*
*
* @param code
* @return
*/
public static String getDescriptionByStatus(Integer code) {
for (SmsStatus value : SmsStatus.values()) {
if (value.getCode().equals(code)) {
return value.getDescription();
}
}
return "";
}
} }

@ -13,7 +13,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum SmsSupplier { public enum SmsSupplier implements PowerfulEnum {
/** /**
@ -27,19 +27,4 @@ public enum SmsSupplier {
private final Integer code; private final Integer code;
private final String description; private final String description;
/**
*
*
* @param code
* @return
*/
public static String getDescriptionByStatus(Integer code) {
for (SmsStatus value : SmsStatus.values()) {
if (value.getCode().equals(code)) {
return value.getDescription();
}
}
return "";
}
} }

@ -12,7 +12,7 @@ import lombok.ToString;
@Getter @Getter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
public enum TemplateType { public enum TemplateType implements PowerfulEnum {
/** /**
* () * ()

Loading…
Cancel
Save