类型转换器默认扩展新增针对数字、Boolean、字符串类型的处理
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
package fun.asgc.neutrino.core.type;
|
||||
|
||||
import fun.asgc.neutrino.core.util.Assert;
|
||||
import fun.asgc.neutrino.core.util.StringUtil;
|
||||
import fun.asgc.neutrino.core.util.TypeUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -143,9 +144,99 @@ public class TypeMatchers {
|
||||
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
|
||||
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
|
||||
if (TypeUtil.isNormalBasicType(clazz) && targetClass == String.class) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 10);
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 100);
|
||||
matchInfo.setTypeConverter(((value, targetType) -> String.valueOf(value)));
|
||||
}
|
||||
if (TypeUtil.isChar(targetClass) && TypeUtil.isString(clazz)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 101);
|
||||
matchInfo.setTypeConverter(((value, targetType) -> ((String)value).charAt(0)));
|
||||
}
|
||||
return matchInfo;
|
||||
}
|
||||
});
|
||||
// Boolean
|
||||
extensionMatcherList.add(new TypeMatcher() {
|
||||
@Override
|
||||
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
|
||||
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
|
||||
if (TypeUtil.isString(clazz) && TypeUtil.isBoolean(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 110);
|
||||
matchInfo.setTypeConverter(((value, targetType) -> ((String)value).toLowerCase().equals("true")));
|
||||
}
|
||||
return matchInfo;
|
||||
}
|
||||
});
|
||||
// Number
|
||||
extensionMatcherList.add(new TypeMatcher() {
|
||||
@Override
|
||||
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
|
||||
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
|
||||
if (TypeUtil.isString(clazz) && TypeUtil.isByte(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 111);
|
||||
matchInfo.setTypeConverter((value, targetType) -> {
|
||||
try {
|
||||
return Byte.valueOf((String)value);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
if (TypeUtil.isString(clazz) && TypeUtil.isShort(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 112);
|
||||
matchInfo.setTypeConverter((value, targetType) -> {
|
||||
try {
|
||||
return Short.valueOf((String)value);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
if (TypeUtil.isString(clazz) && TypeUtil.isInteger(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 113);
|
||||
matchInfo.setTypeConverter((value, targetType) -> {
|
||||
try {
|
||||
return Integer.valueOf((String)value);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
if (TypeUtil.isString(clazz) && TypeUtil.isLong(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 114);
|
||||
matchInfo.setTypeConverter((value, targetType) -> {
|
||||
try {
|
||||
return Long.valueOf((String)value);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
if (TypeUtil.isString(clazz) && TypeUtil.isFloat(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 115);
|
||||
matchInfo.setTypeConverter((value, targetType) -> {
|
||||
try {
|
||||
return Float.valueOf((String)value);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
if (TypeUtil.isString(clazz) && TypeUtil.isDouble(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 116);
|
||||
matchInfo.setTypeConverter((value, targetType) -> {
|
||||
try {
|
||||
return Double.valueOf((String)value);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
return matchInfo;
|
||||
}
|
||||
});
|
||||
@@ -155,7 +246,7 @@ public class TypeMatchers {
|
||||
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
|
||||
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
|
||||
if (TypeUtil.isLong(clazz) && TypeUtil.isDate(targetClass)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 20);
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 200);
|
||||
matchInfo.setTypeConverter(((value, targetType) -> {
|
||||
if (targetClass == java.util.Date.class) {
|
||||
return new java.util.Date((long)value);
|
||||
@@ -173,7 +264,7 @@ public class TypeMatchers {
|
||||
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
|
||||
TypeMatchInfo matchInfo = new TypeMatchInfo(clazz, targetClass, this);
|
||||
if (TypeUtil.isLong(targetClass) && TypeUtil.isDate(clazz)) {
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 30);
|
||||
matchInfo.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 300);
|
||||
matchInfo.setTypeConverter(((value, targetType) -> ((java.util.Date)value).getTime()));
|
||||
}
|
||||
return matchInfo;
|
||||
@@ -185,7 +276,7 @@ public class TypeMatchers {
|
||||
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.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 400);
|
||||
matchInfo.setTypeConverter(((value, targetType) -> Stream.of((Object[])value).collect(Collectors.toList())));
|
||||
}
|
||||
return matchInfo;
|
||||
@@ -197,7 +288,7 @@ public class TypeMatchers {
|
||||
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.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 500);
|
||||
matchInfo.setTypeConverter((value, targetType) -> ((List)value).toArray());
|
||||
}
|
||||
return matchInfo;
|
||||
@@ -209,7 +300,7 @@ public class TypeMatchers {
|
||||
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.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 600);
|
||||
matchInfo.setTypeConverter(((value, targetType) -> Stream.of((Object[])value).collect(Collectors.toSet())));
|
||||
}
|
||||
return matchInfo;
|
||||
@@ -221,7 +312,7 @@ public class TypeMatchers {
|
||||
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.setTypeDistance(TypeMatchLevel.EXTENSION.getDistanceMin() + 700);
|
||||
matchInfo.setTypeConverter((value, targetType) -> ((Set)value).toArray());
|
||||
}
|
||||
return matchInfo;
|
||||
|
||||
@@ -114,6 +114,22 @@ public class StringUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isNumeric(String cs) {
|
||||
if (isEmpty(cs)) {
|
||||
return false;
|
||||
} else {
|
||||
int sz = cs.length();
|
||||
|
||||
for(int i = 0; i < sz; ++i) {
|
||||
if (!Character.isDigit(cs.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param str
|
||||
|
||||
@@ -25,6 +25,7 @@ package fun.asgc.neutrino.core.util;
|
||||
import com.google.common.collect.Lists;
|
||||
import fun.asgc.neutrino.core.type.TypeMatchInfo;
|
||||
import fun.asgc.neutrino.core.type.TypeMatchers;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
@@ -238,7 +239,15 @@ public class TypeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static boolean isByte(Field field) {
|
||||
Class<?> clazz = getFieldType(field);
|
||||
return isByte(getFieldType(field));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是byte类型
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static boolean isByte(Class clazz) {
|
||||
return clazz == byte.class || clazz == Byte.class;
|
||||
}
|
||||
|
||||
@@ -248,7 +257,15 @@ public class TypeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static boolean isShort(Field field) {
|
||||
Class<?> clazz = getFieldType(field);
|
||||
return isShort(getFieldType(field));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是short类型
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static boolean isShort(Class clazz) {
|
||||
return clazz == short.class || clazz == Short.class;
|
||||
}
|
||||
|
||||
@@ -258,7 +275,15 @@ public class TypeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static boolean isInteger(Field field) {
|
||||
Class<?> clazz = getFieldType(field);
|
||||
return isInteger(getFieldType(field));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是integer类型
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static boolean isInteger(Class clazz) {
|
||||
return clazz == int.class || clazz == Integer.class;
|
||||
}
|
||||
|
||||
@@ -286,7 +311,15 @@ public class TypeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static boolean isFloat(Field field) {
|
||||
Class<?> clazz = getFieldType(field);
|
||||
return isFloat(getFieldType(field));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是float类型
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static boolean isFloat(Class<?> clazz) {
|
||||
return clazz == float.class || clazz == Float.class;
|
||||
}
|
||||
|
||||
@@ -296,7 +329,15 @@ public class TypeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static boolean isDouble(Field field) {
|
||||
Class<?> clazz = getFieldType(field);
|
||||
return isDouble(getFieldType(field));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是double类型
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static boolean isDouble(Class<?> clazz) {
|
||||
return clazz == double.class || clazz == Double.class;
|
||||
}
|
||||
|
||||
@@ -326,7 +367,15 @@ public class TypeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static boolean isString(Field field) {
|
||||
Class<?> clazz = getFieldType(field);
|
||||
return isString(getFieldType(field));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是String类型
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static boolean isString(Class clazz) {
|
||||
return clazz == String.class;
|
||||
}
|
||||
|
||||
|
||||
@@ -92,23 +92,23 @@ public class TypeUtilTest {
|
||||
|
||||
@Test
|
||||
public void conversionString() {
|
||||
System.out.println(typeMatchers.conversion("aabb", boolean.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", byte.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", short.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", char.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", int.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", long.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", float.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", double.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Boolean.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Byte.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Short.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Character.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Integer.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Long.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Float.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", Double.class));
|
||||
System.out.println(typeMatchers.conversion("aabb", String.class));
|
||||
System.out.println(typeMatchers.conversion("true", boolean.class));
|
||||
System.out.println(typeMatchers.conversion("100", byte.class));
|
||||
System.out.println(typeMatchers.conversion("101", short.class));
|
||||
System.out.println(typeMatchers.conversion("a", char.class));
|
||||
System.out.println(typeMatchers.conversion("1000", int.class));
|
||||
System.out.println(typeMatchers.conversion("500000", long.class));
|
||||
System.out.println(typeMatchers.conversion("1.3", float.class));
|
||||
System.out.println(typeMatchers.conversion("566.0099", double.class));
|
||||
System.out.println(typeMatchers.conversion("false", Boolean.class));
|
||||
System.out.println(typeMatchers.conversion("88", Byte.class));
|
||||
System.out.println(typeMatchers.conversion("444", Short.class));
|
||||
System.out.println(typeMatchers.conversion("B", Character.class));
|
||||
System.out.println(typeMatchers.conversion("99", Integer.class));
|
||||
System.out.println(typeMatchers.conversion("5666666", Long.class));
|
||||
System.out.println(typeMatchers.conversion("10.24", Float.class));
|
||||
System.out.println(typeMatchers.conversion("3.1415926", Double.class));
|
||||
System.out.println(typeMatchers.conversion('a', String.class));
|
||||
|
||||
System.out.println(typeMatchers.conversion("aabb", Animal.class));
|
||||
System.out.println(typeMatchers.conversion("aabb:10", Cat.class));
|
||||
@@ -118,7 +118,6 @@ public class TypeUtilTest {
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user