当前位置:网站首页>Collection object replication

Collection object replication

2022-06-26 09:22:00 Java backend Guide

/**
     *  Copy of collection data 
     * @param sources:  The data source class 
     * @param target:  Target class ::new(eg: UserVO::new)
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
    
        return copyListProperties(sources, target, null);
    }


    /**
     *  Copy of collection data with callback function ( You can customize the field copy rules )
     * @param sources:  The data source class 
     * @param target:  Target class ::new(eg: UserVO::new)
     * @param callBack:  Callback function 
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack) {
    
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
    
            T t = target.get();
            copyProperties(source, t);
            list.add(t);
            if (callBack != null) {
    
                //  Callback 
                callBack.callBack(source, t);
            }
        }
        return list;
    }
@FunctionalInterface
public interface BeanCopyUtilCallBack <S, T> {
    
    /**
     *  Define the default callback method 
     * @param t
     * @param s
     */
    void callBack(S t, T s);
}
原网站

版权声明
本文为[Java backend Guide]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260850251104.html