Search in sources :

Example 1 with ObjectIntProcedure

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));
}
Also used : BigInteger(java.math.BigInteger) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) Test(org.junit.Test) Verify(org.eclipse.collections.impl.test.Verify) Iterate(org.eclipse.collections.impl.utility.Iterate) MutableList(org.eclipse.collections.api.list.MutableList) Executors(java.util.concurrent.Executors) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableSet(org.eclipse.collections.api.set.MutableSet) List(java.util.List) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) Lists(org.eclipse.collections.impl.factory.Lists) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) AddFunction(org.eclipse.collections.impl.block.function.AddFunction) BigInteger(java.math.BigInteger) NoSuchElementException(java.util.NoSuchElementException) Assert(org.junit.Assert) ExecutorService(java.util.concurrent.ExecutorService) Predicates(org.eclipse.collections.impl.block.factory.Predicates) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Example 2 with ObjectIntProcedure

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();
    }));
}
Also used : Counter(org.eclipse.collections.impl.Counter) Iterator(java.util.Iterator) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) EmptyIterator(org.eclipse.collections.impl.EmptyIterator) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Optional(java.util.Optional) Procedure(org.eclipse.collections.api.block.procedure.Procedure) NoSuchElementException(java.util.NoSuchElementException) Iterate(org.eclipse.collections.impl.utility.Iterate) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) Counter(org.eclipse.collections.impl.Counter)

Example 3 with ObjectIntProcedure

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());
}
Also used : LazyIterable(org.eclipse.collections.api.LazyIterable) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) SumProcedure(org.eclipse.collections.impl.math.SumProcedure) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test) Sum(org.eclipse.collections.impl.math.Sum) Assert(org.junit.Assert) FastList(org.eclipse.collections.impl.list.mutable.FastList) Before(org.junit.Before) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) Test(org.junit.Test)

Example 4 with ObjectIntProcedure

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());
}
Also used : SumProcedure(org.eclipse.collections.impl.math.SumProcedure) Test(org.junit.Test) Sum(org.eclipse.collections.impl.math.Sum) FastList(org.eclipse.collections.impl.list.mutable.FastList) LazyIterable(org.eclipse.collections.api.LazyIterable) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Interval(org.eclipse.collections.impl.list.Interval) Assert(org.junit.Assert) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Aggregations

ObjectIntProcedure (org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure)4 FastList (org.eclipse.collections.impl.list.mutable.FastList)4 LazyIterable (org.eclipse.collections.api.LazyIterable)3 Procedure2 (org.eclipse.collections.api.block.procedure.Procedure2)3 IntegerSum (org.eclipse.collections.impl.math.IntegerSum)3 Assert (org.junit.Assert)3 Test (org.junit.Test)3 Iterator (java.util.Iterator)2 NoSuchElementException (java.util.NoSuchElementException)2 MutableList (org.eclipse.collections.api.list.MutableList)2 Predicates (org.eclipse.collections.impl.block.factory.Predicates)2 Interval (org.eclipse.collections.impl.list.Interval)2 Sum (org.eclipse.collections.impl.math.Sum)2 SumProcedure (org.eclipse.collections.impl.math.SumProcedure)2 Iterate (org.eclipse.collections.impl.utility.Iterate)2 LazyIterate (org.eclipse.collections.impl.utility.LazyIterate)2 Before (org.junit.Before)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1