java 中实体类值转换

public class ConvertModelHelper {

    /**
     * 转换模型对象类型
     *
     * @param source    要转换的源对象
     * @param clzTarget 转换后的类型
     * @param <T>       转换后的类型
     * @return 转换后的对象
     * @deprecated use {@link #convert(Object, Object)} instead
     */
    @Deprecated
    public static <T> T convert(Object source, Class<T> clzTarget) {
        try {
            T target = clzTarget.newInstance();
            assign(source, target);
            return target;
        } catch (ReflectiveOperationException e) {
            throw new ConvertModelException();
        }
    }

    /**
     * 转换模型对象类型
     *
     * @param source 要转换的源对象
     * @param target 转换的目标对象
     * @param <T>    转换目标类型
     * @return 转换的目标对象
     */
    public static <T> T convert(Object source, T target) {
        if (source == null) {
            return null;
        }
        assign(source, target);
        return target;
    }

    /**
     * 将模型对象的所有属性复制到另一个模型对象
     *
     * @param source 源对象
     * @param target 目标对象
     * @param <T>    目标类型
     * @return 复制失败的属性集合
     */
    public static <T> Set<String> assign(Object source, T target) {
        Set<String> failedFields = new HashSet<>();
        Class clzTarget = target.getClass();
        Class clzSource = source.getClass();
        Field[] fields = clzSource.getDeclaredFields();
        if (fields != null) {
            for (Field field : fields) {
                String fieldName = field.getName();
                String setterName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                try {
                    Method setter = clzTarget.getMethod(setterName, field.getType());
                    Method getter = clzSource.getMethod(getterName);
                    Object returnVal = getter.invoke(source);
                    setter.invoke(target, returnVal);
                } catch (ReflectiveOperationException ex) {
                    failedFields.add(fieldName);
                }
            }
        }
        return failedFields;
    }

    /**
     * 转换 QueryResponse 的元素类型
     *
     * @param from    源对象
     * @param toClass 目标类型
     * @param <F>     源类型
     * @param <T>     目标类型
     * @return 转换后的 QueryResponse
     */
   /* public static <F, T> QueryResponse<T> convertQueryResponse(QueryResponse<F> from, Class<T> toClass) {
        QueryResponse<T> to = new QueryResponse<>(
            convertList(from.getData(), new LinkedList<>(), toClass)
        );

        to.setTotalPages(from.getTotalPages());
        to.setTotalItems(from.getTotalItems());
        to.setParams(from.getParams());
        to.setPageNum(from.getPageNum());

        return to;
    }*/

    /**
     * 转换 List 的元素类型
     *
     * @param from    源对象
     * @param toClass 目标类型
     * @param <F>     源类型
     * @param <T>     目标类型
     * @return 转换后的 List
     */
    public static <F, T> List<T> convertList(List<F> from, List<T> to, Class<T> toClass) {
        return (List<T>)convertCollection(from, to, toClass);
    }

    /**
     * 转换 Collection 的元素类型
     *
     * @param from    源对象
     * @param toClass 目标类型
     * @param <F>     源类型
     * @param <T>     目标类型
     * @return 转换后的 Collection
     */
    public static <F, T> Collection<T> convertCollection(Collection<F> from, Collection<T> to, Class<T> toClass) {
        return new CollectionConverter<F, T>(from).to(to, toClass);
    }

    /**
     * Collection 类型转换器
     *
     * @param <F> 源类型
     * @param <T> 目标类型
     */
    public static class CollectionConverter<F, T> {
        private final Collection<F> fromCollection;

        /**
         * 创建类型转换器
         *
         * @param fromCollection 源对象
         */
        public CollectionConverter(Collection<F> fromCollection) {
            this.fromCollection = fromCollection;
        }

        /**
         * 执行类型转换
         *
         * @param toCollection 目标 Collection
         * @param toClass      目标类型
         * @return 目标 Collection
         */
        public Collection<T> to(Collection<T> toCollection, Class<T> toClass) {
            try {
                for (F fromElement : fromCollection) {
                    toCollection.add(ConvertModelHelper.convert(fromElement, toClass.newInstance()));
                }
            } catch (ReflectiveOperationException e) {
                throw new ConvertModelException();
            }
            return toCollection;
        }
    }

    /**
     * 转换异常
     */
    private static class ConvertModelException extends RuntimeException {
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值