Javers Comparing Lists

2019-09-14 02:00发布

I'm attempting to convert a diff on two lists of Entities into a more human readable format by interpreting container changes (v1.6.0).

If I have a List of Entities (listBefore):

  entity1
  entity2
  entity3
  entity4

and I reorder the list (listAfter)

  entity1
  entity4
  entity2
  entity3

The result of comparing these lists using

Javers.compareCollections( listBefore, listAfter, Entity.class ) 

is:

containerChanges:[(3).removed:'entity4', (1).added:'entity4']

From this I can deduce that: entity4 moved from index 3 to index 1.

If I repeat the same comparison, this time adding a new item to the second list:

  entity1
  entity4
  entity2
  entity3
  entity5

the result of the comparison is:

containerChanges:[(3).'entity4'>>'entity5', (1).added:'entity4']

This seems to have missed the fact that 'entity5' was added at index (4) (i.e. not 3) and 'entity4' moved as in the previous example.

Update: I am using the Levenshtein comparator in the above examples.

Any clarification would be appreciated.

标签: javers
1条回答
对你真心纯属浪费
2楼-- · 2019-09-14 02:45

JaVers has two algorithms for comparing lists: Simple and Levenshtein see http://javers.org/documentation/diff-configuration/#list-algorithms

Simple algorithm is used by default, mostly because its speed. Levenshtein is smarter but could be slow for very large lists. From JaVers doc:

SIMPLE algorithm generates changes for shifted elements (in case when elements are inserted or removed in the middle of a list). On the contrary, Levenshtein algorithm calculates short and clear change list even in case when elements are shifted. It doesn’t care about index changes for shifted elements.

My suggestion, try Levenshtein distance algorithm.

查看更多
登录 后发表回答