Search in sources :

Example 6 with LongConsumer

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

the class LongPrimitiveOpsTests method testTee.

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

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

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

Example 7 with LongConsumer

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

the class SplittableRandomTest method testUnsizedLongsCountSeq.

/**
 * A sequential unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCountSeq() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).forEach(new LongConsumer() {

        @Override
        public void accept(long x) {
            counter.increment();
        }
    });
    assertEquals(counter.sum(), size);
}
Also used : LongConsumer(java8.util.function.LongConsumer) LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Example 8 with LongConsumer

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

the class LongPrimitiveOpsTests method testParForEach.

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

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

        @Override
        public void accept(long l) {
            ai.addAndGet(l);
        }
    });
    assertEquals(ai.get(), 20);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) LongConsumer(java8.util.function.LongConsumer) LongPredicate(java8.util.function.LongPredicate) Test(org.testng.annotations.Test)

Example 9 with LongConsumer

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

the class LongPrimitiveOpsTests method testForEach.

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

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

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

Aggregations

LongConsumer (java8.util.function.LongConsumer)9 SplittableRandom (java8.util.SplittableRandom)4 LongAdder (java8.util.concurrent.atomic.LongAdder)3 LongPredicate (java8.util.function.LongPredicate)3 Test (org.testng.annotations.Test)3 NoSuchElementException (java.util.NoSuchElementException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Spliterator (java8.util.Spliterator)1 ObjLongConsumer (java8.util.function.ObjLongConsumer)1