Search in sources :

Example 51 with Stream

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

the class CollectorsTest method testFlatMappingClose.

@Test
public void testFlatMappingClose() {
    Function<Integer, Integer> classifier = i -> i;
    AtomicInteger ai = new AtomicInteger();
    Function<Integer, Stream<Integer>> flatMapper = i -> RefStreams.of(i, i).onClose(ai::getAndIncrement);
    Map<Integer, List<Integer>> m = RefStreams.of(1, 2).collect(groupingBy(classifier, flatMapping(flatMapper, toList())));
    assertEquals(m.size(), ai.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) LambdaTestHelpers.assertContents(java8.util.stream.LambdaTestHelpers.assertContents) LambdaTestHelpers.assertContentsUnordered(java8.util.stream.LambdaTestHelpers.assertContentsUnordered) Collectors.toSet(java8.util.stream.Collectors.toSet) Collectors.reducing(java8.util.stream.Collectors.reducing) Collector(java8.util.stream.Collector) Collectors.toMap(java8.util.stream.Collectors.toMap) Test(org.testng.annotations.Test) Optional(java8.util.Optional) Predicate(java8.util.function.Predicate) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) StreamOpFlagTestHelper(java8.util.stream.StreamOpFlagTestHelper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BinaryOperator(java8.util.function.BinaryOperator) Collectors.partitioningBy(java8.util.stream.Collectors.partitioningBy) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OpTestCase(java8.util.stream.OpTestCase) Set(java.util.Set) StreamSupport(java8.util.stream.StreamSupport) Supplier(java8.util.function.Supplier) Collectors.flatMapping(java8.util.stream.Collectors.flatMapping) Doubles(java8.lang.Doubles) Collectors.groupingBy(java8.util.stream.Collectors.groupingBy) Comparators(java8.util.Comparators) List(java.util.List) Collectors.filtering(java8.util.stream.Collectors.filtering) Collectors.toConcurrentMap(java8.util.stream.Collectors.toConcurrentMap) LambdaTestHelpers.mDoubler(java8.util.stream.LambdaTestHelpers.mDoubler) Integers(java8.lang.Integers) Collectors.collectingAndThen(java8.util.stream.Collectors.collectingAndThen) Collectors.toList(java8.util.stream.Collectors.toList) Collectors.groupingByConcurrent(java8.util.stream.Collectors.groupingByConcurrent) HashMap(java.util.HashMap) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) Collectors.mapping(java8.util.stream.Collectors.mapping) Longs(java8.lang.Longs) Iterator(java.util.Iterator) Collectors(java8.util.stream.Collectors) StringJoiner(java8.util.StringJoiner) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TreeMap(java.util.TreeMap) RefStreams(java8.util.stream.RefStreams) Collectors.toCollection(java8.util.stream.Collectors.toCollection) Predicates(java8.util.function.Predicates) Collections(java.util.Collections) Stream(java8.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stream(java8.util.stream.Stream) List(java.util.List) Collectors.toList(java8.util.stream.Collectors.toList) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 52 with Stream

use of java8.util.stream.Stream 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 53 with Stream

use of java8.util.stream.Stream 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 54 with Stream

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

the class SplittableRandomTest method testUnsizedIntsCount.

/**
 * A parallel unsized stream of ints generates at least 100 values
 */
public void testUnsizedIntsCount() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.ints().limit(size).parallel().forEach(x -> counter.increment());
    assertEquals(size, counter.sum());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Example 55 with Stream

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

the class SplittableRandomTest method testBoundedLongs.

/**
 * Each of a parallel sized stream of bounded longs is within bounds
 */
public void testBoundedLongs() {
    AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    long size = 123L;
    for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
        for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
            final long lo = least, hi = bound;
            r.longs(size, lo, hi).parallel().forEach(x -> {
                if (x < lo || x >= hi)
                    fails.getAndIncrement();
            });
        }
    }
    assertEquals(0, fails.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SplittableRandom(java8.util.SplittableRandom)

Aggregations

LongAdder (java8.util.concurrent.atomic.LongAdder)36 SplittableRandom (java8.util.SplittableRandom)27 ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 ArrayList (java.util.ArrayList)9 Test (org.testng.annotations.Test)9 Function (java8.util.function.Function)8 OpTestCase (java8.util.stream.OpTestCase)8 List (java.util.List)7 StreamSupport (java8.util.stream.StreamSupport)7 TestData (java8.util.stream.TestData)7 Set (java.util.Set)6 Supplier (java8.util.function.Supplier)6 DoubleStream (java8.util.stream.DoubleStream)6 IntStream (java8.util.stream.IntStream)6 LongStream (java8.util.stream.LongStream)6 HashSet (java.util.HashSet)5 Spliterator (java8.util.Spliterator)5 Consumer (java8.util.function.Consumer)5 Collection (java.util.Collection)4