• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

裹集合对象

javers 来源:Alina Bolindu 4次浏览

我有以下实体:裹集合对象

public class ComplexEntity { 
    public List<TenderLocation> tenderList; 

    public ComplexEntity(List<TenderLocation> tenderList) { 
     this.tenderList = tenderList; 
    } 
} 


public class TenderLocation { 
    public String location; 

    public List<TenderAirline> tenderAirlines; 

    public TenderLocation(String location, List<TenderAirline> tenderAirlines) { 
     this.tenderAirlines = tenderAirlines; 
     this.location = location; 
    } 
} 


public class TenderAirline { 
    public int ID; 
    public String name; 

    public TenderAirline(int ID, String name) { 
      this.ID = ID; 
      this.name = name; 
    } 
} 

而下面的测试用于比较两个ComplexEntiey

public class ComplexObjectGraphComparisonExample { 

    @Test 
     public void shouldCompareTwoComplexObjects() { 

      // given 
      Javers javers = JaversBuilder.javers().build(); 

      // Construct test data 
      // ComplexEntity: 
      // - List<TLocation> 
      // TLoation: 
      //  - location: String 
      //  - List<TAir> 
      //  TAir: 
      //   - int ID 
      //   - String Name 

      int locations = 3; 
      List<TenderLocation> tenderLocationsBase = new ArrayList<TenderLocation>(locations); 
      List<TenderLocation> tenderLocationsRef = new ArrayList<TenderLocation>(locations); 
      for (int j = 0; j < locations; ++j) { 
       int airlines = 10; 
       List<TenderAirline> tenderAirlinesBase = new ArrayList<TenderAirline>(airlines); 
       List<TenderAirline> tenderAirlinesRef = new ArrayList<TenderAirline>(airlines); 

       for (int i = 0; i < airlines; ++i) { 
        tenderAirlinesBase.add(new TenderAirline(i, "Airline" + i)); 
        tenderAirlinesRef.add(new TenderAirline(i, "Airline" + i)); 
       } 

       tenderLocationsBase.add(new TenderLocation("BV" + j, tenderAirlinesBase)); 
       tenderLocationsRef.add(new TenderLocation("BV" + j, tenderAirlinesBase)); 
      } 

      ComplexEntity baseEntity = new ComplexEntity(tenderLocationsBase); 
      ComplexEntity referenceEntity = new ComplexEntity(tenderLocationsRef); 

      // when 
      Diff diff = javers.compare(baseEntity, referenceEntity); 

      assertThat(diff.getChanges()).hasSize(0); 

      // Change a single small thing 
      referenceEntity.tenderList.get(1).location = "Difference_1"; 

      // then there is a single change detected 
      diff = javers.compare(baseEntity, referenceEntity); 
      assertThat(diff.getChanges()).hasSize(1); 

      // there should be one change of type {@link ValueChange} 
      ValueChange change = diff.getChangesByType(ValueChange.class).get(0); 

      assertThat(change.getPropertyName()).isEqualTo("location"); 
      assertThat(change.getLeft()).isEqualTo("BV1"); 
      assertThat(change.getRight()).isEqualTo("Difference_1"); 

      // do another change 
      referenceEntity.tenderList.get(1).tenderAirlines.get(1).name = "Difference_2"; 

      // second difference is not detected, failing the commented test 
      diff = javers.compare(baseEntity, referenceEntity); 
      assertThat(diff.getChanges()).hasSize(2); 

      System.out.println(diff); 
     } 
} 

在比较我的第二个变化是没有确定,因为compare方法没有深入比较我的列表。

我已经在这里

http://www.atetric.com/atetric/javadoc/org.javers/javers-core/1.3.4/org/javers/core/Javers.html

阅读,如果我"wrap collections in some Value Objects"深比较集合是可能的。

我的问题是,我怎样才能将我的收藏包裹到Value Objects

===========解决方案如下:

你可以用像下面的对象的东西:

public class Wrapper 
    { 
     private final WrappedObject obj; 

     public Wrapper (WrappedObject obj) 
     { 
     this.obj = obj; 
     } 
    } 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)