All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.featherfly.conversion.string.AbstractToStringConvertor Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version

package cn.featherfly.conversion.string;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.featherfly.common.lang.ClassUtils;
import cn.featherfly.common.lang.GenericType;
import cn.featherfly.conversion.TypePolicys;

/**
 * 

* 抽象转换器 *

* * @param 转换对象 * @param GenericType * @author 钟冀 */ public abstract class AbstractToStringConvertor> implements ToStringConvertor { private Class type; private TypePolicys policy = TypePolicys.CLASS; /** */ public AbstractToStringConvertor() { 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(S value, G genericType); /** *

* 字符串转换为对象 *

* * @param value 字符串 * @param genericType 指定对象的上下文属性 * @return 对象 */ protected abstract S doToObject(String value, G genericType); /** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public > String sourceToTarget(S value, GT genericType) { if (supportFor(genericType)) { return doToString(value, (G) genericType); } return null; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public > S targetToSource(String value, GT genericType) { if (supportFor(genericType)) { return doToObject(value, (G) genericType); } return null; } /** * {@inheritDoc} */ @Override public Class getSourceType() { return type; } /** * {@inheritDoc} */ @Override public Class getTargetType() { return String.class; } /** * 返回policy * * @return policy */ @Override public TypePolicys getPolicy() { return policy; } /** * 设置policy * * @param policy policy */ public void setPolicy(TypePolicys policy) { this.policy = policy; } }