コード例 #1
0
  @Test
  public void forEachWithIndex() {
    Sum sum = new IntegerSum(0);
    FastList<Integer> indices = FastList.newList(5);
    ObjectIntProcedure<Integer> indexRecordingAndSumProcedure =
        (each, index) -> {
          indices.add(index);
          sum.add(each);
        };

    this.dropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0, 1, 2), indices);
    Assert.assertEquals(12, sum.getValue().intValue());

    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.emptyListDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());

    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.zeroCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0, 1, 2, 3, 4), indices);
    Assert.assertEquals(15, sum.getValue().intValue());

    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.nearCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0), indices);
    Assert.assertEquals(5, sum.getValue().intValue());

    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.sameCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());

    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.higherCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());
  }