package com.zm.order.tool.server.utils;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Description 转换
 * @Author: ph
 * @Date: 2020/4/25 13:27
 */
public class ConvertUtils {

    private static Mapper mapper = DozerBeanMapperBuilder.buildDefault();

    public static <T> T convertObject(Object object, Class<T> clazz) {
        if (Objects.isNull(object)) {
            return null;
        }
        return mapper.map(object, clazz);
    }

    public static <T> T convert(Object object, Class<T> clazz) {
        if (Objects.isNull(object)) {
            return null;
        }
        return mapper.map(object, clazz);
    }

    public static <T> List<T> convertList(List<? extends Object> objectList, Class<T> clazz) {
        if (CollectionUtils.isEmpty(objectList)) {
            return Collections.EMPTY_LIST;
        }
        List<T> list = new ArrayList<>();
        for (Object obj: objectList) {
            if (obj != null) {
                list.add(mapper.map(obj, clazz));
            }
        }
        return list;
    }


}
