对象,集合 高效率拷贝


/**
 * 对Cglib BeanCopier进行封装 方便进行bean复制
 * 引用自:https://segmentfault.com/a/1190000006922799
 * @author:
 * @date: 2019/11/7 17:55
 */
public abstract class WrappedBeanCopier {
    private static final Map<String, BeanCopier> beanCopierCache = new ConcurrentHashMap<>();
    private static final Map<String, ConstructorAccess<?>> constructorAccessCache = new ConcurrentHashMap<>();

    /**
     * 拷贝泛型对象
     *
     * <pre>
     * Page&lt;ProductLimitPrice&gt; src = new Page<>();
     * Page&lt;ProductLimitPriceDTO&gt; dist = WrappedBeanCopier.copyProperties(src, new TypeReference&lt;Page&lt;ProductLimitPriceDTO&gt;&gt;(){});
     * </pre>
     */
    public static <T> T copyProperties(Object source, TypeReference<T> targetType) {
        if (source == null) return null;
        return TypeUtils.cast(source, targetType.getType(), ParserConfig.getGlobalInstance());
    }

    /**
     * 拷贝普通对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T copyProperties(Object source, Class<T> targetClass) {
        if (source == null) return null;
        ConstructorAccess<?> constructorAccess = getConstructorAccess(targetClass);
        T t = (T) constructorAccess.newInstance();
        copyProperties(source, t);
        return t;
    }

    /**
     * 拷贝List
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> copyPropertiesOfList(List<?> sourceList, Class<T> targetClass) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return Collections.emptyList();
        }
        ConstructorAccess<?> constructorAccess = getConstructorAccess(targetClass);
        List<T> resultList = new ArrayList<>(sourceList.size());
        for (Object o : sourceList) {
            T t = null;
            try {
                t = (T) constructorAccess.newInstance();
                copyProperties(o, t);
                resultList.add(t);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return resultList;
    }

    private static void copyProperties(Object source, Object target) {
        BeanCopier copier = getBeanCopier(source.getClass(), target.getClass());
        copier.copy(source, target, null);
    }

    private static BeanCopier getBeanCopier(Class<?> sourceClass, Class<?> targetClass) {
        String beanKey = generateKey(sourceClass, targetClass);
        BeanCopier copier = null;
        if (!beanCopierCache.containsKey(beanKey)) {
            copier = BeanCopier.create(sourceClass, targetClass, false);
            beanCopierCache.put(beanKey, copier);
        } else {
            copier = beanCopierCache.get(beanKey);
        }
        return copier;
    }

    private static String generateKey(Class<?> class1, Class<?> class2) {
        return class1.toString() + class2.toString();
    }

    private static ConstructorAccess<?> getConstructorAccess(Class<?> targetClass) {
        ConstructorAccess<?> constructorAccess = constructorAccessCache.get(targetClass.toString());
        if(constructorAccess != null) {
            return constructorAccess;
        }
        try {
            constructorAccess = ConstructorAccess.get(targetClass);
//            constructorAccess.newInstance();
            constructorAccessCache.put(targetClass.toString(),constructorAccess);
        } catch (Exception e) {
            throw new RuntimeException("Create new instance of "+targetClass+" failed: "+e.getMessage());
        }
        return constructorAccess;
    }

}
  Page<ProjectInfoDTO> projectInfoDTOPage = WrappedBeanCopier.copyProperties(pageResult, new TypeReference<Page<ProjectInfoDTO>>() {
        });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值