Search code examples
javalistcomparejavers

Javers comparing lists with different order


I have two list with same data but in different orders.

Why is that Javers compareCollections method see two changes (one ValueRemoved and one ValueAdded) with Levenshtein distance?

Maybe Javers doesn't allow multiple fields with @Id annotation?

Code:

class A {
    @Id
    private String a;
    @Id
    private String b;

    public A() {
    }

    public A(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }
}

@Test
public void testJavers() {
    List<A> oldList = Arrays.asList(
            new A("a1", "b1"),
            new A("a2", "b2"));
    List<A> newList = Arrays.asList(
            new A("a2", "b2"),
            new A("a1", "b1"));
    Javers javers = JaversBuilder.javers()
         .withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE)
         .build();
    Diff diff = javers.compareCollections(oldList, newList, A.class);
    System.out.println(javers.getJsonConverter().toJson(diff));
}

Result:

{
  "changes": [
    {
      "changeType": "ListChange",
      "globalId": {
        "valueObject": "org.javers.core.graph.LiveGraphFactory$ListWrapper"
      },
      "property": "list",
      "elementChanges": [
        {
          "elementChangeType": "ValueRemoved",
          "index": 1,
          "value": {
            "a": "a2",
            "b": "b2"
          }
        },
        {
          "elementChangeType": "ValueAdded",
          "index": 0,
          "value": {
            "a": "a2",
            "b": "b2"
          }
        }
      ]
    }
  ]
}

Solution

  • First, you have to choose exactly one property as the Id-property. If you have two properties with @Id, JaVers picks one of them as the Id-property (which one is undefined).

    Second, both algorithms for comparing lists (LEVENSHTEIN and SIMPLE) pay attention to ordering. Lists are ordered collections. If you don't care about ordering use Sets.