Search in sources :

Example 16 with Spliterator

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

the class Collection8Test method testRandomElementRemovalDuringTraversal.

/**
 * Some elements randomly disappear in the middle of traversal.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testRandomElementRemovalDuringTraversal(String description, Supplier<CollectionImplementation> sci) {
    CollectionImplementation impl = sci.get();
    if (CopyOnWriteArrayList.class.equals(impl.klazz()) || CopyOnWriteArraySet.class.equals(impl.klazz())) {
        return;
    }
    if (HAS_JAVA8_SPLITERATOR_BUG && (LinkedBlockingDeque.class.equals(impl.klazz())) || LinkedBlockingQueue.class.equals(impl.klazz())) {
        // https://bugs.openjdk.java.net/browse/JDK-8171051
        return;
    }
    Collection c = impl.emptyCollection();
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    int n = rnd.nextInt(6);
    ArrayList copy = new ArrayList();
    for (int i = 0; i < n; i++) {
        Object x = impl.makeElement(i);
        copy.add(x);
        c.add(x);
    }
    ArrayList iterated = new ArrayList();
    ArrayList spliterated = new ArrayList();
    ArrayList removed = new ArrayList();
    Spliterator<?> s = Spliterators.spliterator(c);
    Iterator<?> it = c.iterator();
    if (!(s.hasCharacteristics(Spliterator.CONCURRENT) || s.hasCharacteristics(Spliterator.IMMUTABLE)))
        return;
    for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
        assertTrue(s.tryAdvance(e -> {
        }));
        if (rnd.nextBoolean())
            assertTrue(it.hasNext());
        it.next();
    }
    // TODO: many more removal methods
    if (rnd.nextBoolean()) {
        for (Iterator<?> z = c.iterator(); z.hasNext(); ) {
            Object e = z.next();
            if (rnd.nextBoolean()) {
                try {
                    z.remove();
                } catch (UnsupportedOperationException ok) {
                    return;
                }
                removed.add(e);
            }
        }
    } else {
        Predicate randomlyRemove = e -> {
            if (rnd.nextBoolean()) {
                removed.add(e);
                return true;
            } else
                return false;
        };
        Iterables.removeIf(c, randomlyRemove);
    }
    s.forEachRemaining(spliterated::add);
    while (it.hasNext()) iterated.add(it.next());
    assertTrue(copy.containsAll(iterated));
    assertTrue(copy.containsAll(spliterated));
    assertTrue(copy.containsAll(removed));
    if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
        ArrayList<?> iteratedAndRemoved = new ArrayList<>(iterated);
        ArrayList<?> spliteratedAndRemoved = new ArrayList<>(spliterated);
        iteratedAndRemoved.retainAll(removed);
        spliteratedAndRemoved.retainAll(removed);
        assertTrue(iteratedAndRemoved.size() <= 1);
        assertTrue(spliteratedAndRemoved.size() <= 1);
        if (testImplementationDetails && !(c instanceof java.util.concurrent.ArrayBlockingQueue))
            assertTrue(spliteratedAndRemoved.isEmpty());
    }
}
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) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Predicate(java8.util.function.Predicate) Collection(java.util.Collection) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.testng.annotations.Test)

Example 17 with Spliterator

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

the class ConcatTest method assertLongConcat.

private void assertLongConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
    LongStream result = LongStreams.concat(s1.mapToLong(Integer::longValue), s2.mapToLong(Integer::longValue));
    assertEquals(result.isParallel(), parallel);
    assertConcatContent(result.spliterator(), ordered, StreamSupport.stream(expected).mapToLong(Integer::longValue).spliterator());
}
Also used : LongStream(java8.util.stream.LongStream)

Example 18 with Spliterator

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

the class SequentialOpTest method testLazy.

@SuppressWarnings("unchecked")
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class, groups = { "serialization-hostile" })
public void testLazy(String name, TestData.OfRef<Integer> data) {
    Function<Integer, Integer> id = LambdaTestHelpers.identity();
    AtomicInteger counter = new AtomicInteger();
    Supplier<Stream<Integer>>[] suppliers = new Supplier[] { () -> data.stream(), () -> data.parallelStream() };
    UnaryOperator<Stream<Integer>>[] configs = new UnaryOperator[] { (UnaryOperator<Stream<Integer>>) s -> s.peek(e -> {
        counter.incrementAndGet();
    }), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
        counter.incrementAndGet();
    }).sequential().map(id), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
        counter.incrementAndGet();
    }).parallel().map(id), (UnaryOperator<Stream<Integer>>) s -> s.sequential().map(id).peek(e -> {
        counter.incrementAndGet();
    }).map(id), (UnaryOperator<Stream<Integer>>) s -> s.parallel().map(id).peek(e -> {
        counter.incrementAndGet();
    }).map(id) };
    for (int i = 0; i < suppliers.length; i++) {
        setContext("supplierIndex", i);
        Supplier<Stream<Integer>> supp = suppliers[i];
        for (int j = 0; j < configs.length; j++) {
            setContext("configIndex", j);
            UnaryOperator<Stream<Integer>> config = configs[j];
            counter.set(0);
            Stream<Integer> stream = config.apply(supp.get());
            assertEquals(0, counter.get());
            Iterator<Integer> iterator = stream.iterator();
            assertEquals(0, counter.get());
            if (iterator.hasNext())
                iterator.next();
            assertTrue(data.size() == 0 || counter.get() > 0);
            counter.set(0);
            stream = config.apply(supp.get());
            Spliterator<Integer> spliterator = stream.spliterator();
            assertEquals(0, counter.get());
            spliterator.forEachRemaining(e -> {
            });
            assertTrue(data.size() == 0 || counter.get() > 0);
        }
    }
}
Also used : Iterator(java.util.Iterator) Spliterator(java8.util.Spliterator) OpTestCase(java8.util.stream.OpTestCase) Test(org.testng.annotations.Test) UnaryOperator(java8.util.function.UnaryOperator) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) Supplier(java8.util.function.Supplier) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) Comparators(java8.util.Comparators) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) Stream(java8.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Supplier(java8.util.function.Supplier) Stream(java8.util.stream.Stream) UnaryOperator(java8.util.function.UnaryOperator) Test(org.testng.annotations.Test)

Example 19 with Spliterator

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

the class DoublePipeline method forEachWithCancel.

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

Example 20 with Spliterator

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

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