package com.iailab.framework.common.util.object; import cn.hutool.core.bean.BeanUtil; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.collection.CollectionUtils; import java.util.List; import java.util.function.Consumer; /** * Bean 工具类 * * 1. 默认使用 {@link cn.hutool.core.bean.BeanUtil} 作为实现类,虽然不同 bean 工具的性能有差别,但是对绝大多数同学的项目,不用在意这点性能 * 2. 针对复杂的对象转换,可以搜参考 AuthConvert 实现,通过 mapstruct + default 配合实现 * * @author iailab */ public class BeanUtils { public static T toBean(Object source, Class targetClass) { return BeanUtil.toBean(source, targetClass); } public static T toBean(Object source, Class targetClass, Consumer peek) { T target = toBean(source, targetClass); if (target != null) { peek.accept(target); } return target; } public static List toBean(List source, Class targetType) { if (source == null) { return null; } return CollectionUtils.convertList(source, s -> toBean(s, targetType)); } public static List toBean(List source, Class targetType, Consumer peek) { List list = toBean(source, targetType); if (list != null) { list.forEach(peek); } return list; } public static PageResult toBean(PageResult source, Class targetType) { return toBean(source, targetType, null); } public static PageResult toBean(PageResult source, Class targetType, Consumer peek) { if (source == null) { return null; } List list = toBean(source.getList(), targetType); if (peek != null) { list.forEach(peek); } return new PageResult<>(list, source.getTotal()); } public static void copyProperties(Object source, Object target) { if (source == null || target == null) { return; } BeanUtil.copyProperties(source, target, false); } }