0

P.P.S. Ok, I founded hier Javers Comparing Lists following comment

There is no concept of move in JaVers list comparison algorithms. After a move there will be two changes reported: ValueAdded and ValueRemoved, just like you have mentioned.

But how then I can recognize, that the list actually has not been changed?

P.S. Even if I get @Entities and @Id to ZasFish, ZasCatchZone and ZasCatchArea I still get Diff: 1. NewObject{globalId:'my.javers.comparator.ZasFish/2'} 2. ObjectRemoved{globalId:'my.javers.comparator.ZasFish/1'}

I'm trying to compare lists of custom objects. I set LEVENSHTEIN_DISTANCE and created custom comparators. The only difference between objects is the order of values in the lists. I would expect "no changes", but I got ListChange. The result and example is below. What am I doing wrong?

Many thanks and regards, Andrej

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);

    }

}
Community
  • 1
  • 1
anli
  • 1
  • 2

1 Answers1

0

I think I founded a solution for my issue. If I register values and value objects like this

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();

Then I get as diff

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']}

And as I don't have any ValueChange in this case I can ignore ListChange -> my Lists are identical.

anli
  • 1
  • 2