首页 > 解决方案 > 包含数组列表的两个对象的比较

问题描述

首先,只需检查四个模型所有者,所有者,地址(与您的不同),代码信息来自这里 https://github.com/rckr20/javers/tree/master/javerscore/src/test/java/org/javers/core /示例/模型

现在我正在做的是以这种方式使用 LEVENSHTEIN_DISTANCE 算法比较两个 Owners 对象。

/*Comparing List */
Owners Owners1 = new Owners();
Owners1.add(new Owner(1,new Addresss("Delhi",null,new CodeInfo(null))));

Owners Owners2 = new Owners();
Owners2.add(new Owner(2,new Addresss("Noida","SEZ",new CodeInfo("abc"))));

Diff diff = JaversBuilder.javers().withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE).build().compare(Owners1, Owners2);

现在你会问为什么我不使用简单的 List 而不是使用 Owners(它扩展了所有者的 ArrayList)。其次,我不能在这里使用 compareCollection()。我目前的项目架构就是这样,我无法改变。

因此,比较我需要的是值更改,但它返回列表更改。

Diff:

changes on org.javers.core.graph.LiveGraphFactory$ListWrapper/ :
'list' collection changes :
0. 'org.divik.javers.JaversBasic.Owner@3f0846c6' changed to 'org.divik.javers.JaversBasic.Owner@77a98a6a'
And just for solving this i just tried to convert the Owners object into the JsonNode.

您也可以查看此 https://easyupload.io/aez105

刚刚尝试将所有者映射为实体,但结果相同。但是得到了解决方案我们可以这样比较它。

Diff diff = JaversBuilder.javers().withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE).build().compareCollections(((List)Owners1), (List)Owners2,Owner.class);

但是如果我将所有者(即类扩展数组列表)嵌套在某个模型中,则再次面临问题

   Example:--
    class Car{
  @Id
   int id;
   Owners owners;

   public Car(int id, Owners owners) {
     this.id= id;
     this.owners = owners;
    }
   }

我再次得到嘈杂的结果。

标签: javers

解决方案


  1. 我可以做的任何事情来让javers了解所有者的类型。

  2. 由于在我的实际用例中有多个列表,并且使用 compareCollection 比较每个列表效率不高,因为它们不在顶层。

  3. 我可以实现类似 CustomList 或 Property 比较器的东西,并使用 itemType.class 为每个这样的列表注册它吗

示例:-- javers = JaversBuilder.javers() .registerCustomType(Owners, new CustomListComparator())

并覆盖 CustomListComparator 方法并调用 compareCollection 并在其中传递类型,即 Owner 在这种情况下。


推荐阅读