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);
});
}
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);
}
}
}
}
Aggregations