package com.neusoft.smart.pos.framework.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ListUtils {
	
	/**
	 * 将泛型list转换为其它泛型list，并把相同字段进行赋值
	 * @param sourceList
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> convertListToList(List<?> sourceList,Class<T> clazz){
		List<T> resultList = new ArrayList<>();
		T t;
		try {
			for(Object temp:sourceList){
				t = clazz.newInstance();
				BeanUtils.copyProperties(temp,t);
				resultList.add(t);
			}
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
		return resultList;
	}

	public static <T> List<T> convertListMapToListClass(List<Map> sourceList,Class<T> clazz){
		List<T> resultList = new ArrayList<>();
		T t;
		try {
			for(Map temp:sourceList){
				t = clazz.newInstance();
				t = MapBeanUtils.mapToBean(temp,clazz);
//				BeanUtils.copyProperties(temp,t);
				resultList.add(t);
			}
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
		return resultList;
	}
}
