use of org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure in project eclipse-collections by eclipse.
the class IntervalTest method forEachWithIndex.
@Test
public void forEachWithIndex() {
IntegerSum sum = new IntegerSum(0);
Interval.oneTo(5).forEachWithIndex((ObjectIntProcedure<Integer>) (each, index) -> sum.add(each + index));
Assert.assertEquals(25, sum.getIntSum());
IntegerSum zeroSum = new IntegerSum(0);
Interval.fromTo(0, -4).forEachWithIndex((ObjectIntProcedure<Integer>) (each, index) -> zeroSum.add(each + index));
Assert.assertEquals(0, zeroSum.getIntSum());
Verify.assertThrows(IndexOutOfBoundsException.class, () -> Interval.zeroTo(10).forEachWithIndex(null, -1, 10));
}
use of org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure in project eclipse-collections by eclipse.
the class CompositeIterable method forEachWithIndex.
@Override
public void forEachWithIndex(ObjectIntProcedure<? super E> objectIntProcedure) {
Counter index = new Counter();
this.iterables.each(iterable -> Iterate.forEach(iterable, object -> {
objectIntProcedure.value(object, index.getCount());
index.increment();
}));
}
use of org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure in project eclipse-collections by eclipse.
the class DropIterableTest method forEachWithIndex.
@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());
}
use of org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure in project eclipse-collections by eclipse.
the class DropWhileIterableTest method forEachWithIndex.
@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.dropWhileIterable.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.emptyListDropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
Assert.assertEquals(0, indices.size());
indices.clear();
sum.add(sum.getValue().intValue() * -1);
this.alwaysFalseDropWhileIterable.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.mostlyFalseDropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
Assert.assertEquals(FastList.newListWith(0), indices);
Assert.assertEquals(5, sum.getValue().intValue());
indices.clear();
sum.add(sum.getValue().intValue() * -1);
this.alwaysTrueDropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
Assert.assertEquals(0, indices.size());
}
Aggregations