コード例 #1
0
 /** @deprecated since 3.0. */
 @Deprecated
 @Test
 public void lazySelectForEach() {
   UnifiedSetWithHashingStrategy<Integer> integers =
       UnifiedSetWithHashingStrategy.newSetWith(INTEGER_HASHING_STRATEGY, 1, 2, 3, 4, 5);
   LazyIterable<Integer> select = integers.lazySelect(Predicates.lessThan(5));
   Sum sum = new IntegerSum(0);
   select.forEach(new SumProcedure<Integer>(sum));
   Assert.assertEquals(10, sum.getValue().intValue());
 }
コード例 #2
0
 @Test
 public void injectIntoWithOver100() {
   Sum result = new IntegerSum(0);
   Integer parameter = 2;
   Function3<Sum, Integer, Integer, Sum> function =
       new Function3<Sum, Integer, Integer, Sum>() {
         public Sum value(Sum sum, Integer element, Integer withValue) {
           return sum.add((element.intValue() - element.intValue()) * withValue.intValue());
         }
       };
   Sum sumOfDoubledValues =
       ArrayListIterate.injectIntoWith(result, this.getOver100IntegerList(), function, parameter);
   Assert.assertEquals(0, sumOfDoubledValues.getValue().intValue());
 }
コード例 #3
0
  @Test
  public void batchForEach() {
    // Testing batch size of 1 to 16 with no chains
    UnifiedSet<Integer> set = UnifiedSet.<Integer>newSet(10).with(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    for (int sectionCount = 1; sectionCount <= 16; ++sectionCount) {
      Sum sum = new IntegerSum(0);
      for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex) {
        set.batchForEach(new SumProcedure<Integer>(sum), sectionIndex, sectionCount);
      }
      Assert.assertEquals(55, sum.getValue());
    }

    // Testing 1 batch with chains
    Sum sum2 = new IntegerSum(0);
    UnifiedSetWithHashingStrategy<Integer> set2 =
        UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 3)
            .with(COLLISION_1, COLLISION_2, COLLISION_3, 1, 2);
    int numBatches = set2.getBatchCount(100);
    for (int i = 0; i < numBatches; ++i) {
      set2.batchForEach(new SumProcedure<Integer>(sum2), i, numBatches);
    }
    Assert.assertEquals(1, numBatches);
    Assert.assertEquals(54, sum2.getValue());

    // Testing batch size of 3 with chains and uneven last batch
    Sum sum3 = new IntegerSum(0);
    UnifiedSetWithHashingStrategy<Integer> set3 =
        UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 4, 1.0F)
            .with(COLLISION_1, COLLISION_2, 1, 2, 3, 4, 5);
    int numBatches2 = set3.getBatchCount(3);
    for (int i = 0; i < numBatches2; ++i) {
      set3.batchForEach(new SumProcedure<Integer>(sum3), i, numBatches2);
    }
    Assert.assertEquals(32, sum3.getValue());

    // Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
    Sum sum4 = new IntegerSum(0);
    UnifiedSetWithHashingStrategy<Integer> set4 =
        UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY);
    set4.batchForEach(new SumProcedure<Integer>(sum4), 0, set4.getBatchCount(1));
    Assert.assertEquals(0, sum4.getValue());
  }
コード例 #4
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());
  }
コード例 #5
0
  @Test
  public void forEach() {
    Sum sum1 = new IntegerSum(0);
    this.dropIterable.forEach(new SumProcedure<>(sum1));
    Assert.assertEquals(12, sum1.getValue().intValue());

    Sum sum2 = new IntegerSum(0);
    this.emptyListDropIterable.forEach(new SumProcedure<>(sum2));
    Assert.assertEquals(0, sum2.getValue().intValue());

    Sum sum3 = new IntegerSum(0);
    this.zeroCountDropIterable.forEach(new SumProcedure<>(sum3));
    Assert.assertEquals(15, sum3.getValue().intValue());

    Sum sum5 = new IntegerSum(0);
    this.nearCountDropIterable.forEach(new SumProcedure<>(sum5));
    Assert.assertEquals(5, sum5.getValue().intValue());

    Sum sum6 = new IntegerSum(0);
    this.sameCountDropIterable.forEach(new SumProcedure<>(sum6));
    Assert.assertEquals(0, sum6.getValue().intValue());

    Sum sum7 = new IntegerSum(0);
    this.higherCountDropIterable.forEach(new SumProcedure<>(sum7));
    Assert.assertEquals(0, sum7.getValue().intValue());
  }
コード例 #6
0
  @Override
  @Test
  public void iterator() {
    Sum sum1 = new IntegerSum(0);
    for (Integer each : this.dropIterable) {
      sum1.add(each);
    }
    Assert.assertEquals(12, sum1.getValue().intValue());

    Sum sum2 = new IntegerSum(0);
    for (Integer each : this.emptyListDropIterable) {
      sum2.add(each);
    }
    Assert.assertEquals(0, sum2.getValue().intValue());

    Sum sum3 = new IntegerSum(0);
    for (Integer each : this.zeroCountDropIterable) {
      sum3.add(each);
    }
    Assert.assertEquals(15, sum3.getValue().intValue());

    Sum sum5 = new IntegerSum(0);
    for (Integer each : this.nearCountDropIterable) {
      sum5.add(each);
    }
    Assert.assertEquals(5, sum5.getValue().intValue());

    Sum sum6 = new IntegerSum(0);
    for (Integer each : this.sameCountDropIterable) {
      sum6.add(each);
    }
    Assert.assertEquals(0, sum6.getValue().intValue());

    Sum sum7 = new IntegerSum(0);
    for (Integer each : this.higherCountDropIterable) {
      sum7.add(each);
    }
    Assert.assertEquals(0, sum7.getValue().intValue());
  }
コード例 #7
0
  @Test
  public void forEachWith() {
    Procedure2<Integer, Sum> sumAdditionProcedure = (each, sum) -> sum.add(each);

    Sum sum1 = new IntegerSum(0);
    this.dropIterable.forEachWith(sumAdditionProcedure, sum1);
    Assert.assertEquals(12, sum1.getValue().intValue());

    Sum sum2 = new IntegerSum(0);
    this.emptyListDropIterable.forEachWith(sumAdditionProcedure, sum2);
    Assert.assertEquals(0, sum2.getValue().intValue());

    Sum sum3 = new IntegerSum(0);
    this.zeroCountDropIterable.forEachWith(sumAdditionProcedure, sum3);
    Assert.assertEquals(15, sum3.getValue().intValue());

    Sum sum5 = new IntegerSum(0);
    this.nearCountDropIterable.forEachWith(sumAdditionProcedure, sum5);
    Assert.assertEquals(5, sum5.getValue().intValue());

    Sum sum6 = new IntegerSum(0);
    this.sameCountDropIterable.forEachWith(sumAdditionProcedure, sum6);
    Assert.assertEquals(0, sum6.getValue().intValue());

    Sum sum7 = new IntegerSum(0);
    this.higherCountDropIterable.forEachWith(sumAdditionProcedure, sum7);
    Assert.assertEquals(0, sum7.getValue().intValue());
  }