Search in sources :

Example 1 with Spliterators

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

the class Collection8Test method testLateBindingStyle.

/**
 * Spliterators are either IMMUTABLE or truly late-binding or, if
 * concurrent, use the same "late-binding style" of returning
 * elements added between creation and first use.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(dataProvider = "Source")
public void testLateBindingStyle(String description, Supplier<CollectionImplementation> sci) {
    if (!testImplementationDetails)
        return;
    CollectionImplementation impl = sci.get();
    // for jdk8
    if (impl.klazz() == ArrayList.class)
        return;
    // Immutable (snapshot) spliterators are exempt
    if (Spliterators.spliterator(impl.emptyCollection()).hasCharacteristics(Spliterator.IMMUTABLE))
        return;
    final Object one = impl.makeElement(1);
    {
        final Collection c = impl.emptyCollection();
        final Spliterator<?> split = Spliterators.spliterator(c);
        c.add(one);
        assertTrue(split.tryAdvance(e -> {
            assertSame(e, one);
        }));
        assertFalse(split.tryAdvance(e -> {
            throw new AssertionError();
        }));
        assertTrue(c.contains(one));
    }
    {
        final AtomicLong count = new AtomicLong(0);
        final Collection c = impl.emptyCollection();
        final Spliterator<?> split = Spliterators.spliterator(c);
        c.add(one);
        split.forEachRemaining(e -> {
            assertSame(e, one);
            count.getAndIncrement();
        });
        assertEquals(1L, count.get());
        assertFalse(split.tryAdvance(e -> {
            throw new AssertionError();
        }));
        assertTrue(c.contains(one));
    }
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) PriorityQueue(java.util.PriorityQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) Iterables(java8.lang.Iterables) Deque(java.util.Deque) Predicate(java8.util.function.Predicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) HashSet(java.util.HashSet) Phaser(java8.util.concurrent.Phaser) Iterators(java8.util.Iterators) Future(java.util.concurrent.Future) J8Arrays(java8.util.J8Arrays) Vector(java.util.Vector) Consumer(java8.util.function.Consumer) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) BlockingDeque(java.util.concurrent.BlockingDeque) Collection(java.util.Collection) Spliterator(java8.util.Spliterator) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java8.util.stream.Collectors) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Spliterators(java8.util.Spliterators) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) StreamSupport(java8.util.stream.StreamSupport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) Supplier(java8.util.function.Supplier) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HOURS(java.util.concurrent.TimeUnit.HOURS) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ConcurrentModificationException(java.util.ConcurrentModificationException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) Collection(java.util.Collection) Spliterator(java8.util.Spliterator) Test(org.testng.annotations.Test)

Example 2 with Spliterators

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

the class Collection8Test method testStickySpliteratorExhaustion.

/**
 * Concurrent Spliterators, once exhausted, stay exhausted.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testStickySpliteratorExhaustion(String description, Supplier<CollectionImplementation> sci) throws Throwable {
    CollectionImplementation impl = sci.get();
    if (HAS_JAVA8_SPLITERATOR_BUG && PriorityBlockingQueue.class.equals(impl.klazz())) {
        // https://bugs.openjdk.java.net/browse/JDK-8172023
        return;
    }
    if (!impl.isConcurrent())
        return;
    if (!testImplementationDetails)
        return;
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final Consumer<?> alwaysThrows = e -> {
        throw new AssertionError();
    };
    final Collection c = impl.emptyCollection();
    final Spliterator s = Spliterators.spliterator(c);
    if (rnd.nextBoolean()) {
        assertFalse(s.tryAdvance(alwaysThrows));
    } else {
        s.forEachRemaining(alwaysThrows);
    }
    final Object one = impl.makeElement(1);
    // Spliterator should not notice added element
    c.add(one);
    if (rnd.nextBoolean()) {
        assertFalse(s.tryAdvance(alwaysThrows));
    } else {
        s.forEachRemaining(alwaysThrows);
    }
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) PriorityQueue(java.util.PriorityQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) Iterables(java8.lang.Iterables) Deque(java.util.Deque) Predicate(java8.util.function.Predicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) HashSet(java.util.HashSet) Phaser(java8.util.concurrent.Phaser) Iterators(java8.util.Iterators) Future(java.util.concurrent.Future) J8Arrays(java8.util.J8Arrays) Vector(java.util.Vector) Consumer(java8.util.function.Consumer) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) BlockingDeque(java.util.concurrent.BlockingDeque) Collection(java.util.Collection) Spliterator(java8.util.Spliterator) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java8.util.stream.Collectors) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Spliterators(java8.util.Spliterators) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) StreamSupport(java8.util.stream.StreamSupport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) Supplier(java8.util.function.Supplier) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HOURS(java.util.concurrent.TimeUnit.HOURS) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ConcurrentModificationException(java.util.ConcurrentModificationException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) Spliterator(java8.util.Spliterator) Test(org.testng.annotations.Test)

Example 3 with Spliterators

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

the class Spliterators method iterator.

// Iterators from Spliterators
/**
 * Creates an {@code Iterator} from a {@code Spliterator}.
 *
 * <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 <T> Type of elements
 * @param spliterator The spliterator
 * @return An iterator
 * @throws NullPointerException if the given spliterator is {@code null}
 */
public static <T> Iterator<T> iterator(Spliterator<? extends T> spliterator) {
    Objects.requireNonNull(spliterator);
    class Adapter implements Iterator<T>, Consumer<T> {

        boolean valueReady = false;

        T nextElement;

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

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

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

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

Aggregations

Iterator (java.util.Iterator)3 NoSuchElementException (java.util.NoSuchElementException)3 ArrayDeque (java.util.ArrayDeque)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 Deque (java.util.Deque)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 PriorityQueue (java.util.PriorityQueue)2 Queue (java.util.Queue)2 Set (java.util.Set)2 Vector (java.util.Vector)2 BlockingDeque (java.util.concurrent.BlockingDeque)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2