Java对象通用比对工具

目录

背景

思路

实现


背景

  前段时间的任务中,遇到了需要识别两个对象不同属性的场景,如果使用传统的一个个属性比对equals方法,会存在大量的重复工作,而且为对象新增了属性后,比对方法也需要同步修改,不方便维护,于是便打算构造一个通用的比对方法,需要支持对象嵌套,并支持过滤比对逻辑的策略,适用大部分比对场景

思路

  一个java对象,他的属性是多样的,会有集合类型(如Collection,Map,Array)基本类型(如String,int,double,long等认为是最基本对象的比较),或者对象类型,本质上是一个树状的结构

怎么去遍历对象树

常用的方法就是递归,那怎么定义递归的终点?基于上面三种类型,定为基本类型的比对为递归的终点,对于集合类型,那就再对每一个元素进行比较,对于对象类型,遍历属性进行比较

为了达成通用目的,这里使用java反射实现,基于约定和性能考虑,只通过无参的getter方法获取属性进行比较,另外如何记录当前属性所在的路径?这里参考spel表达式,相对轻量直观

实现

首先定义递归过程记录的上下文信息

这里记录了根对象,当前处理的对象,以及反射获取的对象信息

protected static class CompareContext {
        //对比结果
        private List<CompareResult.CompareInfo> resCollect;
        //当前遍历的对象信息,用于定位
        private ArrayDeque<String> pathMessageStack;
        protected Object rootSourceObj;
        protected Object rootOtherObj;
        protected Object sourceObj;
        protected Object otherObj;
        protected Object sourceInvokeVal;
        protected Object otherInvokeVal;
        //过滤耗费时间
        private long filterCost;

        private String checkCycle() {
            Set<String> set = new LinkedHashSet<>();
            //出现重复退出
            while (set.add(pathMessageStack.removeLast())) {
            }
            String[] elements = new String[set.size()];
            Iterator<String> iterator = set.iterator();
            int index = 0;
            while (iterator.hasNext()) {
                elements[set.size() - 1 - index++] = iterator.next();
            }
            return getPath(elements);
        }

        protected String getPath(String[] elements) {
            Object obj = this.rootSourceObj == null ? this.rootOtherObj : this.rootSourceObj;
            String simpleName = obj.getClass().getSimpleName();
            StringBuilder builder = new StringBuilder(simpleName);
            if (elements == null) {
                elements = this.pathMessageStack.toArray(new String[0]);
            }
            for (int i = elements.length - 1; i >= 0; i--) {
                String cur = elements[i];
                String value = cur.substring(2);
                if (cur.startsWith(FIELD_SIGN)) {
                    builder.append(".").append(value);
                } else {
                    builder.append("[").append(value).append("]");
                }
            }
            return builder.toString();
        }
    }

基于这个上下文,定义过滤接口,因为有些属性可能不用比对,如果命中过滤接口,则跳过

@FunctionalInterface
public interface MethodFilter {
    boolean isSkipCompare(Method method, String methodKey, CompareUtil.CompareContext compareContext);
}

比对结果类,主要存放每个比对不通过属性的值及其路径信息

@Getter
@ToString
public class CompareResult {
    protected boolean same;
    protected Object source;
    protected Object other;
    protected List<CompareInfo> diffList;


    //=====额外信息,可以不关注====
    //整体对比耗时
    private long totalCostMill;

    //方法过滤耗时
    protected long methodFilterCostMill;



    protected void genCost(long start) {
        this.totalCostMill = System.currentTimeMillis() - start;
    }

   

    @Getter
    public static class CompareInfo {
        protected Object sourceVal;
        protected Object otherVal;
        protected String path;

        protected boolean isShow() {
            if (sourceVal == null || otherVal == null) {
                return true;
            }
            if (CompareUtil.getObjType(sourceVal) != Object.class) {
                return true;
            }
            if (sourceVal instanceof Collection) {
                return ((Collection<?>) sourceVal).size() != ((Collection<?>) otherVal).size();
            }
            if (sourceVal instanceof Map) {
                return ((Map<?, ?>) sourceVal).size() != ((Map<?, ?>) otherVal).size();
            }
            if (sourceVal.getClass().isArray()) {
                return ((Object[]) sourceVal).length != ((Object[]) otherVal).length;
            }
            return false;
        }

