List数据拷贝,支持自定义lambda语句

解决拷贝List数据问题,简化代码

一、无lambda语句

原代码

for (S source : sources) {
    T target = new T();
    copyProperties(source, target);
    list.add(target);
}

简化后

BeanConvertUtils.converToList(sources, T::new)

 二、自定义lambda语句

原代码

for (S source : sources) {
    T target = new T();
    copyProperties(source, target);
    target.setxxx(source.getxxx());
    list.add(target);
}

简化后

BeanConvertUtils.converToList(sources, T::new,(s, t) -> t.setxxx(s.getxxx()));

 

三、工具代码

package com.mars.otn.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author: 
 * @date: 2022/6/23
 * @description: 数据拷贝,支持自定义lambda
 */
public class BeanConvertUtils extends BeanUtils {


    /**
     * @param source   源对象
     * @param target  目标对象
     * @return {@link T}目标对象
     */
    public static <S, T> T convertTo(S source, Supplier<T> target) {
        return convertTo(source, target, null);
    }

    /**
     * 转换对象
     *
     * @param source         源对象
     * @param target 目标对象
     * @param callBack       回调方法
     * @param <S>            源对象类型
     * @param <T>            目标对象类型
     * @return {@link T}     目标对象
     */
    public static <S, T> T convertTo(S source, Supplier<T> target, ConvertCallBack<S, T> callBack) {
        if (null == source || null == target) {
            return null;
        }
        T targetOut = target.get();
        copyProperties(source, targetOut);
        if (callBack != null) {
            callBack.callBack(source, targetOut);
        }
        return targetOut;
    }

    /**
     * @param sources   源对象
     * @param target   目标对象
     * @return {@link List} 目标对象list
     */
    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> target) {
        return convertListTo(sources, target, null);
    }

    /**
     * 转换对象
     *
     * @param sources        源对象list
     * @param target 目标对象供应方
     * @param callBack       回调方法
     * @param <S>            源对象类型
     * @param <T>            目标对象类型
     * @return {@link List}  目标对象list
     */
    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> target, ConvertCallBack<S, T> callBack) {
        if (CollectionUtils.isEmpty(sources) || null == target) {
            return Collections.emptyList();
        }
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T targetOut = target.get();
            copyProperties(source, targetOut);
            if (callBack != null) {
                callBack.callBack(source, targetOut);
            }
            list.add(targetOut);
        }
        return list;
    }

    /**
     * 回调接口
     *
     * @param <S> 源对象类型
     * @param <T> 目标对象类型
     */
    @FunctionalInterface
    public interface ConvertCallBack<S, T> {
        void callBack(S t, T s);
    }
}

 

posted @ 2022-07-05 16:24  那时·此刻  阅读(293)  评论(0编辑  收藏  举报
/* 看板娘 */