package com.base.core.utiles;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BeanUtils extends org.springframework.beans.BeanUtils{

    /**
     * Bean属性复制工具方法。
     *
     * @param dest 目标对象
     * @param src 源对象
     */
    public static void copyBeanProp(Object dest, Object src)
    {
        try
        {
            copyProperties(src, dest);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static <T, V> List<T> listCopyProperties(Class<T> clasaz, List<V> origs){
        List<T> destList = null;
        if (origs != null && !origs.isEmpty()){
            destList = new ArrayList<>(origs.size());
            Iterator<V> iterator = origs.iterator();

            while (iterator.hasNext()){
                Object orig = iterator.next();

                try{
                    T dest = clasaz.newInstance();
                    copyBeanProp(dest, orig);
                    destList.add(dest);
                }catch (InstantiationException  | IllegalAccessException exception){
                    exception.printStackTrace();
                }
            }
        }
        else {
            destList = new ArrayList<>();
        }
        return destList;
    }
}
