【java】ObjectUtils

检查两个对象的属性是否相同,可以排除不需要比较的属性

package com.ljq.common.lib.util;

import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Set;

public class ObjectUtils {

    /**
     * 检查两个对象的属性是否相同,可以排除不需要比较的属性
     *
     * @param obj1          第一个对象
     * @param obj2          第二个对象
     * @param excludeFields 不需要比较的属性名称
     * @return 如果相同字段的数据不相同,返回true,否则返回false
     */
    public static boolean checkFieldEquals(Object obj1, Object obj2, Set<String> excludeFields) {
        if (obj1 == null || obj2 == null) {
            return false;
        }
        if (!obj1.getClass().equals(obj2.getClass())) {
            return false;
        }
        Field[] fields = obj1.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (excludeFields != null && (!CollectionUtils.isEmpty(excludeFields)) && excludeFields.contains(field.getName())) {
                continue;
            }
            field.setAccessible(true);
            try {
                Object value1 = field.get(obj1);
                Object value2 = field.get(obj2);
                if (value1 == null && value2 == null) {
                    continue;
                }
                if (value1 == null || value2 == null) {
                    return true;
                }
                if (!value1.equals(value2)) {
                    return true;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
	
	/**
     * 检查两个对象的属性是否相同(只校验需要验证的字段)
     */
    public static boolean checkNeedFieldEquals(Object obj1, Object obj2, Set<String> excludeFields) {
        if (obj1 == null || obj2 == null) {
            return false;
        }
        if (!obj1.getClass().equals(obj2.getClass())) {
            return false;
        }
        Field[] fields = obj1.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (excludeFields != null && (!CollectionUtils.isEmpty(excludeFields)) && !excludeFields.contains(field.getName())) {
                continue;
            }
            field.setAccessible(true);
            try {
                Object value1 = field.get(obj1);
                Object value2 = field.get(obj2);
                if (value1 == null && value2 == null) {
                    continue;
                }
                if (value1 == null || value2 == null) {
                    return true;
                }
                if (!value1.equals(value2)) {
                    return true;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

	/**
     * 存放Beancopier的MAp 拷贝使用
     */
    static final ConcurrentHashMap<String, BeanCopier> BEAN_COPIER_MAP = new ConcurrentHashMap<>();

    /**
     * 复制开始
     */
    public static <T> T copy(Object source, Class<T> target) {
        try {
            T instance = target.newInstance();
            copy(source, instance, null);
            return instance;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建复制:列表使用这个会性能更好。
     */
    public static void copy(Object source, Object target, Converter converter) {
        String key = genKey(source.getClass(), target.getClass());
        BeanCopier beanCopier;
        if (BEAN_COPIER_MAP.containsKey(key)) {
            beanCopier = BEAN_COPIER_MAP.get(key);
        } else {
            beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
            BEAN_COPIER_MAP.put(key, beanCopier);
        }
        beanCopier.copy(source, target, converter);
    }

    /**
     * 生成key
     *
     * @param srcClazz 源文件的class
     * @param tgtClazz 目标文件的class
     * @return string
     */
    private static String genKey(Class<?> srcClazz, Class<?> tgtClazz) {
        return srcClazz.getName() + tgtClazz.getName();
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘叶风凌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值