LEVENSHTEIN_DISTANCE通过比较列表

时间:2016-10-17 20:25:05

标签: list compare javers

P.P.S。好的,我在评论

后创建了hier Javers Comparing Lists

在JaVers列表比较算法中没有移动的概念。移动后,将报告两个更改:ValueAdded和ValueRemoved,就像您提到的那样。

但是,我怎么能认识到,列表实际上还没有改变?

P.S。即使我得到@Entities和@Id给ZasFish,ZasCatchZone和ZasCatchArea我仍然得到     DIFF:     1. NewObject {globalId:' my.javers.comparator.ZasFish / 2'}     2. ObjectRemoved {globalId:' my.javers.comparator.ZasFish / 1'}

我试图比较自定义对象列表。我设置了LEVENSHTEIN_DISTANCE并创建了自定义比较器。对象之间的唯一区别是列表中值的顺序。我希望"没有变化",但我得到了ListChange。结果和示例如下。我做错了什么?

非常感谢和问候, 安德烈

Diff:
1. ListChange{globalId:'my.javers.comparator.ZasFish/', property:'zones', containerChanges:[(2).'my.javers.comparator.ZasCatchZone@7a9273a8'>>'my.javers.comparator.ZasCatchZone@26a7b76d', (1).'my.javers.comparator.ZasCatchZone@4abdb505'>>'my.javers.comparator.ZasCatchZone@7ce6a65d', (0).'my.javers.comparator.ZasCatchZone@1500955a'>>'my.javers.comparator.ZasCatchZone@e874448']}
2. ListChange{globalId:'my.javers.comparator.ZasFish/', property:'areas', containerChanges:[(2).'my.javers.comparator.ZasCatchArea@7113b13f'>>'my.javers.comparator.ZasCatchArea@45820e51', (1).'my.javers.comparator.ZasCatchArea@42d8062c'>>'my.javers.comparator.ZasCatchArea@6043cd28', (0).'my.javers.comparator.ZasCatchArea@cb51256'>>'my.javers.comparator.ZasCatchArea@59906517']}



package my.javers.comparator;

public class ZasCatchArea {
    String catchArea;

    public String getCatchArea() {
        return catchArea;
    }

    public void setCatchArea(String catchArea) {
        this.catchArea = catchArea;
    }
}

public class ZasCatchZone {
    String catchZone;

    public String getCatchZone() {
        return catchZone;
    }

    public void setCatchZone(String catchZone) {
        this.catchZone = catchZone;
    }
}

public class ZasFish {
    String fischName;
    List<ZasCatchZone> zones = new ArrayList<ZasCatchZone>();
    List<ZasCatchArea> areas = new ArrayList<ZasCatchArea>();

    public String getFischName() {
        return fischName;
    }

    public void setFischName(String fischName) {
        this.fischName = fischName;
    }

    public List<ZasCatchZone> getZones() {
        return zones;
    }

    public void setZones(List<ZasCatchZone> zones) {
        this.zones = zones;
    }

    public List<ZasCatchArea> getAreas() {
        return areas;
    }
    public void setAreas(List<ZasCatchArea> areas) {
        this.areas = areas;
    }
}

public class ZasCatchAreaComparator implements 
CustomPropertyComparator<ZasCatchArea, ValueChange> {

    public ValueChange compare(ZasCatchArea left, ZasCatchArea right, 
                GlobalId affectedCdoId, Property propertyName) {
        if (left.getCatchArea().equals(right.getCatchArea()))
            return null;

        return new ValueChange(affectedCdoId, propertyName.getName(), left, right);
    }

}

public class ZasCatchZoneComparator implements 
CustomPropertyComparator<ZasCatchZone, ValueChange> {

    public ValueChange compare(ZasCatchZone left, ZasCatchZone right, 
                    GlobalId affectedCdoId, Property propertyName) {
        if (left.getCatchZone().equals(right.getCatchZone()))
            return null;

        return new ValueChange(affectedCdoId, propertyName.getName(), left, right);
    }

}

