类型匹配器代码调整、精简
This commit is contained in:
@@ -1,107 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package fun.asgc.neutrino.core.type;
|
||||
|
||||
/**
|
||||
* 类型距离
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/6/16
|
||||
*/
|
||||
public interface TypeDistance {
|
||||
|
||||
/**
|
||||
* 最小值,表示完全匹配
|
||||
*/
|
||||
int MIN = TypeMatchLevel.NULL.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 最大值,表示完全不匹配
|
||||
*/
|
||||
int MAX = Integer.MAX_VALUE;
|
||||
|
||||
/**
|
||||
* 空匹配
|
||||
*/
|
||||
int NULL = MIN;
|
||||
|
||||
/**
|
||||
* 完全匹配
|
||||
*/
|
||||
int PERFECT = TypeMatchLevel.PERFECT.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 完全不匹配
|
||||
*/
|
||||
int NOT = MAX;
|
||||
|
||||
/**
|
||||
* 包装匹配
|
||||
*/
|
||||
int PACKING = TypeMatchLevel.PACKING.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 解包装匹配
|
||||
*/
|
||||
int UNPACKING = TypeMatchLevel.UNPACKING.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 类型提升起始值
|
||||
*/
|
||||
int ASCENSION_MIN = TypeMatchLevel.ASCENSION.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 类型提升最大值
|
||||
*/
|
||||
int ASCENSION_MAX = TypeMatchLevel.ASCENSION.getDistanceMax();
|
||||
|
||||
/**
|
||||
* 超类匹配起始值
|
||||
* 每增加1层,值加1
|
||||
*/
|
||||
int SUPER_MIN = TypeMatchLevel.SUPER.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 超类匹配结束值
|
||||
*/
|
||||
int SUPER_MAX = TypeMatchLevel.SUPER.getDistanceMax();
|
||||
|
||||
/**
|
||||
* 内部扩展起始值
|
||||
*/
|
||||
int EXTENSION_MIN = TypeMatchLevel.EXTENSION.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 内部扩展结束值
|
||||
*/
|
||||
int EXTENSION_MAX = TypeMatchLevel.EXTENSION.getDistanceMax();
|
||||
|
||||
/**
|
||||
* 自定义类型转换起始值
|
||||
*/
|
||||
int CUSTOM_MIN = TypeMatchLevel.CUSTOM.getDistanceMin();
|
||||
|
||||
/**
|
||||
* 自定义类型转换结束值
|
||||
*/
|
||||
int CUSTOM_MAX = TypeMatchLevel.CUSTOM.getDistanceMax();
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class TypeMatchInfo {
|
||||
this.valueType = valueType;
|
||||
this.targetType = targetType;
|
||||
this.typeMatcher = typeMatcher;
|
||||
this.typeDistance = TypeDistance.NOT;
|
||||
this.typeDistance = TypeMatchLevel.NOT.getDistanceMin();
|
||||
}
|
||||
|
||||
public boolean isNotMatch() {
|
||||
|
||||
@@ -50,14 +50,28 @@ public enum TypeMatchLevel {
|
||||
// 超类匹配,当且仅当目标类是匹配类的超类时
|
||||
SUPER(5, 1000000, 2000000, "超类匹配"),
|
||||
// 内置的扩展匹配
|
||||
EXTENSION(6, 10000000, 20000000, "扩展匹配"),
|
||||
EXTENSION(6, 10000000, 50000000, "扩展匹配"),
|
||||
// 自定义匹配
|
||||
CUSTOM(7, 100000000, 200000000, "自定义匹配");
|
||||
CUSTOM(7, 100000000, 500000000, "自定义匹配"),
|
||||
// 不匹配
|
||||
NOT(8, Integer.MAX_VALUE, Integer.MAX_VALUE, "不匹配");
|
||||
private static Map<Integer,TypeMatchLevel> levelMap = Stream.of(TypeMatchLevel.values()).collect(Collectors.toMap(TypeMatchLevel::getLevel, Function.identity()));
|
||||
|
||||
/**
|
||||
* 匹配级别
|
||||
*/
|
||||
private int level;
|
||||
/**
|
||||
* 类型距离最小值
|
||||
*/
|
||||
private int distanceMin;
|
||||
/**
|
||||
* 类型距离最大值
|
||||
*/
|
||||
private int distanceMax;
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String desc;
|
||||
|
||||
public static TypeMatchLevel byLevel(int level) {
|
||||
|
||||
@@ -210,4 +210,32 @@ public class ClassUtil {
|
||||
}
|
||||
return annotation;
|
||||
}
|
||||
|
||||
public static boolean isPublic(Class<?> clazz) {
|
||||
return Modifier.isPublic(clazz.getModifiers());
|
||||
}
|
||||
|
||||
public static boolean isPrivate(Class<?> clazz) {
|
||||
return Modifier.isPrivate(clazz.getModifiers());
|
||||
}
|
||||
|
||||
public static boolean isProtected(Class<?> clazz) {
|
||||
return Modifier.isProtected(clazz.getModifiers());
|
||||
}
|
||||
|
||||
public static boolean isFinal(Class<?> clazz) {
|
||||
return Modifier.isFinal(clazz.getModifiers());
|
||||
}
|
||||
|
||||
public static boolean isAbstract(Class<?> clazz) {
|
||||
return Modifier.isAbstract(clazz.getModifiers());
|
||||
}
|
||||
|
||||
public static boolean isStatic(Class<?> clazz) {
|
||||
return Modifier.isStatic(clazz.getModifiers());
|
||||
}
|
||||
|
||||
public static boolean isInterface(Class<?> clazz) {
|
||||
return clazz.isInterface();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ package fun.asgc.neutrino.core.util;
|
||||
import com.google.common.collect.Sets;
|
||||
import fun.asgc.neutrino.core.cache.Cache;
|
||||
import fun.asgc.neutrino.core.cache.MemoryCache;
|
||||
import fun.asgc.neutrino.core.type.TypeDistance;
|
||||
import fun.asgc.neutrino.core.type.TypeMatchLevel;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -236,7 +236,7 @@ public class ReflectUtil {
|
||||
if (methodOptional.isPresent()) {
|
||||
return methodOptional.get();
|
||||
}
|
||||
int level = TypeDistance.MAX;
|
||||
int level = TypeMatchLevel.NOT.getDistanceMax();
|
||||
Method method = null;
|
||||
for (Method m : getMethods(field.getDeclaringClass())) {
|
||||
if (m.getName().equals(setMethodName) && m.getParameters().length == 1) {
|
||||
@@ -247,7 +247,7 @@ public class ReflectUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (level < TypeDistance.MAX && method != null) {
|
||||
if (level < TypeMatchLevel.NOT.getDistanceMin() && method != null) {
|
||||
return method;
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -25,7 +25,6 @@ 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.*;
|
||||
@@ -74,7 +73,7 @@ public class TypeUtil {
|
||||
/**
|
||||
* 基本类型的映射
|
||||
*/
|
||||
public static final Map<Class<?>, Class<?>> basicTypeMap = new HashMap<Class<?>, Class<?>>() {
|
||||
private static final Map<Class<?>, Class<?>> basicTypeMap = new HashMap<Class<?>, Class<?>>() {
|
||||
{
|
||||
this.put(byte.class, byte.class);
|
||||
this.put(short.class, short.class);
|
||||
@@ -99,7 +98,7 @@ public class TypeUtil {
|
||||
/**
|
||||
* 包装类型的映射
|
||||
*/
|
||||
public static final Map<Class<?>, Class<?>> wrapTypeMap = new HashMap<Class<?>, Class<?>>() {
|
||||
private static final Map<Class<?>, Class<?>> wrapTypeMap = new HashMap<Class<?>, Class<?>>() {
|
||||
{
|
||||
this.put(byte.class, Byte.class);
|
||||
this.put(short.class, Short.class);
|
||||
|
||||
@@ -36,7 +36,7 @@ public class TypeUtilTest {
|
||||
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
|
||||
TypeMatchInfo typeMatchInfo = new TypeMatchInfo(clazz, targetClass, this);
|
||||
if (clazz == String.class && targetClass == Animal.class) {
|
||||
typeMatchInfo.setTypeDistance(TypeDistance.CUSTOM_MIN);
|
||||
typeMatchInfo.setTypeDistance(TypeMatchLevel.CUSTOM.getDistanceMin() + 1);
|
||||
typeMatchInfo.setTypeConverter(((value, targetType) -> new Animal().setName((String)value)));
|
||||
}
|
||||
return typeMatchInfo;
|
||||
@@ -47,7 +47,7 @@ public class TypeUtilTest {
|
||||
public TypeMatchInfo match(Class<?> clazz, Class<?> targetClass) {
|
||||
TypeMatchInfo typeMatchInfo = new TypeMatchInfo(clazz, targetClass, this);
|
||||
if (clazz == String.class && targetClass == Cat.class) {
|
||||
typeMatchInfo.setTypeDistance(TypeDistance.CUSTOM_MIN);
|
||||
typeMatchInfo.setTypeDistance(TypeMatchLevel.CUSTOM.getDistanceMin() + 2);
|
||||
typeMatchInfo.setTypeConverter((value, targetType) -> {
|
||||
String[] tmp = ((String)value).split(":");
|
||||
Cat cat = new Cat();
|
||||
|
||||
Reference in New Issue
Block a user