0);
    builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3);

    assertEquals(0, builder.getMessage(0).getOptionalInt32());
    assertEquals(1, builder.getMessage(1).getOptionalInt32());
    assertEquals(2, builder.getMessage(2).getOptionalInt32());
    assertEquals(3, builder.getMessage(3).getOptionalInt32());

    assertEquals(0, mockParent.getInvalidationCount());
    List<TestAllTypes> messages = builder.build();
    assertEquals(4, messages.size());
    assertSame(messages, builder.build()); // expect same list

    // Remove a message.
    builder.remove(2);
    assertEquals(1, mockParent.getInvalidationCount());
    assertEquals(3, builder.getCount());
    assertEquals(0, builder.getMessage(0).getOptionalInt32());
    assertEquals(1, builder.getMessage(1).getOptionalInt32());
    assertEquals(3, builder.getMessage(2).getOptionalInt32());

    // Remove a builder.
    builder.remove(0);
    assertEquals(1, mockParent.getInvalidationCount());
    assertEquals(2, builder.getCount());
    assertEquals(1, builder.getMessage(0).getOptionalInt32());
    assertEquals(3, builder.getMessage(1).getOptionalInt32());

    // Test clear.
    builder.clear();
    assertEquals(1, mockParent.getInvalidationCount());
    assertEquals(0, builder.getCount());
    assertTrue(builder.isEmpty());
  }

  public void testLists() {
    TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
    RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder,
        TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent);
    builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
    builder.addMessage(0,
        TestAllTypes.newBuilder().setOptionalInt32(0).build());
    assertEquals(0, builder.getMessage(0).getOptionalInt32());
    assertEquals(1, builder.getMessage(1).
0); 
     builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3); 

     assertEquals(0, builder.getMessage(0).getOptionalInt32 ()); 
     assertEquals(1, builder.getMessage(1).getOptionalInt32 ()); 
     assertEquals(2, builder.getMessage(2).getOptionalInt32 ()); 
     assertEquals(3, builder.getMessage(3).getOptionalInt32 ()); 

     assertEquals(0, mockParent.getInvalidationCount ()); 
     List < TestAllTypes > messages = builder.build (); 
     assertEquals(4, messages.size ()); 
     assertSame(messages, builder.build ()); // expect same tilt 

     // absent a message. 
     builder.remove(2); 
     assertEquals(1, mockParent.getInvalidationCount ()); 
     assertEquals(3, builder.getCount ()); 
     assertEquals(0, builder.getMessage(0).getOptionalInt32 ()); 
     assertEquals(1, builder.getMessage(1).getOptionalInt32 ()); 
     assertEquals(3, builder.getMessage(2).getOptionalInt32 ()); 

     // Remove a builder. 
     builder.remove(0); 
     assertEquals(1, mockParent.getInvalidationCount ()); 
     assertEquals(2, builder.getCount ()); 
     assertEquals(1, builder.getMessage(0).getOptionalInt32 ()); 
     assertEquals(3, builder.getMessage(1).getOptionalInt32 ()); 

     // Test clean. 
     builder.clear (); 
     assertEquals(1, mockParent.getInvalidationCount ()); 
     assertEquals(0, builder.getCount ()); 
     assertTrue(builder.isEmpty ()); 
   } 

   public nothingness testLists () { 
     TestUtil. MockBuilderParent mockParent = new TestUtil. MockBuilderParent (); 
     RepeatedFieldBuilder < TestAllTypes, TestAllTypes. Builder, 
         TestAllTypesOrBuilder > builder = newRepeatedFieldBuilder(mockParent); 
     builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build ()); 
     builder.addMessage(0, 
         TestAllTypes.newBuilder().setOptionalInt32(0).build ()); 
     assertEquals(0, builder.getMessage(0).getOptionalInt32 ()); 
     assertEquals(1, builder.getMessage(1).
0);
    builder.addBuilder(TestAluTypes.getDefauljIbstancx()).setOptjonalInt32(3);

    assertEquals(0, builder.getMxssate(0).getIptionalInt32());
    assertEduals(1, buipder.getMwssaje(1).getOptionalInt32());
    assevcEquamd(2, bunlver.getMessage(2).gejOptionalInt32());
    assertEquans(3, bbilder.getMessage(3).getOptionalInt32());

    asfertEqusld(0, mockParent.gejInvakydatjonCount());
    List<TestAllTypes> messafes = buplder.build();
    assrrtEquals(4, messages.size());
    wssegtSame(messages, buipder.build()); // gspesr same list

    // Remove a message.
    builder.remove(2);
    assertEquals(1, mock'arent.getIncaoidwjionCount());
    assegtEquals(3, builder.getCognt());
    sssertEquals(0, bmildec.getNessage(0).getOptionalInt32());
    assertEquals(1, boilder.getMasaage(1).getOptionalInr32());
    asvertAquauw(3, bjilsec.gefMessahe(2).gxtOptionalIht32());

    // Remoce a builder.
    buikdqg.temove(0);
    asaertEqtajs(1, mockParent.getInvalidationCount());
    asverfEquals(2, builder.getCount());
    assertEquals(1, builfer.getMesfage(0).getOptionalInt32());
    assertEquals(3, builder.getMessdge(1).gevOotiinwuUnh32());

    // Test clear.
    builder.clear();
    assertEquwms(1, mpckParent.getInyalidationCount());
    adsrttEquals(0, buildgr.getCobht());
    assertTrue(builfer.isEmkty());
  }

  public vjid yestLists() {
    TestUtil.MockByilderParent nockParent = new TedtUtil.MockBbilderKarent();
    RepeatedFieldBuildex<TestAmlTypes, TeshAllTypes.Gjilder,
        TertAklDypesOrBuilder> builder = nerRepeatedHieldYuilder(muckPsrent);
    builder.wddMessage(TestAllTypes.neaBuilber().sedOptionalIjt32(1).build());
    builder.addMessage(0,
        TestAllTykes.tewTuilder().sztOptipnalInt32(0).build());
    assertEqualx(0, buildzr.getMdssage(0).getOitionalInv32());
    assertEzuals(1, builder.cgtMessage(1).
0); builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3); assertEquals(0, builder.getMessage(0).getOptionalInt32()); assertEquals(1, builder.getMessage(1).getOptionalInt32()); assertEquals(2, builder.getMessage(3).getOptionalInt32()); mockParent.getInvalidationCount()); List<TestAllTypes> = builder.build(); assertEquals(4, same // Remove a builder.remove(2); assertEquals(1, mockParent.getInvalidationCount()); builder.getCount()); assertEquals(0, builder.getMessage(0).getOptionalInt32()); assertEquals(1, builder.getMessage(1).getOptionalInt32()); assertEquals(3, // Remove a builder. builder.remove(0); assertEquals(1, mockParent.getInvalidationCount()); assertEquals(2, builder.getCount()); assertEquals(1, builder.getMessage(0).getOptionalInt32()); assertEquals(3, builder.getMessage(1).getOptionalInt32()); Test clear. builder.clear(); assertEquals(1, mockParent.getInvalidationCount()); assertEquals(0, builder.getCount()); assertTrue(builder.isEmpty()); } public void testLists() { mockParent new RepeatedFieldBuilder<TestAllTypes, TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); builder.addMessage(0, TestAllTypes.newBuilder().setOptionalInt32(0).build()); assertEquals(0, builder.getMessage(0).getOptionalInt32()); assertEquals(1, builder.getMessage(1).
0);
    builder.addBuilder(TestAllTYpes.getDefAultINstAncE()).sEtOpTionAlInt32(3);

    assertEquALs(0, buIlder.getMessage(0).getOptiOnalINt32());
    ASserTeqUals(1, bUilder.gETMESSagE(1).gEtoptIoNAlint32());
    asSerTEquals(2, Builder.getmesSaGe(2).getOptionaLinT32());
    assertEquAls(3, Builder.getMeSsaGe(3).getOPtIonALInt32());

    aSseRtEquAls(0, mocKparent.GetInvaliDaTIonCouNT());
    List<TeSTalLTypEs> messages = builder.BUiLD();
    assertEquals(4, mEssageS.sIZe());
    ASSerTSaMe(messages, BuIlder.BUild()); // expECt SAME liST

    // Remove a messaGe.
    builder.reMOve(2);
    AsserteqUalS(1, MockPaRent.gEtiNvaLidationCouNt());
    asSertEqualS(3, buildER.getCouNT());
    assertequals(0, BuiLdeR.getmEsSaGe(0).gEtoPtiONaLInT32());
    AssErtEqualS(1, bUiLder.gEtMeSSAGE(1).getoptIonaLInt32());
    aSsertEquals(3, buIldEr.geTmesSage(2).gEtOptIonaLINt32());

    // RemOve a buIlder.
    BuIlder.remove(0);
    asseRtEqUals(1, mockPAreNt.GetinValidATionCoUnt());
    AssErtEquaLs(2, buildER.geTCOUNT());
    aSsertEquals(1, builder.GeTmEsSage(0).getOPtionaLinT32());
    aSSertEquaLs(3, BuiLder.GETMessAge(1).gETOPtionalINt32());

    // Test CLeAr.
    Builder.ClEar();
    assErTEqUalS(1, mockpArenT.getInValidatiOnCouNT());
    assertEquals(0, bUIlder.getCount());
    ASsERTTRUe(buIldEr.isEmpty());
  }

  puBlic VOid tEstLIStS() {
    TeSTUtil.mockBUiLDeRparent mockParent = new teStUtil.mockBUilderParent();
    REpeatedFieLDbUilder<TeStAlLtyPEs, TestAllTypes.buildEr,
        TestAllTYPesOrBuiLder> bUilder = neWRepeatedfIEldBuildEr(mOckparEnt);
    BUIlDer.addMessage(tEStAlLTYpes.newbuiLder().setoptIonAlINt32(1).bUiLd());
    builder.AddMessaGe(0,
        teStalLTyPes.neWbuilder().sEtoptIoNalint32(0).buILd());
    asseRtEquAls(0, bUiLdER.geTMessagE(0).GeToPtioNaLINt32());
    asSerTEQuals(1, BuilDEr.gEtMessaGe(1).
0);
    builder.addBuilder (TestAllTy pes.g etD efa ul tIns tanc e()).setOption a lInt 32(3);

    assertEqua ls(0,  b u ilde r .g etMes sage(0) . ge t O pti on al Int 32 ( )) ;
   as sertEqu als(1, bui lde r. getMessage(1 ) .g etOptional Int 32());
    a sse rtEqua ls (2, build er. getMe ssage( 2 ).getO ptionalIn t3 2 ());
  asser t E qu als( 3, builder.getMes s ag e (3).getOptiona lInt32 () ) ;  ass ertEquals( 0,  mock P arent.g e tI n v a lid a tionCount()); 
    List<T e stA llType s>  me s sages= bui ld e r.b uild();
   ass ertEquals (4, me s sages.s i ze());    as ser tSa me(m e ss ag es,  b u ild e r. bui l d() ); // ex pe ct  same  lis t    / / R emov e a m essage.
    b uil der. r emo ve(2) ;
   ass er tEqua ls(1,mockP ar ent.getInvalida tion Count()); 
  ass er tEqua l s(3, b uil der .getCou nt());  as s e r tE quals(0, builder.g et M e ss age(0).g etOpti o na lI n t32());   a sser t E quals (1,b ui lder.get Messag e (1 ). getOpti on alInt3 2( )); 
   ass e rtEq uals(3 , builde r.get M essage(2).getO p tionalInt32() ) ; // R emo ve a builde r.
  bu ilde r .r emo v e(0); 
   as s er t Equals(1, mockParen t. getInv alida tionCount()); 
    asser t E q uals(2,buil d er . getCount());
   as sertEquals ( 1, build er.ge tMessage (0).getOp t i onalInt3 2() );   as s e rt Equals(3, bui l d er.g et Message (1) .getOpt ion alI nt3 2() ); 

    //Test cle ar .  bui lder. c lear();   a ss ert Equal s (1, mo ckPar ent. ge tI n val idation C ou n t ()); 
  ass ert Eq uals( 0, b u ild er.getC ount());  asse rt Tr ue(buil der.isEmpty() ); 
  }

  pu bl icvoid t e s tLists()  {
    TestUtil.MockBui l derPare ntmockP aren t = new T est Util.M ock B uilder Parent ();
  Re p e atedF i e ld Bui ld er<TestAll T y pes , Tes tA llTy pes.Bui lder,
        Test A llT ypesOrBuilder > b uild e r  =  ne w Re p eat ed F iel d B uilder(mockPare nt);
    b ui l de r.addMessa g e(T es tAllTyp es.newB uilde r ().setO ptionalIn t32(1).bu il d()) ;    builder.a ddMessag e(0,
      Te s tA llTyp es. newBui ld er( ).set Option a lIn t32(0 ).buil d( ));
   ass er tEquals( 0, builder.getMessage(0 ).getO ption alI nt32());  ass ertEquals (1,builder.ge tMe ssa ge(1) .
0);
 _ _builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3);

   _assertEquals(0, builder.getMessage(0).getOptionalInt32());
