com.feingto.cloud.kit.reflection.ConvertKit Maven / Gradle / Ivy
package com.feingto.cloud.kit.reflection;
import com.feingto.cloud.kit.DateKit;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**
* Bean属性转换工具类
*
* @author longfei
*/
@SuppressWarnings("unchecked")
public class ConvertKit {
static {
registerDateConverter();
}
/**
* 提取集合中的对象的属性(通过getter函数), 组合成List.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
*/
public static List convertElementPropertyToList(final Collection collection, final String propertyName) {
List list = new ArrayList();
try {
for (Object obj : collection) {
list.add(PropertyUtils.getProperty(obj, propertyName));
}
} catch (Exception e) {
throw ReflectionKit.convertReflectionExceptionToUnchecked(e);
}
return list;
}
/**
* 提取集合中的对象的属性(通过getter函数), 组合成由分割符分隔的字符串.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
* @param separator 分隔符.
*/
public static String convertElementPropertyToString(final Collection collection, final String propertyName, final String separator) {
List list = convertElementPropertyToList(collection, propertyName);
return StringUtils.join(list, separator);
}
/**
* 转换字符串到相应类型.
*
* @param value 待转换的字符串.
* @param toType 转换目标类型.
*/
public static Object convertStringToObject(String value, Class> toType) {
try {
return org.apache.commons.beanutils.ConvertUtils.convert(value, toType);
} catch (Exception e) {
throw ReflectionKit.convertReflectionExceptionToUnchecked(e);
}
}
/**
* 定义日期Converter的格式: yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss
*/
private static void registerDateConverter() {
DateConverter dc = new DateConverter();
dc.setUseLocaleFormat(true);
dc.setPatterns(new String[]{DateKit.DATE, DateKit.DATE_TIME});
org.apache.commons.beanutils.ConvertUtils.register(dc, Date.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy