java自定义Bean对象复制转换工具类

admin2024-05-15  0

使用示例:
 

public static void main(String[] args) {
	//将ProjectA的值copy到ProjectB
	ProjectB projectB = BeanCopierUtil.copy(new ProjectA("1", "项目名称"), ProjectB.class);
	List<ProjectA> list = new ArrayList<>();
	//将ProjectA数组copy到产生新的数组ProjectB
	List<ProjectB> list2 = BeanCopierUtil.copyLists(list, ProjectB.class);
}

工具类:


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter;

public class BeanCopierUtil {

    private static final ConcurrentHashMap<String, BeanCopier> BEAN_COPIER_CACHE = new ConcurrentHashMap();

    public BeanCopierUtil() {
    }

    public static void copy(Object source, Object target, Converter converter) {
        String key = genKey(source.getClass(), target.getClass());
        BeanCopier beanCopier;
        if (BEAN_COPIER_CACHE.containsKey(key)) {
            beanCopier = (BeanCopier)BEAN_COPIER_CACHE.get(key);
        } else {
            beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
            BEAN_COPIER_CACHE.put(key, beanCopier);
        }

        beanCopier.copy(source, target, converter);
    }

    public static void copy(Object source, Object target) {
        copy(source, (Object)target, (Converter)null);
    }

    public static <T> T copy(Object source, Class<T> target, Converter converter) {
        if (source != null && target != null) {
            Object obj;
            try {
                obj = target.newInstance();
            } catch (Exception var5) {
                throw new RuntimeException(var5);
            }

            copy(source, obj, converter);
            return (T) obj;
        } else {
            return null;
        }
    }

    public static <T> T copy(Object source, Class<T> target) {
        return (T) copy(source, (Class)target, (Converter)null);
    }

    public static <S, T> T copy(S source, Supplier<T> target, BiConsumer<S, T> callback) {
        if (source != null && target != null) {
            T obj = target.get();
            copy(source, (Object)obj, (Converter)null);
            if (callback != null) {
                callback.accept(source, obj);
            }

            return obj;
        } else {
            return null;
        }
    }

    public static <T, S> List<T> copyLists(List<S> sourceList, Class<T> targetClass) {
        return copyLists(sourceList, (Class)targetClass, (Converter)null);
    }

    public static <T, S> List<T> copyLists(List<S> sourceList, Class<T> targetClass, Converter converter) {
        if (CollectionUtil.isEmpty(sourceList)) {
            return ListUtil.empty();
        } else {
            List<T> target = new ArrayList(sourceList.size());
            Iterator var4 = sourceList.iterator();

            while(var4.hasNext()) {
                S source = (S) var4.next();
                T newInstance = copy(source, targetClass, converter);
                target.add(newInstance);
            }

            return target;
        }
    }

    public static <S, T> List<T> copyLists(List<S> sources, Supplier<T> targetSupplier, BiConsumer<S, T> callback) {
        if (CollectionUtil.isEmpty(sources)) {
            return ListUtil.empty();
        } else {
            List<T> target = new ArrayList(sources.size());
            Iterator var4 = sources.iterator();

            while(var4.hasNext()) {
                S source = (S) var4.next();
                T newInstance = copy(source, targetSupplier, callback);
                target.add(newInstance);
            }

            return target;
        }
    }

    private static String genKey(Class<?> srcClazz, Class<?> tgtClazz) {
        return srcClazz.getName() + tgtClazz.getName();
    }
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!