Search in sources :

Example 1 with DoubleConsumer

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

the class Spliterators method iterator.

/**
 * Creates an {@code PrimitiveIterator.OfDouble} from a
 * {@code Spliterator.OfDouble}.
 *
 * <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.OfDouble iterator(Spliterator.OfDouble spliterator) {
    Objects.requireNonNull(spliterator);
    class Adapter implements PrimitiveIterator.OfDouble, DoubleConsumer {

        boolean valueReady = false;

        double nextElement;

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

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

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

        @Override
        public Double next() {
            return nextDouble();
        }

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

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

Example 2 with DoubleConsumer

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

the class SplittableRandomTest method testDoublesCount.

/**
 * A parallel sized stream of doubles generates the given number of values
 */
public void testDoublesCount() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.doubles(size).parallel().forEach(new DoubleConsumer() {

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

Example 3 with DoubleConsumer

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

the class SplittableRandomTest method testBoundedDoubles.

/**
 * Each of a parallel sized stream of bounded doubles is within bounds
 */
public void testBoundedDoubles() {
    final AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    long size = 456;
    for (double least = 0.00011; least < 1.0e20; least *= 9) {
        for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
            final double lo = least, hi = bound;
            r.doubles(size, lo, hi).parallel().forEach(new DoubleConsumer() {

                @Override
                public void accept(double x) {
                    if (x < lo || x >= hi)
                        fails.getAndIncrement();
                }
            });
        }
    }
    assertEquals(fails.get(), 0);
}
Also used : DoubleConsumer(java8.util.function.DoubleConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SplittableRandom(java8.util.SplittableRandom)

Example 4 with DoubleConsumer

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

the class SplittableRandomTest method testUnsizedDoublesCountSeq.

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

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

Example 5 with DoubleConsumer

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

the class SplittableRandomTest method testUnsizedDoublesCount.

/**
 * A parallel unsized stream of doubles generates at least 100 values
 */
public void testUnsizedDoublesCount() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.doubles().limit(size).parallel().forEach(new DoubleConsumer() {

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

Aggregations

DoubleConsumer (java8.util.function.DoubleConsumer)6 SplittableRandom (java8.util.SplittableRandom)4 LongAdder (java8.util.concurrent.atomic.LongAdder)3 NoSuchElementException (java.util.NoSuchElementException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Spliterator (java8.util.Spliterator)1 ObjDoubleConsumer (java8.util.function.ObjDoubleConsumer)1