cn.wanghaomiao.seimi.utils.GenericUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of SeimiCrawler Show documentation
Show all versions of SeimiCrawler Show documentation
一个支持分布式的可以高效开发且可以高效运行的爬虫框架。设计思想上融合了spring与scrapy的优点。
package cn.wanghaomiao.seimi.utils;
import cn.wanghaomiao.seimi.core.CastToNumber;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
* 实现工具类,检查参数化类型的参数类型。
*/
public class GenericUtils {
private static final Class>[] EMPTY_CLASSES = new Class>[0];
private static final Map numberClass = new HashMap(){{
put(int.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).intValue();
}
});
put(Integer.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).intValue();
}
});
put(long.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).longValue();
}
});
put(Long.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).longValue();
}
});
put(short.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).shortValue();
}
});
put(Short.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).shortValue();
}
});
put(float.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).floatValue();
}
});
put(Float.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).floatValue();
}
});
put(double.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).doubleValue();
}
});
put(Double.class, new CastToNumber() {
@Override
public Object castTo(String ori) {
return new BigDecimal(ori).doubleValue();
}
});
}};
/**
* 从参数, 返回值, 基类的: Generic 类型信息获取传入的实际类信息。
*
* @param genericType - Generic 类型信息
* @return 实际类信息
*/
public static Class>[] getActualClass(Type genericType) {
if (genericType instanceof ParameterizedType) {
Type[] actualTypes = ((ParameterizedType) genericType).getActualTypeArguments();
Class>[] actualClasses = new Class>[actualTypes.length];
int i = 0;
while (i < actualTypes.length) {
Type actualType = actualTypes[i];
if (actualType instanceof Class>) {
actualClasses[i] = (Class>) actualType;
} else if (actualType instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) actualType).getGenericComponentType();
actualClasses[i] = Array.newInstance((Class>) componentType, 0).getClass();
}
i++;
}
return actualClasses;
}
return EMPTY_CLASSES;
}
/**
* 判断给定类是否是支持的数字类型
* @param cls
* @return
*/
public static boolean isNumber(Class cls){
return numberClass.containsKey(cls);
}
public static Object castToNumber(Class cls,String val){
return numberClass.get(cls).castTo(val);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy