Search in sources :

Example 1 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class IntPrimitiveOpsTests method testForEach.

@Test(groups = { "serialization-hostile" })
public void testForEach() {
    final int[] sum = new int[1];
    IntStreams.range(1, 10).filter(new IntPredicate() {

        @Override
        public boolean test(int i) {
            return i % 2 == 0;
        }
    }).forEach(new IntConsumer() {

        @Override
        public void accept(int i) {
            sum[0] = sum[0] + i;
        }
    });
    assertEquals(sum[0], 20);
}
Also used : IntPredicate(java8.util.function.IntPredicate) IntConsumer(java8.util.function.IntConsumer) Test(org.testng.annotations.Test)

Example 2 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class IntPrimitiveOpsTests method testParForEach.

@Test(groups = { "serialization-hostile" })
public void testParForEach() {
    final AtomicInteger ai = new AtomicInteger(0);
    IntStreams.range(1, 10).parallel().filter(new IntPredicate() {

        @Override
        public boolean test(int i) {
            return i % 2 == 0;
        }
    }).forEach((IntConsumer) new IntConsumer() {

        @Override
        public void accept(int i) {
            ai.addAndGet(i);
        }
    });
    assertEquals(ai.get(), 20);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntPredicate(java8.util.function.IntPredicate) IntConsumer(java8.util.function.IntConsumer) Test(org.testng.annotations.Test)

Example 3 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class IntPrimitiveOpsTests method testTee.

@Test(groups = { "serialization-hostile" })
public void testTee() {
    final int[] teeSum = new int[1];
    long sum;
    sum = IntStreams.range(1, 10).filter(new IntPredicate() {

        @Override
        public boolean test(int i) {
            return i % 2 == 0;
        }
    }).peek(new IntConsumer() {

        @Override
        public void accept(int i) {
            teeSum[0] = teeSum[0] + i;
        }
    }).sum();
    assertEquals(teeSum[0], sum);
}
Also used : IntPredicate(java8.util.function.IntPredicate) IntConsumer(java8.util.function.IntConsumer) Test(org.testng.annotations.Test)

Example 4 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class Spliterators method iterator.

/**
 * Creates an {@code PrimitiveIterator.OfInt} from a
 * {@code Spliterator.OfInt}.
 *
 * <p>Traversal of elements should be accomplished through the iterator.
 * The behaviour of traversal is undefined if the spliterator is operated
 * after the iterator is returned.
 *
 * @param spliterator The spliterator
 * @return An iterator
 * @throws NullPointerException if the given spliterator is {@code null}
 */
public static PrimitiveIterator.OfInt iterator(Spliterator.OfInt spliterator) {
    Objects.requireNonNull(spliterator);
    class Adapter implements PrimitiveIterator.OfInt, IntConsumer {

        boolean valueReady = false;

        int nextElement;

        @Override
        public void accept(int t) {
            valueReady = true;
            nextElement = t;
        }

        @Override
        public boolean hasNext() {
            if (!valueReady)
                spliterator.tryAdvance(this);
            return valueReady;
        }

        @Override
        public int nextInt() {
            if (!valueReady && !hasNext())
                throw new NoSuchElementException();
            else {
                valueReady = false;
                return nextElement;
            }
        }

        @Override
        public Integer next() {
            return nextInt();
        }

        @Override
        public void forEachRemaining(IntConsumer action) {
            Iterators.forEachRemaining(this, action);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove");
        }
    }
    return new Adapter();
}
Also used : NoSuchElementException(java.util.NoSuchElementException) IntConsumer(java8.util.function.IntConsumer)

Example 5 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class IntPipeline method forEachWithCancel.

@Override
final boolean forEachWithCancel(Spliterator<Integer> spliterator, Sink<Integer> sink) {
    Spliterator.OfInt spl = adapt(spliterator);
    IntConsumer adaptedSink = adapt(sink);
    boolean cancelled;
    do {
    } while (!(cancelled = sink.cancellationRequested()) && spl.tryAdvance(adaptedSink));
    return cancelled;
}
Also used : Spliterator(java8.util.Spliterator) IntConsumer(java8.util.function.IntConsumer) ObjIntConsumer(java8.util.function.ObjIntConsumer)

Aggregations

IntConsumer (java8.util.function.IntConsumer)9 SplittableRandom (java8.util.SplittableRandom)4 LongAdder (java8.util.concurrent.atomic.LongAdder)3 IntPredicate (java8.util.function.IntPredicate)3 Test (org.testng.annotations.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NoSuchElementException (java.util.NoSuchElementException)1 Spliterator (java8.util.Spliterator)1 ObjIntConsumer (java8.util.function.ObjIntConsumer)1