首页 > 其他分享 >copyList

copyList

时间:2022-09-22 20:13:31浏览次数:27  
标签:return target targetSupplier copyList param source import


import cn.hutool.core.collection.CollectionUtil;

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

import static org.springframework.beans.BeanUtils.copyProperties;

/**
 * @Description: list
 * @Author yang
 * @Date: 2022/4/14 14:17
 */
public class CopyListUtils {


    /**
     * 转换对象
     *
     * @param source         源对象
     * @param targetSupplier 目标对象供应方
     * @param <S>            源对象类型
     * @param <T>            目标对象类型
     * @return 目标对象
     */
    public static <S, T> T convertTo(S source, Supplier<T> targetSupplier) {
        if (null == source || null == targetSupplier) {
            return null;
        }

        T target = targetSupplier.get();
        copyProperties(source, target);
        return target;
    }


    /**
     * 转换对象
     *
     * @param sources        源对象list
     * @param targetSupplier 目标对象供应方
     * @param <S>            源对象类型
     * @param <T>            目标对象类型
     * @return 目标对象list
     */
    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier) {
        if (CollectionUtil.isEmpty(sources) || null == targetSupplier) {
            return Collections.emptyList();
        }

        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T target = targetSupplier.get();
            copyProperties(source, target);
            list.add(target);
        }
        return list;
    }
}

标签:return,target,targetSupplier,copyList,param,source,import
From: https://www.cnblogs.com/exceptionalChild/p/16720703.html

相关文章