All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java8.util.PBQueueSpliterator Maven / Gradle / Ivy

Go to download

streamsupport is a backport of the Java 8 java.util.function (functional interfaces) and java.util.stream (streams) API for Java 6 / 7 and Android developers

There is a newer version: 1.7.4
Show newest version
/*
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
package java8.util;

import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;

import java8.util.function.Consumer;
import java8.util.Spliterator;

/*
 * Spliterator for java.util.concurrent.PriorityBlockingQueue.
 * Immutable snapshot spliterator that binds to elements "late".
 */
final class PBQueueSpliterator implements Spliterator {
// CVS rev. 1.140
    private final PriorityBlockingQueue queue;
    private Object[] array;        // null until late-bound-initialized
    private int index;
    private int fence;

    private PBQueueSpliterator(PriorityBlockingQueue queue,
            Object[] array, int index, int fence) {
        this.queue = queue;
        this.array = array;
        this.index = index;
        this.fence = fence;
    }

    static  Spliterator spliterator(PriorityBlockingQueue queue) {
        return new PBQueueSpliterator(queue, null, 0, -1);
    }

    private int getFence() {
        if (array == null)
            fence = (array = queue.toArray()).length;
        return fence;
    }

    @Override
    public PBQueueSpliterator trySplit() {
        int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
        return (lo >= mid) ? null :
            new PBQueueSpliterator(queue, array, lo, index = mid);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void forEachRemaining(Consumer action) {
        Objects.requireNonNull(action);
        int hi = getFence(), lo = index;
        Object[] es = array;
        index = hi;                 // ensure exhaustion
        for (int i = lo; i < hi; i++)
            action.accept((E) es[i]);
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean tryAdvance(Consumer action) {
        Objects.requireNonNull(action);
        if (getFence() > index && index >= 0) {
            action.accept((E) array[index++]);
            return true;
        }
        return false;
    }

    @Override
    public long estimateSize() { return getFence() - index; }

    @Override
    public int characteristics() {
        return (Spliterator.NONNULL |
                Spliterator.SIZED |
                Spliterator.SUBSIZED);
    }

    @Override
    public Comparator getComparator() {
        return Spliterators.getComparator(this);
    }

    @Override
    public long getExactSizeIfKnown() {
        return Spliterators.getExactSizeIfKnown(this);
    }

    @Override
    public boolean hasCharacteristics(int characteristics) {
        return Spliterators.hasCharacteristics(this, characteristics);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy