import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.objenesis.ObjenesisException;
public class BeanConverter {
private static final ModelMapper modelMapper = new ModelMapper();
public BeanConverter() {
}
public static ModelMapper getModelMapper() {
return modelMapper;
}
public static <T> Map<String, Object> beanToMap(T bean) {
Map<String, Object> map = Collections.emptyMap();
if (null != bean) {
BeanMap beanMap = BeanMap.create(bean);
map = new HashMap(beanMap.keySet().size());
Iterator var3 = beanMap.keySet().iterator();
while(var3.hasNext()) {
Object key = var3.next();
((Map)map).put(String.valueOf(key), beanMap.get(key));
}
}
return (Map)map;
}
public static <T> List<Map<String, Object>> beansToMap(List<T> objList) {
List<Map<String, Object>> list = Collections.emptyList();
if (CollectionUtils.isNotEmpty(objList)) {
list = new ArrayList(objList.size());
Iterator var4 = objList.iterator();
while(var4.hasNext()) {
T anObjList = var4.next();
Map<String, Object> map = beanToMap(anObjList);
((List)list).add(map);
}
}
return (List)list;
}
public static <T> List<T> mapToBean(List<Map<String, Object>> mapList, Class<T> beanClass) {
List<T> list = Collections.emptyList();
if (CollectionUtils.isNotEmpty(mapList)) {
list = new ArrayList(mapList.size());
Iterator var5 = mapList.iterator();
while(var5.hasNext()) {
Map<String, Object> map1 = (Map)var5.next();
T bean = mapToBean(map1, beanClass);
((List)list).add(bean);
}
}
return (List)list;
}
public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) {
T entity = newInstance(beanClass);
BeanMap beanMap = BeanMap.create(entity);
beanMap.putAll(map);
return entity;
}
public static <T> List<T> convert(Class<T> clazz, List<?> list) {
return CollectionUtils.isEmpty(list) ? Collections.emptyList() : (List)list.stream().map((e) -> {
return convert(clazz, e);
}).collect(Collectors.toList());
}
public static <T> T convert(Class<T> targetClass, Object source) {
return getModelMapper().map(source, targetClass);
}
public static void convert(Object targetClass, Object source) {
getModelMapper().map(source, targetClass);
}
private static <T> T newInstance(Class<T> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException var2) {
throw new ObjenesisException(var2);
}
}
static {
Jsr310ModuleConfig config = Jsr310ModuleConfig.builder().dateTimePattern("yyyy-MM-dd HH:mm:ss").datePattern("yyyy-MM-dd").zoneId(ZoneId.systemDefault()).build();
modelMapper.registerModule(new Jsr310Module(config)).registerModule(new Jdk8Module());
modelMapper.getConfiguration().setFullTypeMatchingRequired(true);
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
}
BeanConverter复制集合
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- //可变集合类和不可变集合类的copy和mutablecopy有什么区别?如果是集合是内容复制的话,集合里面的元素...
- 整个《面试题》都是对[2017年6月iOS招人心得(附面试题)]的整理 1.可变集合类 和 不可变集合类的 cop...