
cn.featherfly.conversion.core.AbstractConvertor Maven / Gradle / Ivy
package cn.featherfly.conversion.core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.featherfly.common.lang.ClassUtils;
import cn.featherfly.common.lang.GenericType;
/**
*
* 抽象转换器
*
* @param 转换对象
* @param
* @author 钟冀
*/
public abstract class AbstractConvertor> implements Convertor{
private Class type;
/**
*/
public AbstractConvertor() {
type = ClassUtils.getSuperClassGenricType(this.getClass());
}
/**
* 日志
*/
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
*
* 是否提供对传入类型的支持
*
* @param generecType generecType
* @return 是否支持
*/
protected abstract boolean supportFor(GenericType generecType);
/**
*
* 对象转换为字符串
*
* @param value 对象
* @param genericType 指定对象的上下文属性
* @return 字符串
*/
protected abstract String doToString(T value, G genericType);
/**
*
* 字符串转换为对象
*
* @param value 字符串
* @param genericType 指定对象的上下文属性
* @return 对象
*/
protected abstract T doToObject(String value, G genericType);
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public > String toString(T value, GT genericType) {
if (supportFor(genericType)) {
return doToString(value, (G) genericType);
}
return null;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public > T toObject(String value, GT genericType) {
if (supportFor(genericType)) {
return doToObject(value, (G) genericType);
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public Class getType() {
return type;
}
/**
*
* 返回泛型声明的类型
*
* @return 泛型声明的类型
*/
protected Class getGenricType() {
return type;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy