记录一个好用的对象拷贝工具 dozer

package com.***.utils;

import org.dozer.DozerBeanMapperBuilder;
import org.dozer.Mapper;

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

/**
 * 对象拷贝
 * 
 * @author ***
 * @date 2018年7月13日 下午5:17:23
 */
public class DozerUtils {

    private static volatile Mapper mapper;

    private static Object          lock = new Object();

    static {

        if (mapper == null) {
            synchronized (lock) {
                if (mapper == null) {
                    mapper = DozerBeanMapperBuilder.create().build();
                }
            }
        }

    }

    public static void copy(Object source, Object target) {
        mapper.map(source, target);
    }

    public static  T copy(Object source, Class clazz){
        if(Objects.isNull(source)){
            return null;
        }
        return mapper.map(source, clazz);
    }

    public static  List copyList(List sourceList, Class clazz){
        if(Objects.isNull(sourceList) || sourceList.size() == 0){
            return Collections.emptyList();
        }
        List dest = new ArrayList<>();
        for (Object source : sourceList){
            dest.add(copy(source, clazz));
        }
        return dest;
    }


}

需要导入依赖包


		
			com.github.dozermapper
			dozer-core
			6.2.0
		

 

你可能感兴趣的:(记录一个好用的对象拷贝工具 dozer)