fix:增加枚举工具类
This commit is contained in:
parent
a450832f80
commit
a16a886a88
@ -0,0 +1,16 @@
|
|||||||
|
package cn.azxo.framework.common.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author mr.jie
|
||||||
|
* @version 1.0
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/5 17:28
|
||||||
|
*/
|
||||||
|
@Documented
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
|
||||||
|
public @interface BaseEnumJson {
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
package cn.azxo.framework.common.ienum;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import cn.azxo.framework.common.model.BaseEnum;
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举反序列化
|
||||||
|
*
|
||||||
|
* @author mr.jie
|
||||||
|
* @see BaseEnumDeserializer
|
||||||
|
* @since 2021-08-05 19:13
|
||||||
|
*/
|
||||||
|
public class BaseEnumDeserializer extends StdDeserializer<BaseEnum> {
|
||||||
|
|
||||||
|
public BaseEnumDeserializer() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseEnumDeserializer(Class<BaseEnum> vc) {
|
||||||
|
super(vc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseEnum deserialize(JsonParser p, DeserializationContext ctxt)
|
||||||
|
throws IOException, JsonProcessingException {
|
||||||
|
String currentName = p.currentName();
|
||||||
|
Object currentValue = p.getCurrentValue();
|
||||||
|
Class findPropertyType = BeanUtils.findPropertyType(currentName, currentValue.getClass());
|
||||||
|
if (BaseEnum.class.isAssignableFrom(findPropertyType) && Enum.class
|
||||||
|
.isAssignableFrom(findPropertyType)) {
|
||||||
|
Object[] enumConstants = findPropertyType.getEnumConstants();
|
||||||
|
for (Object enumConstant : enumConstants) {
|
||||||
|
if (enumConstant instanceof BaseEnum) {
|
||||||
|
try {
|
||||||
|
if (((BaseEnum) enumConstant).getCode().toString().equals(p.getText())) {
|
||||||
|
return (BaseEnum) enumConstant;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package cn.azxo.framework.common.ienum;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import cn.azxo.framework.common.model.BaseEnum;
|
||||||
|
import cn.azxo.framework.common.utils.EnumUtils;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举序列化
|
||||||
|
*
|
||||||
|
* @author mr.jie
|
||||||
|
* @see BaseEnumSerializer
|
||||||
|
* @since 2021-08-05 19:13
|
||||||
|
*/
|
||||||
|
public class BaseEnumSerializer extends StdSerializer<BaseEnum> {
|
||||||
|
|
||||||
|
public BaseEnumSerializer() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseEnumSerializer(Class<BaseEnum> t) {
|
||||||
|
super(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(BaseEnum value, JsonGenerator gen, SerializerProvider provider)
|
||||||
|
throws IOException {
|
||||||
|
gen.writeObject(EnumUtils.writeObject(value));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
package cn.azxo.framework.common.model;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import cn.azxo.framework.common.ienum.BaseEnumDeserializer;
|
||||||
|
import cn.azxo.framework.common.ienum.BaseEnumSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举抽象类
|
||||||
|
*
|
||||||
|
* @author mr.jie
|
||||||
|
* @see BaseEnum
|
||||||
|
* @since 2021-08-05 19:13
|
||||||
|
*/
|
||||||
|
@JsonDeserialize(using = BaseEnumDeserializer.class)
|
||||||
|
@JsonSerialize(using = BaseEnumSerializer.class)
|
||||||
|
public interface BaseEnum<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举 code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
T getCode();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举描述信息
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getMessage();
|
||||||
|
|
||||||
|
static <T extends BaseEnum> T getByCode(Class<T> clazz, Object code) {
|
||||||
|
return Stream.of(clazz.getEnumConstants())
|
||||||
|
.filter(v -> v.getCode().equals(code))
|
||||||
|
.findAny().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
package cn.azxo.framework.common.utils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import cn.azxo.framework.common.annotation.BaseEnumJson;
|
||||||
|
import cn.azxo.framework.common.model.BaseEnum;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enumUtil
|
||||||
|
*
|
||||||
|
* @author mr.jie
|
||||||
|
* @version 1.0
|
||||||
|
* @description
|
||||||
|
* @date 2022/1/26 19:59
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public class EnumUtils {
|
||||||
|
|
||||||
|
|
||||||
|
private static final Map<Class<? extends BaseEnum>, Map<String, BaseEnum>> ENUM_MAP = new ConcurrentHashMap<>();
|
||||||
|
private static final Map<Class<? extends BaseEnum>, Field> ENUM_FIELD_MAP = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enum Serializer out <----> in enum Deserializer
|
||||||
|
*
|
||||||
|
* @param findPropertyType
|
||||||
|
* @param findKey
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@SneakyThrows
|
||||||
|
public static BaseEnum getFieldCache(Class<? extends BaseEnum> findPropertyType,
|
||||||
|
String findKey) {
|
||||||
|
|
||||||
|
Map<String, BaseEnum> tempMap = ENUM_MAP.computeIfAbsent(findPropertyType, clazz -> new ConcurrentHashMap<>());
|
||||||
|
BaseEnum baseEnum = tempMap.get(findKey);
|
||||||
|
if(null != baseEnum) {
|
||||||
|
return baseEnum;
|
||||||
|
}
|
||||||
|
BaseEnum[] enumConstants = findPropertyType.getEnumConstants();
|
||||||
|
for (BaseEnum iBaseEnum : enumConstants) {
|
||||||
|
if(iBaseEnum.getCode().equals(findKey)){
|
||||||
|
tempMap.put(findKey, iBaseEnum);
|
||||||
|
return iBaseEnum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
public static <T extends BaseEnum> Object writeObject(T value) {
|
||||||
|
Class<? extends BaseEnum> findPropertyType = value.getClass();
|
||||||
|
Field field = ENUM_FIELD_MAP.get(findPropertyType);
|
||||||
|
if (field != null) {
|
||||||
|
return field.get(value);
|
||||||
|
}
|
||||||
|
Optional<Field> tempField = Arrays.stream(findPropertyType.getDeclaredFields())
|
||||||
|
.filter(f -> f.isAnnotationPresent(
|
||||||
|
BaseEnumJson.class)).findFirst();
|
||||||
|
if (tempField.isPresent()) {
|
||||||
|
field = tempField.get();
|
||||||
|
if (!field.isAccessible()) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
}
|
||||||
|
ENUM_FIELD_MAP.put(findPropertyType, field);
|
||||||
|
return field.get(value);
|
||||||
|
}
|
||||||
|
return value.getCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user