Search in sources :

Example 11 with Spliterator

use of java.util.Spliterator in project jdk8u_jdk by JetBrains.

the class IntPipeline method forEachWithCancel.

@Override
final void forEachWithCancel(Spliterator<Integer> spliterator, Sink<Integer> sink) {
    Spliterator.OfInt spl = adapt(spliterator);
    IntConsumer adaptedSink = adapt(sink);
    do {
    } while (!sink.cancellationRequested() && spl.tryAdvance(adaptedSink));
}
Also used : Spliterator(java.util.Spliterator) IntConsumer(java.util.function.IntConsumer) ObjIntConsumer(java.util.function.ObjIntConsumer)

Example 12 with Spliterator

use of java.util.Spliterator in project jdk8u_jdk by JetBrains.

the class SequentialOpTest method testLazy.

@SuppressWarnings({ "rawtypes", "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) TestData(java.util.stream.TestData) StreamTestDataProvider(java.util.stream.StreamTestDataProvider) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) UnaryOperator(java.util.function.UnaryOperator) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Stream(java.util.stream.Stream) LambdaTestHelpers(java.util.stream.LambdaTestHelpers) OpTestCase(java.util.stream.OpTestCase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertTrue(org.testng.Assert.assertTrue) Comparator(java.util.Comparator) Spliterator(java.util.Spliterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Supplier(java.util.function.Supplier) Stream(java.util.stream.Stream) UnaryOperator(java.util.function.UnaryOperator) Test(org.testng.annotations.Test)

Aggregations

Spliterator (java.util.Spliterator)12 DoubleConsumer (java.util.function.DoubleConsumer)5 IntConsumer (java.util.function.IntConsumer)5 LongConsumer (java.util.function.LongConsumer)5 Comparator (java.util.Comparator)4 Function (java.util.function.Function)4 UnaryOperator (java.util.function.UnaryOperator)4 LambdaTestHelpers (java.util.stream.LambdaTestHelpers)4 OpTestCase (java.util.stream.OpTestCase)4 Stream (java.util.stream.Stream)4 StreamTestDataProvider (java.util.stream.StreamTestDataProvider)4 TestData (java.util.stream.TestData)4 Test (org.testng.annotations.Test)4 Arrays (java.util.Arrays)3 List (java.util.List)3 Consumer (java.util.function.Consumer)3 DoubleStream (java.util.stream.DoubleStream)3 DoubleStreamTestDataProvider (java.util.stream.DoubleStreamTestDataProvider)3 IntStream (java.util.stream.IntStream)3 IntStreamTestDataProvider (java.util.stream.IntStreamTestDataProvider)3