2017-02-13 34 views
1

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

回答

0

在JaVers,ObjectRemoved通知您的對象從左側的對象圖消失。而ListChange->ValueRemovedPropertyChange,並通知有關發生更改的對象圖中的具體位置。

這兩個更改涉及相同的對象(刪除一個),但不重複。

例如:

Class A { 
    List<B> listOfB 
    B bRef 
} 

如果你比較:

def b1 = new B() 
javers.compare(new A(listOfB:[b1], bRef:b1), new A(listOfB:[], bRef:null)) 

您將有三個變化:

  • B1 ObjectRemoved(一般信息)
  • B1 ListChange->ValueRemovedlistOfB屬性(對象圖中的具體位置)
  • ValueChanged從B1到零上bRef屬性(對象圖中的另一個特定的地方)

我可以建議你簡直是無視ObjectRemoved變化和依靠僅在PropertyChangesListChangeValueChange ...)