package com.thinkgem.jeesite.common.mapper;

import java.util.Collection;
import java.util.List;

import org.dozer.DozerBeanMapper;

import com.google.common.collect.Lists;


/**
 * 简单封装Dozer, 实现深度转换Bean<->Bean的Mapper.实现:
 *  
 * 1. 持有Mapper的单例. 
 * 2. 返回值类型转换.
 * 3. 批量转换Collection中的所有对象.
 * 4. 区分创建新的B对象与将对象A值复制到已存在的B对象两种函数.
 * 
 * @author calvin
 */
public class BeanMapper {

	/**
	 * 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
	 */
	private static DozerBeanMapper dozer = new DozerBeanMapper();

	/**
	 * 基于Dozer转换对象的类型.
	 */
	public static <T> T map(Object source, Class<T> destinationClass) {
		return dozer.map(source, destinationClass);
	}

	/**
	 * 基于Dozer转换Collection中对象的类型.
	 */
	@SuppressWarnings("rawtypes")
	public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
		List<T> destinationList = Lists.newArrayList();
		for (Object sourceObject : sourceList) {
			T destinationObject = dozer.map(sourceObject, destinationClass);
			destinationList.add(destinationObject);
		}
		return destinationList;
	}

	/**
	 * 基于Dozer将对象A的值拷贝到对象B中.
	 */
	public static void copy(Object source, Object destinationObject) {
		dozer.map(source, destinationObject);
	}

	
}
最近下载更多
managment  LV3 前天
vluobo  LV1 3月19日
formatself1  LV2 1月28日
Gin19960217  LV4 1月11日
15210869511  LV1 1月8日
WBelong  LV7 2023年12月28日
颜伟峰  LV1 2023年12月20日
小芳同学  LV1 2023年12月7日
1waxzsq212345  LV2 2023年11月22日
pray654  LV2 2023年11月14日
最近浏览更多
managment  LV3 前天
CrystalQ  LV8 4月13日
lo8lukoukoum  LV4 4月10日
ljmadness 4月9日
暂无贡献等级
邓艺妮 4月3日
暂无贡献等级
Chuhhx 4月2日
暂无贡献等级
3334004690  LV3 3月28日
vluobo  LV1 3月19日
暂无贡献等级
zxc123zdq  LV14 3月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友