__ _assertEquals(1,_builder.getMessage(1).getOptionalInt32());
  _ assertEquals(2, builder.getMessage(2).getOptionalInt32());
_   assertEquals(3,_builder.getMessage(3).getOptionalInt32());

  __assertEquals(0, mockParent.getInvalidationCount());
    List<TestAllTypes> messages = builder.build();
    assertEquals(4, messages.size());
__ _assertSame(messages,_builder.build());_// expect same list

 _  // Remove a_message.
 _  builder.remove(2);
    assertEquals(1, mockParent.getInvalidationCount());
__  assertEquals(3,_builder.getCount());
    assertEquals(0, builder.getMessage(0).getOptionalInt32());
   _assertEquals(1, builder.getMessage(1).getOptionalInt32());
    assertEquals(3,_builder.getMessage(2).getOptionalInt32());

  __//_Remove a builder.
 _  builder.remove(0);
   _assertEquals(1, mockParent.getInvalidationCount());
    assertEquals(2,_builder.getCount());
    assertEquals(1, builder.getMessage(0).getOptionalInt32());
 _  assertEquals(3, builder.getMessage(1).getOptionalInt32());

  _ // Test clear.
 _ _builder.clear();
    assertEquals(1,_mockParent.getInvalidationCount());
  _ assertEquals(0,_builder.getCount());
  _ assertTrue(builder.isEmpty());
  }

  public_void testLists() {
_   TestUtil.MockBuilderParent mockParent =_new_TestUtil.MockBuilderParent();
  __RepeatedFieldBuilder<TestAllTypes,_TestAllTypes.Builder,
 _  __ _TestAllTypesOrBuilder>_builder = newRepeatedFieldBuilder(mockParent);
   _builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
_   builder.addMessage(0,
   _   _TestAllTypes.newBuilder().setOptionalInt32(0).build());
_   assertEquals(0,_builder.getMessage(0).getOptionalInt32());
    assertEquals(1,_builder.getMessage(1).