内部类型转换扩展,增加集合与数组之间的转换支持

This commit is contained in:
aoshiguchen
2022-06-18 19:10:13 +08:00
parent dc04fe2fde
commit 89c9b3b19c
3 changed files with 77 additions and 172 deletions
@@ -18,6 +18,9 @@ import fun.asgc.neutrino.core.util.TypeUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
*
@@ -176,6 +179,55 @@ public class TypeMatchers {
return matchInfo;
}
});
// Object[] -> List
extensionMatcherList.add(new TypeMatcher() {
@Override
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
if (List.class == targetClass && clazz.isArray()) {
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 40);
matchInfo.setTypeConverter(((value, targetType) -> Stream.of((Object[])value).collect(Collectors.toList())));
}
return matchInfo;
}
});
// List -> Object[]
extensionMatcherList.add(new TypeMatcher() {
@Override
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
if (List.class.isAssignableFrom(clazz) && targetClass.isArray()) {
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 50);
matchInfo.setTypeConverter((value, targetType) -> ((List)value).toArray());
}
return matchInfo;
}
});
// Object[] -> Set
extensionMatcherList.add(new TypeMatcher() {
@Override
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
if (Set.class == targetClass && clazz.isArray()) {
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 60);
matchInfo.setTypeConverter(((value, targetType) -> Stream.of((Object[])value).collect(Collectors.toSet())));
}
return matchInfo;
}
});
// Set -> Object[]
extensionMatcherList.add(new TypeMatcher() {
@Override
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
if (Set.class.isAssignableFrom(clazz) && targetClass.isArray()) {
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 70);
matchInfo.setTypeConverter((value, targetType) -> ((Set)value).toArray());
}
return matchInfo;
}
});
}
/**
@@ -27,7 +27,6 @@ import fun.asgc.neutrino.core.type.TypeMatchInfo;
import fun.asgc.neutrino.core.type.TypeMatchers;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
@@ -476,121 +475,9 @@ public class TypeUtil {
public static TypeMatchInfo typeMatch(Class<?> fieldType, Class<?> valueType) {
Assert.notNull(fieldType, "字段类型不能为空!");
Assert.notNull(valueType, "值类型不能为空!");
// // 1、完全匹配
// if (fieldType == valueType) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.PERFECT);
// }
// // 2、包装匹配
// if (isStrictBasicType(valueType) && getWrapType(valueType) == fieldType) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.PACKING);
// }
// // 3、解包装匹配
// if (isStrictBasicType(fieldType) && getWrapType(fieldType) == valueType) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.UNPACKING);
// }
// // 4、类型提升
// if (isNormalBasicType(fieldType) && isNormalBasicType(valueType)) {
// int index1 = basicTypeList.indexOf(fieldType);
// int index2 = basicTypeList.indexOf(valueType);
// if (index1 > index2 && index2 > 0) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.ASCENSION_MIN + (index1 - index2) - 1);
// }
// }
// // 5、超类匹配
// if (fieldType.isAssignableFrom(valueType)) {
// int inheritLevel = getInheritLevel(fieldType, valueType);
// if (-1 == inheritLevel) {
// throw new RuntimeException(String.format("超类匹配异常! fieldType:%s valueType:%s", fieldType.getName(), valueType.getName()));
// }
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.SUPER_MIN + inheritLevel - 1);
// }
// // 6、自定义转换
// if (fieldType == Date.class || fieldType == java.sql.Date.class) {
// if (valueType == long.class) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN);
// } else if (valueType == long.class) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 1);
// }
// }
// if (fieldType == Long.class || fieldType == long.class) {
// if (valueType == Date.class) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 1000);
// } else if (valueType == java.sql.Date.class) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 1001);
// }
// }
// if (fieldType == String.class) {
// if (valueType == char.class) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 2000);
// } else if (valueType == Character.class) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 2001);
// } else if (Number.class.isAssignableFrom(valueType)) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 2002);
// }
// }
// if (Iterable.class.isAssignableFrom(fieldType) && Iterable.class.isAssignableFrom(valueType)) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 3000);
// }
// if (Iterable.class.isAssignableFrom(fieldType) && String[].class == valueType) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 4000);
// }
// if (Iterable.class.isAssignableFrom(valueType) && String[].class == fieldType) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 5000);
// }
// if (Iterable.class.isAssignableFrom(fieldType) && Integer[].class == valueType) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 6000);
// }
// if (Iterable.class.isAssignableFrom(valueType) && Integer[].class == fieldType) {
// return new TypeMatchInfo(fieldType, valueType, TypeDistance.CUSTOM_MIN + 7000);
// }
return new TypeMatchInfo(fieldType, valueType, null);
}
/**
* 类型提升
* @param value
* @param clazz
* @return
*/
public static Object ascensionType(Object value, Class<?> clazz) {
Class<?> valueType = value.getClass();
int index1 = basicTypeList.indexOf(valueType);
int index2 = basicTypeList.indexOf(clazz);
if (-1 == index1 || -1 == index2 || index1 >= index2) {
return value;
}
if (getWrapType(valueType) == clazz || getWrapType(clazz) == valueType) {
return value;
}
if (isChar(clazz)) {
return value;
}
String strValue;
if (isBoolean(valueType)) {
strValue = ((Boolean)value) ? "0" : "1";
} else if (isChar(valueType)){
strValue = (int)((Character)value) + "";
} else {
strValue = String.valueOf(value);
}
if (isBoolean(clazz)) {
return "0".equals(strValue);
}
Method method = ReflectUtil.getValueOfMethod(clazz, String.class);
if (null == method) {
return value;
}
try {
return method.invoke(null, strValue);
} catch (Exception e) {
// ignore
}
return value;
}
/**
* 类型转换
* @param value
@@ -600,63 +487,4 @@ public class TypeUtil {
public static Object conversion(Object value, Class<?> targetType) {
return defaultTypeMatchers.conversion(value, targetType);
}
// /**
// * 类型转换
// * @param value
// * @param targetType
// * @return
// */
// public static Object conversion(Object value, Class<?> targetType) {
// Assert.notNull(targetType, "目标类型不能为空!");
// if (null == value) {
// return null;
// }
// Class<?> valueType = value.getClass();
// TypeMatchInfo matchingInfo = typeMatch(targetType, valueType);
// if (matchingInfo.isNotMatch()) {
// return null;
// }
// // 类型提升
// if (matchingInfo.getTypeDistance() <= TypeDistance.ASCENSION_MAX) {
// return TypeUtil.ascensionType(value, targetType);
// }
// // 超类
// if (matchingInfo.getTypeDistance() <= TypeDistance.SUPER_MAX) {
// return value;
// }
// if (matchingInfo.getTypeDistance() >= TypeDistance.CUSTOM_MIN && matchingInfo.getTypeDistance() <= TypeDistance.CUSTOM_MAX) {
// // 自定义转换
// if (targetType == Date.class) {
// if (valueType == long.class || value == Long.class) {
// return new Date((Long) value);
// }
// } else if (targetType == java.sql.Date.class) {
// if (valueType == long.class || value == Long.class) {
// return new java.sql.Date((Long) value);
// }
// } else if (targetType == Long.class || targetType == long.class) {
// if (valueType == java.sql.Date.class || valueType == Date.class) {
// return ((Date) value).getTime();
// }
// } else if (targetType == String.class) {
// if (valueType == char.class || valueType == Character.class || Number.class.isAssignableFrom(valueType)) {
// return String.valueOf(value);
// }
// } else if (Iterable.class.isAssignableFrom(targetType) && Iterable.class.isAssignableFrom(valueType)) {
// if (Set.class.isAssignableFrom(targetType)) {
// return Sets.newHashSet((Iterable) value);
// }
// } else if (Iterable.class.isAssignableFrom(valueType) && String[].class == targetType) {
// if (List.class.isAssignableFrom(valueType)) {
// return ((List<Object>) value).stream().map(String::valueOf).collect(Collectors.toList()).toArray(new String[]{});
// }
// } else if (Iterable.class.isAssignableFrom(valueType) && Integer[].class == targetType) {
// if (List.class.isAssignableFrom(valueType)) {
// return ((List<Object>) value).stream().map(String::valueOf).map(Integer::valueOf).collect(Collectors.toList()).toArray(new Integer[]{});
// }
// }
// }
// return null;
// }
}
@@ -12,11 +12,15 @@
*/
package fun.asgc.neutrino.core.util;
import com.google.common.collect.Lists;
import fun.asgc.neutrino.core.type.*;
import lombok.Data;
import lombok.experimental.Accessors;
import org.junit.Test;
import java.util.List;
import java.util.Set;
/**
*
* @author: wen.y
@@ -80,6 +84,7 @@ public class TypeUtilTest {
System.out.println(typeMatchers.conversion(null, Float.class));
System.out.println(typeMatchers.conversion(null, Double.class));
System.out.println(typeMatchers.conversion(null, String.class));
System.out.println(typeMatchers.conversion(null, List.class));
System.out.println(typeMatchers.conversion(null, Animal.class));
System.out.println(typeMatchers.conversion(null, Cat.class));
@@ -107,6 +112,19 @@ public class TypeUtilTest {
System.out.println(typeMatchers.conversion("aabb", Animal.class));
System.out.println(typeMatchers.conversion("aabb:10", Cat.class));
System.out.println(typeMatchers.conversion(new String[]{"11","22","33","22"}, List.class));
System.out.println(typeMatchers.conversion(new Integer[]{10, 20, 30,20}, List.class));
System.out.println(typeMatchers.conversion(new Boolean[]{true, true, false}, List.class));
System.out.println(typeMatchers.conversion(new Animal[]{new Animal("aa"), new Animal("bb"), new Animal("cc"),}, List.class));
System.out.println(typeMatchers.conversion(Lists.newArrayList("11","22","33","22"), Object[].class));
System.out.println(typeMatchers.conversion(new String[]{"11","22","33","22"}, Set.class));
System.out.println(typeMatchers.conversion(new Integer[]{10, 20, 30,20}, Set.class));
System.out.println(typeMatchers.conversion(new Boolean[]{true, true, false}, Set.class));
}
@Test
@@ -130,6 +148,7 @@ public class TypeUtilTest {
System.out.println(typeMatchers.conversion(114, Float.class));
System.out.println(typeMatchers.conversion(115, Double.class));
System.out.println(typeMatchers.conversion(116, String.class));
System.out.println(typeMatchers.conversion(200, List.class));
System.out.println(typeMatchers.conversion(117, Animal.class));
System.out.println(typeMatchers.conversion(118, Cat.class));
@@ -140,6 +159,12 @@ public class TypeUtilTest {
@Data
public static class Animal {
private String name;
public Animal() {
}
public Animal(String name) {
this.name = name;
}
}
@Accessors(chain = true)