public class MyComparator {

     public static void main(String[] args) {
        Javers javers = JaversBuilder.javers()
            .registerCustomComparator(new ZasCatchAreaComparator(), ZasCatchArea.class)
            .registerCustomComparator(new ZasCatchZoneComparator(), ZasCatchZone.class)
            .withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE).build();

        ZasFish fisch1 = new ZasFish();
        ZasFish fisch2 = new ZasFish();

        ZasCatchZone z1 = new ZasCatchZone();
        z1.setCatchZone("zone1");
        ZasCatchZone z2 = new ZasCatchZone();
        z2.setCatchZone("zone2");
        ZasCatchZone z3 = new ZasCatchZone();
        z3.setCatchZone("zone3");

        fisch1.getZones().add(z1);
        fisch1.getZones().add(z2);
        fisch1.getZones().add(z3);

        ZasCatchArea a1 = new ZasCatchArea();
        a1.setCatchArea("area1");
        ZasCatchArea a2 = new ZasCatchArea();
        a2.setCatchArea("area2");
        ZasCatchArea a3 = new ZasCatchArea();
        a3.setCatchArea("area3");

        fisch1.getAreas().add(a1);
        fisch1.getAreas().add(a2);
        fisch1.getAreas().add(a3);

        ZasCatchZone z4 = new ZasCatchZone();
        z4.setCatchZone("zone3");
        ZasCatchZone z5 = new ZasCatchZone();
        z5.setCatchZone("zone2");
        ZasCatchZone z6 = new ZasCatchZone();
        z6.setCatchZone("zone1");

        fisch2.getZones().add(z4);
        fisch2.getZones().add(z5);
        fisch2.getZones().add(z6);

        ZasCatchArea a4 = new ZasCatchArea();
        a4.setCatchArea("area3");
        ZasCatchArea a5 = new ZasCatchArea();
        a5.setCatchArea("area1");
        ZasCatchArea a6 = new ZasCatchArea();
        a6.setCatchArea("area2");

        fisch2.getAreas().add(a4);
        fisch2.getAreas().add(a5);
        fisch2.getAreas().add(a6);

        Diff diff = javers.compare(fisch1, fisch2);


        System.out.println(diff);

    }

}

1 个答案:

答案 0 :(得分:0)

我想我为我的问题找到了一个解决方案。如果我像这样注册值和值对象

final Javers javers = JaversBuilder.javers()
            .registerCustomComparator(new ZasCatchAreaComparator(), ZasCatchArea.class)
            .registerCustomComparator(new ZasCatchZoneComparator(), ZasCatchZone.class)
            .registerValue(ZasCatchArea.class).registerValue(ZasCatchZone.class).registerValueObject(ZasFish.class)
            .withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE).build();

然后我得到差异

1. ListChange{globalId:'my.javers.comparator.ZasFish/', property:'zones', containerChanges:[(2).'my.javers.comparator.ZasCatchZone@19bef4e5'>>'my.javers.comparator.ZasCatchZone@3f1abed', (1).'my.javers.comparator.ZasCatchZone@e37f307f'>>'my.javers.comparator.ZasCatchZone@2ad1e8a5', (0).'my.javers.comparator.ZasCatchZone@2ccd3209'>>'my.javers.comparator.ZasCatchZone@c0fd1f30']}
2. ListChange{globalId:'my.javers.comparator.ZasFish/', property:'areas', containerChanges:[(2).'my.javers.comparator.ZasCatchArea@48115f4e'>>'my.javers.comparator.ZasCatchArea@a1efa37', (1).'my.javers.comparator.ZasCatchArea@c08d9768'>>'my.javers.comparator.ZasCatchArea@d65a5a2', (0).'my.javers.comparator.ZasCatchArea@bb03583'>>'my.javers.comparator.ZasCatchArea@1ebaaab0']}

由于我在这种情况下没有任何ValueChange,我可以忽略ListChange - &gt;我的名单是相同的。