        @Override
        public String toString() {
            return String.format("path:%s,source:[%s],other:[%s]", path, sourceVal, otherVal);
        }
    }


    public String getBaseObjDiffInfo() {
        return getBaseObjDiffInfo("\n");
    }

    /**
     * 过滤出为空的父对象或基本对象,原生的diffList会包含父对象,不方便查看
     *
     * @param seperator
     * @return
     */
    public String getBaseObjDiffInfo(String seperator) {
        if (!same) {
            StringBuilder builder = new StringBuilder();
            diffList.stream().filter(CompareInfo::isShow).forEach(v -> builder.append(v).append(seperator));
            return builder.toString();
        }
        return "";
    }

}

随后定义入口方法

  public static <T> CompareResult compareObjByGetter(T source, T other, MethodFilter... methodFilters) {
        List<CompareResult.CompareInfo> diffList = new ArrayList<>();
        CompareContext compareContext = new CompareContext();
        compareContext.resCollect = diffList;
        compareContext.pathMessageStack = new ArrayDeque<>(MAX_DEEP_SIZE);
        compareContext.rootSourceObj = source;
        compareContext.rootOtherObj = other;
        long start = System.currentTimeMillis();
        boolean res = compareObjByGetter(source, other, compareContext, methodFilters);
        CompareResult compareResult = new CompareResult();
        compareResult.genCost(start);
        compareResult.diffList = diffList;
        compareResult.same = res;
        compareResult.other = other;
        compareResult.source = source;
        compareResult.methodFilterCostMill = compareContext.filterCost;
        return compareResult;
    }

主流程方法,流程如下

  1. 校验
    1. 路径长度校验
    2. 类校验
  2. 递归终点,对于基本类型,直接走比对方法,并生成路径
  3. 对于复合类型,递归进行比对逻辑
  4. 对于对象类型,采用反射获取具体值,并通过方法过滤进行比较
    1. 首先判断是否有public的无参数getter方法
    2. 有的话采用调用该方法获取两边的值,设置上下文信息
    3. 过滤器过滤无需比对的字段
    4. 如果任一方为空,构造结果
    5. 对于集合类型的属性,需要进一步获取里面的元素进行递归处理
    6. 对于对象类型或基本类型,重复上述步骤

主流程代码如下

