java-object-diff对象对比

霍建章
2023-12-01

MetaDiff


MetaDiff

metaDiff 可以对比两个java对象(nativeObject),然后生成一个MetaObject(包含了对象各个字段间的差异信息),同时可以将这个对象转为nativeObject。

public class GenericTest {

    public static void main(String[] args) {
        List<Mod> base = new LinkedList<>();
        base.add(new Man("man"));
        base.add(new Mod("mod"));
        List<Mod> update = new LinkedList<>();
        update.add(new Man("manU"));
        update.add(new Man("modU"));
        MetaObject diff = MetaContext.toMeta(
                new MetaDiffModel(new TypeToken<List<Mod>>() {
                }, update, base)
        );
        System.out.println(diff);
        List<Mod> o = MetaContext.toNative(diff);
        System.out.println(o);
        MetaPath path = MetaPath.builder(false).p("0").build();
        MetaObject<?, ?> obj1 = MetaPathSearch.search(diff, path);
        System.out.println(obj1);
        Object o1 = MetaContext.toNative(obj1);
        MetaObject<?, ?> obj2 = MetaPathSearch.search(obj1, path);
        Object o2 = MetaContext.toNative(obj2);
        System.out.println(obj2);
    }

    public static class Mod {
        private String name;

        public Mod() {
        }

        public Mod(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static class Man extends Mod {

        public Man() {
        }

        public Man(String name) {
            super(name);
        }
    }
}
public class Demo {
    private static MetaChangeWalker.Walker walker = new MetaChangeWalker.Walker() {
        @Override
        public boolean stopWalk(MetaObject metaObject) {
            return false;
        }

        @Override
        public void print(MetaObject metaObject) {
            System.out.println(metaObject);
        }
    };

    public static void main(String[] args) {
        diffMan();

        Map<String, String> map1 = new HashMap<>();
        map1.put("name", "jsen");
        map1.put("age", "12");
        Map<String, String> map2 = new HashMap<>();
        map2.put("name", "jack");
        map2.put("sex", "女");
        MetaObject diffObject = MetaContext.toMeta(
                new MetaDiffModel(new TypeToken<Map<String, String>>() {
                }, map1, map2)
        );
        Object nativeObject = MetaContext.toNative(diffObject);

//
        List<String> array1 = Lists.newArrayList("hello", "world");
        List<String> array2 = Lists.newArrayList("hello", "jsen");
        MetaObject obj3 = MetaContext.toMeta(
                new MetaDiffModel(new TypeToken<List<String>>() {
                }, array1, array2)
        );
        MetaObject obj4 = MetaContext.toMeta(
                new MetaDiffModel(new TypeToken<TEnum>() {
                }, TEnum.AGE, TEnum.COLOR)
        );
        MetaObject obj5 = MetaContext.toMeta(
                new MetaDiffModel(new TypeToken<Integer>() {
                }, null, null)
        );
        Object n5 = MetaContext.toNative(obj5);
        Sex s1 = new Sex(1, new Sex(2, null));
        Sex s2 = new Sex(2, new Sex(3, null));
        MetaObject obj6 = MetaContext.toMeta(
                new MetaDiffModel(TypeToken.of(Sex.class), s1, s2)
        );
        Object nSex = MetaContext.toNative(obj6);
        System.out.println("ok");
    }

    private static void diffMan() {
        Man newMan = new Man(1L, 27, 100L, Lists.newArrayList(1L, 2L), TEnum.AGE,
                LocalDateTime.now());
        Man oldMan = new Man(1L, null, 100L, Lists.newArrayList(2L), TEnum.COLOR,
                LocalDateTime.now());
        MetaObject oLoad = MetaContext.loadMeta(TypeToken.of(Man.class), newMan);
        MetaObject obj = MetaContext.toMeta(new MetaDiffModel(TypeToken.of(Man.class), newMan, oldMan));
        MetaTreePrint.print(obj, System.out::println);
        Object o = MetaContext.toNative(obj);
        MetaChangeWalker.walk(obj, walker);
        MetaPath path = MetaPath.builder(true).p("tEnum").build();
        MetaObject tEnum = MetaPathSearch.search(obj, path);

        Map<String, Man> manMap1 = new HashMap<>();
        manMap1.put("man", newMan);
        Map<String, Man> manMap2 = new HashMap<>();
        manMap2.put("man", oldMan);
        TypeToken typeToken = new TypeToken<Map<String, Man>>() {
        };
        MetaObject objManDiff = MetaContext.toMeta(
                new MetaDiffModel<>(typeToken, manMap1, manMap2)
        );
        Object objMan = MetaContext.toNative(objManDiff);
    }

    public enum TEnum {
        AGE,
        COLOR
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Man {
        @MetaFieldAnnotation(uniqueKey = true)
        private Long id;
        @MetaFieldAnnotation(ignore = true)
        private Integer age;
        private Long high;
        @MetaFieldAnnotation(alias = "支持")
        private List<Long> support;
        @MetaFieldAnnotation(alias = "颜色")
        private TEnum tEnum;
        private LocalDateTime time;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Sex {
        private int sex;
        private Sex sexI;
    }
}

https://gitee.com/jsenht/meta-diff

 类似资料: