ConvertResponse.java 1.24 KB
package cn.com.glsx.entmicro.converter;

import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: 返回体转化参数
 * @date 2021/7/9 11:21
 */
@Data
public class ConvertResponse {

    /**
     * 转化集合
     *
     * @param input 原集合
     * @param clazz 新集合类
     * @param <T>   泛型
     * @return
     */
    public static <E, T> List<T> convertList(List<E> input, Class<T> clazz) {
        List<T> output = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(input)) {
            for (E source : input) {
                T target = BeanUtils.instantiateClass(clazz);
                BeanUtils.copyProperties(source, target);
                output.add(target);
            }
        }
        return output;
    }


    /**
     * 转化类型
     *
     * @param object 原类
     * @param clazz 新类
     * @return
     */
    public static <T> T convertObject(Object object, Class<T> clazz) {
        T target = BeanUtils.instantiateClass(clazz);
        BeanUtils.copyProperties(object, target);
        return target;
    }
}