Search in sources :

Example 1 with LongConsumer

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

the class Spliterators method iterator.

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

        boolean valueReady = false;

        long nextElement;

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

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

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

        @Override
        public Long next() {
            return nextLong();
        }

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

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

Example 2 with LongConsumer

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

the class LongPipeline method forEachWithCancel.

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

Example 3 with LongConsumer

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

the class SplittableRandomTest method testBoundedLongs.

/**
 * Each of a parallel sized stream of bounded longs is within bounds
 */
public void testBoundedLongs() {
    final AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    long size = 123L;
    for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
        for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
            final long lo = least, hi = bound;
            r.longs(size, lo, hi).parallel().forEach(new LongConsumer() {

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

Example 4 with LongConsumer

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

the class SplittableRandomTest method testLongsCount.

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

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

Example 5 with LongConsumer

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

the class SplittableRandomTest method testUnsizedLongsCount.

/**
 * A parallel unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCount() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).parallel().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)

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