Search in sources :

Example 6 with DoubleStream

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

the class WhileOpStatefulTest method testWhileMulti.

private void testWhileMulti(Map<String, Supplier<Stream<Integer>>> sources, Consumer<Stream<Integer>> mRef, Consumer<IntStream> mInt, Consumer<LongStream> mLong, Consumer<DoubleStream> mDouble) {
    Map<String, Function<Stream<Integer>, Stream<Integer>>> transforms = new HashMap<>();
    transforms.put("Stream.sequential()", s -> {
        BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
        return s.peek(e -> {
            if (!isWithinExecutionPeriod.getAsBoolean()) {
                throw new RuntimeException();
            }
        });
    });
    transforms.put("Stream.parallel()", s -> {
        BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
        return s.parallel().peek(e -> {
            if (!isWithinExecutionPeriod.getAsBoolean()) {
                throw new RuntimeException();
            }
        });
    });
    Map<String, Consumer<Stream<Integer>>> actions = new HashMap<>();
    actions.put("Ref", mRef);
    actions.put("Int", s -> mInt.accept(s.mapToInt(e -> e)));
    actions.put("Long", s -> mLong.accept(s.mapToLong(e -> e)));
    actions.put("Double", s -> mDouble.accept(s.mapToDouble(e -> e)));
    actions.put("Ref using defaults", s -> mRef.accept(DefaultMethodStreams.delegateTo(s)));
    actions.put("Int using defaults", s -> mInt.accept(DefaultMethodStreams.delegateTo(s.mapToInt(e -> e))));
    actions.put("Long using defaults", s -> mLong.accept(DefaultMethodStreams.delegateTo(s.mapToLong(e -> e))));
    actions.put("Double using defaults", s -> mDouble.accept(DefaultMethodStreams.delegateTo(s.mapToDouble(e -> e))));
    for (Map.Entry<String, Supplier<Stream<Integer>>> s : sources.entrySet()) {
        setContext("source", s.getKey());
        for (Map.Entry<String, Function<Stream<Integer>, Stream<Integer>>> t : transforms.entrySet()) {
            setContext("transform", t.getKey());
            for (Map.Entry<String, Consumer<Stream<Integer>>> a : actions.entrySet()) {
                setContext("shape", a.getKey());
                Stream<Integer> stream = s.getValue().get();
                stream = t.getValue().apply(stream);
                a.getValue().accept(stream);
            }
        }
    }
}
Also used : Collector(java8.util.stream.Collector) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) Function(java8.util.function.Function) Supplier(java8.util.function.Supplier) IntStreams(java8.util.stream.IntStreams) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) RefStreams(java8.util.stream.RefStreams) Collectors.toCollection(java8.util.stream.Collectors.toCollection) Consumer(java8.util.function.Consumer) Map(java.util.Map) DefaultMethodStreams(java8.util.stream.DefaultMethodStreams) BooleanSupplier(java8.util.function.BooleanSupplier) DoubleStream(java8.util.stream.DoubleStream) LinkedList(java.util.LinkedList) Stream(java8.util.stream.Stream) HashMap(java.util.HashMap) Function(java8.util.function.Function) Consumer(java8.util.function.Consumer) Supplier(java8.util.function.Supplier) BooleanSupplier(java8.util.function.BooleanSupplier) BooleanSupplier(java8.util.function.BooleanSupplier) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with DoubleStream

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

the class WhileOpTest method testDoubleDefaultClose.

@Test(groups = { "serialization-hostile" })
public void testDoubleDefaultClose() {
    AtomicBoolean isClosed = new AtomicBoolean();
    DoubleStream s = DoubleStreams.of(1, 2, 3).onClose(() -> isClosed.set(true));
    DoubleStream ds = null;
    try {
        ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3);
        ds.count();
    } finally {
        if (ds != null) {
            ds.close();
        }
    }
    assertTrue(isClosed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) Iterables(java8.lang.Iterables) Predicate(java8.util.function.Predicate) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) ArrayList(java.util.ArrayList) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) LongStreams(java8.util.stream.LongStreams) Map(java.util.Map) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) LinkedHashSet(java.util.LinkedHashSet) DoubleStreams(java8.util.stream.DoubleStreams) Collection(java.util.Collection) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Set(java.util.Set) LongStream(java8.util.stream.LongStream) IntStreams(java8.util.stream.IntStreams) List(java.util.List) RefStreams(java8.util.stream.RefStreams) DefaultMethodStreams(java8.util.stream.DefaultMethodStreams) DoubleStream(java8.util.stream.DoubleStream) Stream(java8.util.stream.Stream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DoubleStream(java8.util.stream.DoubleStream) Test(org.testng.annotations.Test)

Example 8 with DoubleStream

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

the class FindAnyOpTest method exerciseDoubleStream.

void exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs) {
    OptionalDouble or = withData(data).terminal(fs, s -> s.findAny()).equalator(DOUBLE_VALID_ANSWER).exercise();
    if (or.isPresent()) {
        double r = or.getAsDouble();
        PrimitiveIterator.OfDouble it = fs.apply(data.stream()).iterator();
        boolean contained = false;
        while (!contained && it.hasNext()) {
            contained = r == it.nextDouble();
        }
        assertTrue(contained);
    } else {
        assertFalse(fs.apply(data.stream()).iterator().hasNext());
    }
}
Also used : PrimitiveIterator(java8.util.PrimitiveIterator) OptionalDouble(java8.util.OptionalDouble)

Aggregations

DoubleStream (java8.util.stream.DoubleStream)5 List (java.util.List)4 Function (java8.util.function.Function)4 IntStream (java8.util.stream.IntStream)4 LongStream (java8.util.stream.LongStream)4 OpTestCase (java8.util.stream.OpTestCase)4 Test (org.testng.annotations.Test)4 Consumer (java8.util.function.Consumer)3 IntStreams (java8.util.stream.IntStreams)3 StreamSupport (java8.util.stream.StreamSupport)3 TestData (java8.util.stream.TestData)3 Double (java.lang.Double)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 DoubleSummaryStatistics (java8.util.DoubleSummaryStatistics)2 Spliterator (java8.util.Spliterator)2 Spliterators (java8.util.Spliterators)2