Search in sources :

Example 6 with Spliterator

use of java8.util.Spliterator 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 7 with Spliterator

use of java8.util.Spliterator 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 8 with Spliterator

use of java8.util.Spliterator 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 9 with Spliterator

use of java8.util.Spliterator 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)

Example 10 with Spliterator

use of java8.util.Spliterator 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)

Aggregations

Spliterator (java8.util.Spliterator)13 Test (org.testng.annotations.Test)10 List (java.util.List)9 NoSuchElementException (java.util.NoSuchElementException)9 Consumer (java8.util.function.Consumer)9 Arrays (java.util.Arrays)8 Spliterators (java8.util.Spliterators)8 StreamSupport (java8.util.stream.StreamSupport)8 Iterator (java.util.Iterator)7 ArrayDeque (java.util.ArrayDeque)6 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Deque (java.util.Deque)6 HashSet (java.util.HashSet)6 LinkedList (java.util.LinkedList)6 BlockingDeque (java.util.concurrent.BlockingDeque)6 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6 Collections (java.util.Collections)5 ConcurrentModificationException (java.util.ConcurrentModificationException)5