Search in sources :

Example 1 with BooleanSupplier

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

the class WhileOpStatefulTest method testTimedTakeWithCount.

@Test
public void testTimedTakeWithCount() {
    testTakeWhileMulti(s -> {
        BooleanSupplier isWithinTakePeriod = within(System.currentTimeMillis(), COUNT_PERIOD);
        s.takeWhile(e -> isWithinTakePeriod.getAsBoolean()).mapToLong(e -> 1).reduce(0, java8.lang.Longs::sum);
    }, s -> {
        BooleanSupplier isWithinTakePeriod = within(System.currentTimeMillis(), COUNT_PERIOD);
        s.takeWhile(e -> isWithinTakePeriod.getAsBoolean()).mapToLong(e -> 1).reduce(0, java8.lang.Longs::sum);
    }, s -> {
        BooleanSupplier isWithinTakePeriod = within(System.currentTimeMillis(), COUNT_PERIOD);
        s.takeWhile(e -> isWithinTakePeriod.getAsBoolean()).map(e -> 1).reduce(0, java8.lang.Longs::sum);
    }, s -> {
        BooleanSupplier isWithinTakePeriod = within(System.currentTimeMillis(), COUNT_PERIOD);
        s.takeWhile(e -> isWithinTakePeriod.getAsBoolean()).mapToLong(e -> 1).reduce(0, java8.lang.Longs::sum);
    });
}
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) BooleanSupplier(java8.util.function.BooleanSupplier) Test(org.testng.annotations.Test)

Example 2 with BooleanSupplier

use of java8.util.function.BooleanSupplier 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)

Aggregations

HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 BooleanSupplier (java8.util.function.BooleanSupplier)2 Consumer (java8.util.function.Consumer)2 Function (java8.util.function.Function)2 Supplier (java8.util.function.Supplier)2 Collector (java8.util.stream.Collector)2 Collectors.toCollection (java8.util.stream.Collectors.toCollection)2 DefaultMethodStreams (java8.util.stream.DefaultMethodStreams)2 DoubleStream (java8.util.stream.DoubleStream)2 IntStream (java8.util.stream.IntStream)2 IntStreams (java8.util.stream.IntStreams)2 LongStream (java8.util.stream.LongStream)2 OpTestCase (java8.util.stream.OpTestCase)2 RefStreams (java8.util.stream.RefStreams)2 Stream (java8.util.stream.Stream)2