private static <T> boolean compareObjByGetter(T source, T other, CompareContext compareContext, MethodFilter... methodFilters) {
        //对比路径太深或出现循环引用不支持
        if (compareContext.pathMessageStack.size() > MAX_DEEP_SIZE) {
            String path = compareContext.checkCycle();
            if (!StringUtils.isEmpty(path)) {
                //路径仅供参考,不一定准确
                throw new IllegalStateException(String.format("reference cycle happen,please check your object,path:%s", path));
            }
            throw new IllegalStateException(String.format("compare path over max size:%s,please check your object", MAX_DEEP_SIZE));
        }
        if(source==other){
            return true;
        }
        if (source == null || other == null) {
            generateResult(source, other, compareContext);
            return false;
        }
        if (!source.getClass().equals(other.getClass())) {
            throw new IllegalArgumentException(String.format("not the same object,class source:%s,class other:%s,path:%s", source.getClass(), other.getClass(),compareContext.getPath(null)));
        }
        //基本类型不再对比getter方法
        if (getObjType(source) != Object.class) {
            boolean isSame = compareBaseObj(source, other);
            if (!isSame) {
                generateResult(source, other, compareContext);
            }
            return isSame;
        }
        //复合类型
        if (isCollectionOrMapOrArray(source)) {
            return dealWithCMA(source, other, compareContext, methodFilters);
        }
        //对象类型,遍历对应的方法
        final boolean[] val = new boolean[]{true};
        ReflectionUtils.doWithMethods(source.getClass(), new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                String name = method.getName();

                if (method.getModifiers() == Modifier.PUBLIC && (name.startsWith("get") || name.startsWith("is"))) {
                    //有入参的getter不处理
                    if (method.getParameterTypes().length != 0) {
                        return;
                    }
                    String methodKey = method.getDeclaringClass().getName() + "#" + method.getName();
                    Object sourceInvokeVal = null;
                    Object otherInvokeVal = null;
                    try {
                        sourceInvokeVal = method.invoke(source);
                        otherInvokeVal = method.invoke(other);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    compareContext.otherObj = other;
                    compareContext.otherInvokeVal = otherInvokeVal;
                    compareContext.sourceObj = source;
                    compareContext.sourceInvokeVal = sourceInvokeVal;
                    //过滤,methodFilter是||关系
                    long start = System.currentTimeMillis();
                    try {
                        for (MethodFilter methodFilter : methodFilters) {
                            if (methodFilter.isSkipCompare(method, methodKey, compareContext)) {
                                return;
                            }
                        }
                    } finally {
                        compareContext.filterCost += System.currentTimeMillis() - start;
                    }
                    if (sourceInvokeVal == null && otherInvokeVal == null) {
                        return;
                    }
                    compareContext.pathMessageStack.push(String.format("%s%s", FIELD_SIGN, ColumnSelector.getFieldName(method.getName(), method.getDeclaringClass().getName())));
                    if (sourceInvokeVal == null || otherInvokeVal == null) {
                        generateResult(sourceInvokeVal, otherInvokeVal, compareContext);
                        val[0] = false;
                        compareContext.pathMessageStack.pop();
                        return;
                    }
                    if (isCollectionOrMapOrArray(sourceInvokeVal)) {
                        val[0] &= dealWithCMA(sourceInvokeVal, otherInvokeVal, compareContext, methodFilters);
                    } else {
                        //对象类型 or 基本类型
                        val[0] &= compareObjByGetter(sourceInvokeVal, otherInvokeVal, compareContext, methodFilters);
                    }
                    compareContext.pathMessageStack.pop();
                }
            }
        });
        return val[0];
    }

对于集合类型的处理如下

主要根据各自的特性取出元素,再给到上面的主流程去执行

 private static boolean dealWithCMA(Object sourceObj, Object otherObj, CompareContext compareContext, MethodFilter... methodFilters) {
        if(sourceObj==otherObj){
            return true;
        }
        boolean isDiff = true;
        if (sourceObj instanceof Collection) {

            Collection<?> sourceCollection = ((Collection<?>) sourceObj);
            Collection<?> otherCollection = ((Collection<?>) otherObj);
            //要求顺序,这里不做排序
            if (sourceCollection.size() != otherCollection.size()) {
                isDiff = false;
            } else {
                Iterator<?> sourceI = sourceCollection.iterator();
                Iterator<?> otherI = otherCollection.iterator();
                int index = 0;
                while (sourceI.hasNext()) {
                    Object sourceElement = sourceI.next();
                    Object otherElement = otherI.next();
                    //下一层不匹配的值
                    compareContext.pathMessageStack.push(String.format("%s%s", COLLECTION_SIGN, index++));
                    isDiff &= compareObjByGetter(sourceElement, otherElement, compareContext, methodFilters);
                    compareContext.pathMessageStack.pop();
                }
            }
        }

        if (sourceObj.getClass().isArray()) {
            Object[] sourceArray = (Object[]) sourceObj;
            Object[] otherArray = (Object[]) otherObj;
            if (sourceArray.length != otherArray.length) {
                isDiff = false;
            } else {
                for (int i = 0; i < sourceArray.length; i++) {
                    Object sourceElement = sourceArray[i];
                    Object otherElement = otherArray[i];
                    compareContext.pathMessageStack.push(String.format("%s%s", ARRAY_SIGN, i));
                    isDiff &= compareObjByGetter(sourceElement, otherElement, compareContext, methodFilters);
                    compareContext.pathMessageStack.pop();
                }
            }
        }
        if (sourceObj instanceof Map) {
            Map<?, ?> sourceMap = (Map) sourceObj;
            Map<?, ?> otherMap = (Map) otherObj;
            if (sourceMap.size() != otherMap.size()) {
                isDiff = false;
            } else {
                HashSet<?> otherKeySet = new HashSet<>(otherMap.keySet());
                for (Map.Entry<?, ?> entry : sourceMap.entrySet()) {
                    Object sourceKey = entry.getKey();
                    Object otherVal = otherMap.get(sourceKey);
                    otherKeySet.remove(sourceKey);
                    compareContext.pathMessageStack.push(String.format("%s%s", KEY_SIGN, sourceKey));
                    isDiff &= compareObjByGetter(entry.getValue(), otherVal, compareContext, methodFilters);
                    compareContext.pathMessageStack.pop();
                }
                if (!otherKeySet.isEmpty()) {
                    for (Object otherKey : otherKeySet) {
                        compareContext.pathMessageStack.push(String.format("%s%s", KEY_SIGN, otherKey));
                        isDiff &= compareObjByGetter(null, otherMap.get(otherKey), compareContext, methodFilters);
                        compareContext.pathMessageStack.pop();
                    }
                }
            }
        }
        if (!isDiff) {
            generateResult(sourceObj, otherObj, compareContext);
        }
        return isDiff;
    }

对于基本对象判断,如果返回object类型需要进一步解析判断,后续如果有其他基本对象,比如业务自定义的基本对象也可以往里面加,或者再进行优化,通过本地线程的方式动态指定基本对象类型

/**
     * 后续比对有其他基本类型可以往里加
     * @param obj
     * @return
     */
    protected static Class<?> getObjType(Object obj) {
        if (obj instanceof Integer) {
            return Integer.class;
        } else if (obj instanceof String) {
            return String.class;
        } else if (obj instanceof BigDecimal) {
            return BigDecimal.class;
        } else if (obj instanceof Long) {
            return Long.class;
        } else if (obj instanceof Enum) {
            return Enum.class;
        } else if (obj instanceof Double) {
            return Double.class;
        } else if (obj instanceof Boolean) {
            return Boolean.class;
        }
        return Object.class;
    }

对于基本类型的比较方法

private static <T> boolean compareBaseObj(T obj, T other) {
        //同一个对象直接通过
        if(obj==other){
            return true;
        }
        //数字类型
        if (obj instanceof Number && obj instanceof Comparable) {
            return ((Comparable<T>) obj).compareTo(other) == 0;
        }
        //其他类型
        return Objects.equals(obj, other);
    }

效果

Java对象比对功能实现大概如此,来看看效果

定义了比对对象

@Getter
    public static class ComposeObj {
        private String id;
        private Object[] arrays;

        private List<Object> list;

        private Map<String, Object> map;

        private ComposeObj son;

        private int idLength;

        public ComposeObj(String id) {
            this.id = id;
            this.idLength = id.length();
        }
    }

测试代码

 ComposeObj sourceObj = new ComposeObj("source");
        ComposeObj arrayObj = new ComposeObj("A1");
        sourceObj.arrays = new Object[]{arrayObj, new ComposeObj("A22")};
        sourceObj.list = Arrays.asList(new ComposeObj("C1"), new ComposeObj("C11"));
        Map<String, Object> map = new HashMap<>();
        map.put("test", "test");
        map.put("test2", "test2");
        sourceObj.map = map;

      ComposeObj  otherObj = new ComposeObj("other");
        ComposeObj arrayObj2 = new ComposeObj("A2");
        otherObj.arrays = new Object[]{arrayObj2, new ComposeObj("A22")};
        otherObj.list = Arrays.asList(new ComposeObj("C2"), new ComposeObj("C11"));
        Map<String, Object> map2 = new HashMap<>();
        map2.put("test", "test2");
        map2.put("test2", "test22");
        otherObj.map = map2;

测试方法

  CompareResult compareResult = CompareUtil.compareObjByGetter(sourceObj, otherObj);
        assert checkPath(compareResult);
        assert compareResult.getDiffList().size() == 9;
        log.info(compareResult.getBaseObjDiffInfo());

