我使用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