比较两对象属性差异性


import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject

class Diff {

    static Map<String, Object> equals(Object source, Object target) {
        Map<String, Object> diffMap = new HashMap<>()
        JSONObject sourceJson = source ? JSON.toJSON(source) as JSONObject : new JSONObject()
        JSONObject targetJson = target ? JSON.toJSON(target) as JSONObject : new JSONObject()
        diffMap.putAll(equalsJsonObject(sourceJson, targetJson, null))
        return diffMap.sort()
    }

    static Map<String, Object> equals(Object source, Object target, List<Pattern> regexIgnoreFiled) {
        Map<String, Object> diffMap = equals(source, target)
        if (regexIgnoreFiled) {
            diffMap = diffMap.findAll { entry -> !regexIgnoreFiled.find { regex -> entry.key ==~ regex } }
        }
        return diffMap.sort()
    }

    private static Map<String, Object> equalsJsonObject(JSONObject sourceJson, JSONObject targetJson, String key) {
        Map<String, Object> diffMap = new HashMap<>()
        Set<Map.Entry<String, Object>> sourceEntrySet = sourceJson.entrySet()
        Set<Map.Entry<String, Object>> targetEntrySet = targetJson.entrySet()
        Set<Map.Entry<String, Object>> baseEntrySet = sourceEntrySet.size() > targetEntrySet.size() ? sourceEntrySet : targetEntrySet
        baseEntrySet.each { entry ->
            String diffKey = key ? "${key}.${entry.key}" : entry.key
            Object sourceValue = sourceJson.get(entry.key)
            Object targetValue = targetJson.get(entry.key)
            Object value = entry.value ?: sourceValue ?: targetValue
            if (value instanceof JSONObject) {
                diffMap.putAll(equalsJsonObject(sourceValue as JSONObject ?: new JSONObject(), targetValue as JSONObject ?: new JSONObject(), diffKey))
            } else if (value instanceof JSONArray) {
                diffMap.putAll(equalsJsonArray(sourceValue as JSONArray ?: new JSONArray(), targetValue as JSONArray ?: new JSONArray(), diffKey))
            } else {
                if ((sourceValue || targetValue) && sourceValue != targetValue) {
                    diffMap.put(diffKey, "(${sourceValue != null ? sourceValue : ""}) -> (${targetValue != null ? targetValue : ""})".toString())
                }
            }
        }
        return diffMap
    }

    private static Map<String, Object> equalsJsonArray(JSONArray sourceJson, JSONArray targetJson, String key) {
        Map<String, Object> diffMap = new HashMap<>()
        int minSize = Math.min(sourceJson.size(), targetJson.size())
        int maxSize = Math.max(sourceJson.size(), targetJson.size())
        for (int i = 0; i < minSize; i++) {
            Object sourceValue = sourceJson.get(i)
            Object targetValue = targetJson.get(i)
            String diffKey = "${key}[${i}]"
            if (sourceValue instanceof JSONObject) {
                diffMap.putAll(equalsJsonObject(sourceValue as JSONObject ?: new JSONObject(), targetValue as JSONObject ?: new JSONObject(), diffKey))
            } else if (sourceValue instanceof JSONArray) {
                diffMap.putAll(equalsJsonArray(sourceValue as JSONArray ?: new JSONArray(), targetValue as JSONArray ?: new JSONArray(), diffKey))
            } else if (sourceValue != targetValue) {
                diffMap.put(diffKey, "(${sourceValue}) -> (${targetValue})".toString())
            }
        }
        if (minSize != maxSize) {
            JSONArray baseJson = sourceJson.size() > targetJson.size() ? sourceJson : targetJson
            boolean delete = sourceJson.size() > targetJson.size()
            for (int i = minSize; i < maxSize; i++) {
                Object value = baseJson.get(i)
                String diffKey = "${key}[${i}]"
                if (value instanceof JSONObject) {
                    diffMap.putAll(equalsJsonObject(
                            delete ? value as JSONObject : new JSONObject(),
                            !delete ? value as JSONObject : new JSONObject(),
                            diffKey))
                } else if (value instanceof JSONArray) {
                    diffMap.putAll(equalsJsonArray(
                            delete ? value as JSONArray : new JSONArray(),
                            !delete ? value as JSONArray : new JSONArray(),
                            diffKey))
                } else {
                    diffMap.put(diffKey, "(${delete ? value : ""}) -> (${!delete ? value : ""})".toString())
                }
            }
        }
        return diffMap
    }

}
static void main(String[] args) {
        User source = new User(
                userId: "1-userId",
                userName: "1-userName",
                school: new School(
                        name: "1-name",
                        level: "1-level",
                ),
                addresses: [
                        new Address(
                                city: "1-city",
                                street: "1-street"
                        ),
                        new Address(
                                city: "1-1-city",
                                street: "1-1-street"
                        )
                ],
                families: [
                        [
                                new Family(
                                        member: "1-member",
                                        ship: "1-ship"
                                ),
                                new Family(
                                        member: "1-1-member",
                                        ship: "1-1-ship"
                                )
                        ],
                        [
                                new Family(
                                        member: "1-1-1-member",
                                        ship: "1-1-1-ship"
                                ),
                                new Family(
                                        member: "1-1-1-1-member",
                                        ship: "1-1-1-1-ship"
                                )
                        ]
                ],
                friends: ["2", "2-2-2", "1"]
        )

        User target = new User(
                userId: "1-userId",
                userName: "1-userName",
                school: new School(
                        name: "2-name",
                        level: "2-level",
                ),
                addresses: [
                        new Address(
                                city: "1-city",
                                street: "1-street"
                        ),
                        new Address(
                                city: "1-1-city",
                                street: "2-2-street"
                        )
                ],
                families: [
                        [
                                new Family(
                                        member: "2-member",
                                        ship: "2-ship"
                                ),
                                new Family(
                                        member: "2-2-member",
                                        ship: "2-2-ship"
                                )
                        ],
                        [
                                new Family(
                                        member: "2-2-2-member",
                                        ship: "2-2-2-ship"
                                )
                        ]
                ],
                friends: ["2", "2-2", "2-2-2"]
        )
        print(JSON.toJSONString(Diff.equals(target,source)))
    }

    static class User {
        String userId
        String userName
        School school
        List<Address> addresses
        List<List<Family>> families
        List<String> friends
    }

    static class Address {
        String city
        String street
    }

    static class School {
        String name
        String level
    }

    static class Family {
        String member
        String ship
    }
© 版权声明
THE END
喜欢就支持一下吧
点赞13赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片