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

cn.featherfly.conversion.string.basic.EnumConvertor Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version

package cn.featherfly.conversion.string.basic;

import cn.featherfly.common.lang.GenericType;
import cn.featherfly.common.lang.LangUtils;
import cn.featherfly.common.lang.StringUtils;
import cn.featherfly.conversion.ConversionException;

/**
 * 

* Enum类转换器 *

* * @author 钟冀 */ public class EnumConvertor> extends AbstractBasicConvertor>{ /** */ public EnumConvertor() { } /** * {@inheritDoc} */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Class getSourceType() { return (Class) Enum.class; } /** * {@inheritDoc} */ @Override protected String doToString(T value, GenericType genericType) { if (value != null) { return value.toString(); } return ""; } /** * {@inheritDoc} */ @Override protected T doToObject(String value, GenericType genericType) { if (StringUtils.isNotBlank(value)) { try { T t = LangUtils.toEnum(genericType.getType(), value); if (t == null) { throw new ConversionException("#convert_failed", new Object[]{value, genericType.getType().getName()}); } return t; } catch (IllegalArgumentException e) { throw new ConversionException("#convert_failed", new Object[]{value, genericType.getType().getName()}); } } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy