我使用JaVers v3.0.0比较两个包含对象列表的对象。我比较的对象在列表内容上有所不同,例如,对象从列表中删除。javers:确定或避免重复的差异结果
当我执行此比较时,我得到两个更改对象:一个ListChange和一个ObjectRemoved。
当呈现结果时,我需要确保相同的更改不会出现两次。我很难弄清楚如何识别或避免我得到的这些重复。我曾尝试使用GlobalID,但最终解析不完全安全的字符串。我也尝试过从我的演示文稿中跳过ListChange或ObjectRemoved,但是当我还有一个ListChange值的列表或ObjectRemoved不在列表中的对象时会出现问题。
@Test
public void javersDuplicateDiffResult() {
MyMainObj objA = new MyMainObj(Arrays.asList(new MyListedObj("hello"), new MyListedObj("world")));
MyMainObj objB = new MyMainObj(Arrays.asList(new MyListedObj("hello")));
Javers javers = JaversBuilder.javers()
.withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE)
.build();
Diff res = javers.compare(objA, objB);
System.out.println(res);
Assert.assertEquals(1, res.getChanges().size());
}
class MyMainObj {
private List<MyListedObj> theObjectList;
public MyMainObj(List<MyListedObj> anObjectList) {
this.theObjectList = anObjectList;
}
}
class MyListedObj {
private String theText;
public MyListedObj(String aText) {
this.theText = aText;
}
}
以下是运行上述示例代码的输出:
Diff:
1. ObjectRemoved{globalId:'org.example.TestJavers$MyMainObj/#theObjectList/1'}
2. ListChange{globalId:'org.example.TestJavers$MyMainObj/', property:'theObjectList', containerChanges:[(1).removed:'[email protected]']}
java.lang.AssertionError:
Expected :1
Actual :2