ConvertBeanUtil


package com.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.util.DigestUtils;
import com.bwf.common.log.LOG_TYPE;
import com.bwf.common.pagination.PaginationBean;

/**
 * @author BAOWEIFENG234
 * @version $Id: ConvertBeanUtil.java, v 0.1 2017-2-21 下午7:45:50 BAOWEIFENG234 Exp $
 */
public class ConvertBeanUtil {
    private static  Logger           logger = LoggerFactory.getLogger(LOG_TYPE.PAFF_SERVICE.val);
    private static ConcurrentHashMap<String,BeanCopier> cache=new ConcurrentHashMap<String, BeanCopier>();


    /**
     * @param source 源对象class
     * @param target 目标对对象class
     * @param sourceObj 复制的源对象
     * @param useConverter 
     * @return 
     * @throws Exception
     */
    public static <T> T copyBeanProperties(@SuppressWarnings("rawtypes") Class source,Class<T> target,Object sourceObj, boolean useConverter) {
        if(sourceObj==null) return null;
        T t;
        try {
            t = target.newInstance();
        } catch (Exception e) {
            logger.error("", e);
            return null;
        }
        String key=source.getSimpleName()+target.getSimpleName();
        BeanCopier copier = cache.get(key);
        if(copier==null){
             copier=createBeanCopier(source, target, useConverter, key);
        }
        copier.copy(sourceObj, t, null);
        return t;
    }
    /**
     * 
     * 
     * @param sourceObj  源对象
     * @param target  目标对象
     * @param useConverter 
     * @return
     * @throws Exception
     */
    public static <T> T copyBeanProperties(Object sourceObj, T target){
        return copyBeanProperties(sourceObj, target, false);
    }
    /**
     * 
     * 
     * @param sourceObj  源对象
     * @param target  目标对象
     * @param useConverter 
     * @return
     * @throws Exception
     */
    public static <T> T copyBeanProperties(Object sourceObj, T target, boolean useConverter) {
        if(sourceObj==null||target==null) return null;
        String key=sourceObj.getClass().getSimpleName()+target.getClass().getSimpleName();
        BeanCopier copier = cache.get(key);
        if(copier==null){
             copier=createBeanCopier(sourceObj.getClass(), target.getClass(), useConverter, key);
        }
        copier.copy(sourceObj, target, null);
        return target;
    }

    public static <T> List<T> copyListBeanPropertiesToList(List<?> sourceObjs,List<T> targets,Class<T> targetType){
        if(sourceObjs==null||targets==null||targetType==null) return null;
        T t;
        for(Object o:sourceObjs){
            try {
                t=targetType.newInstance();
                targets.add(copyBeanProperties(o,t,false));
            } catch (InstantiationException e) {
                logger.error("", e);
            } catch (IllegalAccessException e) {
                logger.error("", e);
            }
        }
        return targets;
    }
    /**
     * 
     * Pagination对象转换
     * @param sourceObjs
     * @param targets
     * @param targetType
     * @return
     */
    public static <T> PaginationBean<T> copyPageBeanPropertiesToPageBeanList(PaginationBean<?> sourceObjs,PaginationBean<T> targets,Class<T> targetType){

        if(sourceObjs==null||targets==null||targetType==null||sourceObjs.getPageList()==null) return null;
        targets = copyBeanProperties(sourceObjs, targets, false);
        List<T> tlist = new ArrayList<T>();
        T t;
        for(Object o:sourceObjs.getPageList()){
            try {
                t=targetType.newInstance();
                tlist.add(copyBeanProperties(o,t,false));
            } catch (InstantiationException e) {
                logger.error("", e);
            } catch (IllegalAccessException e) {
                logger.error("", e);
            }
        }
        targets.setPageList(tlist);
        return targets;
    }

    @SuppressWarnings("unused")
    private static String getHashKey(String str){
        if(str==null) return null;
        return DigestUtils.md5DigestAsHex(str.getBytes());
    }

    @SuppressWarnings({"rawtypes" })
    private static BeanCopier createBeanCopier(Class sourceClass,Class targetClass,boolean useConverter,String cacheKey){
        BeanCopier copier = BeanCopier.create(sourceClass,targetClass, useConverter);
        cache.putIfAbsent(cacheKey, copier);
        return copier;
    }    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值