结果

默认的支持的表达式过滤器如下

/**
 * 通过简单的表达式
 * "*abc|xxx" ->标识以abc结尾或xxx的属性忽略,不做比对,*代表通配符
 */
public class RegexFieldFilter implements MethodFilter {
    private static final Cache<String, Pattern> REGEX_MAP = CacheBuilder.newBuilder()
            .softValues().maximumSize(2048).build();

    private final List<String> rules;

    private static final String WILDCARD = "*";
    //是否缓存结果,如果调用次数较多(比如属性为list,且实际场景数量较多)可以用启用
    private boolean cacheResult = false;
    //方法key
    private Cache<String, Boolean> resultMap;

    private final Set<String> equalsField = new HashSet<>();

    private final Map<String, String> prefixFieldMap = new HashMap<>();

    private final Map<String, String> suffixFieldMap = new HashMap<>();

    private RegexFieldFilter(String regex, boolean cacheResult) {
        this.cacheResult = cacheResult;
        rules = Splitter.on("|").splitToList(regex);
        for (String rule : rules) {
            //普通比对
            if (!rule.contains(WILDCARD)) {
                equalsField.add(rule);
            }
            //首尾通配符特殊逻辑
            if (onlyOneWildcard(rule)) {
                if (rule.startsWith(WILDCARD)) {
                    suffixFieldMap.put(rule, rule.substring(1));
                }
                if (rule.endsWith(WILDCARD)) {
                    prefixFieldMap.put(rule, rule.substring(0, rule.length() - 1));
                }
            }
        }
        if (cacheResult) {
            resultMap = CacheBuilder.newBuilder().softValues().maximumSize(1024).build();
        }

    }

    public static RegexFieldFilter of(String regex, boolean cacheResult) {
        return new RegexFieldFilter(regex, cacheResult);
    }

    public static RegexFieldFilter of(String regex) {
        return of(regex, false);
    }


    private boolean canSkip(String rule, String fieldName) {
        if (rule.contains(WILDCARD)) {
            if (suffixFieldMap.containsKey(rule)) {
                return fieldName.endsWith(suffixFieldMap.get(rule));
            }
            if (prefixFieldMap.containsKey(rule)) {
                return fieldName.startsWith(prefixFieldMap.get(rule));
            }
            //在中间或多个通配符
            String replace = StringUtils.replace(rule, WILDCARD, ".*");
            Pattern pattern = REGEX_MAP.asMap().computeIfAbsent(replace, Pattern::compile);
            return pattern.matcher(fieldName).matches();
        }
        return equalsField.contains(fieldName);
    }

    private boolean onlyOneWildcard(String rule) {
        if (!rule.contains(WILDCARD)) {
            return false;
        }
        return rule.indexOf(WILDCARD, rule.indexOf(WILDCARD) + 1) == -1;
    }

    @Override
    public boolean isSkipCompare(Method method, String methodKey, CompareUtil.CompareContext context) {
        return cacheResult ? resultMap.asMap().computeIfAbsent(methodKey, s -> judgeSkip(method)) : judgeSkip(method);

    }

    private boolean judgeSkip(Method method) {
        if (!CollectionUtils.isEmpty(rules)) {
            String name = method.getName();
            String fieldName = CompareUtil.getFieldName(name, method.getDeclaringClass().getCanonicalName());
            for (String rule : rules) {
                if (canSkip(rule, fieldName)) {
                    return true;
                }
            }
        }
        return false;
    }


}

测试代码如下,定义这个过滤会把属性名为arrays,带有'l',或者ma开头的属性都过滤掉,不参与比对逻辑

  boolean cacheResult = false;
        RegexFieldFilter of = RegexFieldFilter.of("arrays|*l*|ma*", cacheResult);
        CompareResult compareResult = CompareUtil.compareObjByGetter(source, other, of);
        assert checkPath(compareResult);
        assert compareResult.getDiffList().size() == 15;

ComopareUtil完整代码

https://github.com/97lele/redis-aux/tree/dev/common/src/main/java/com/xl/redisaux/common/utils/compare

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值