use of java8.util.function.Function in project streamsupport by stefan-zobel.
the class FindAnyOpTest method exerciseLongStream.
void exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs) {
OptionalLong or = withData(data).terminal(fs, s -> s.findAny()).equalator(LONG_VALID_ANSWER).exercise();
if (or.isPresent()) {
long r = or.getAsLong();
PrimitiveIterator.OfLong it = fs.apply(data.stream()).iterator();
boolean contained = false;
while (!contained && it.hasNext()) {
contained = r == it.nextLong();
}
assertTrue(contained);
} else {
assertFalse(fs.apply(data.stream()).iterator().hasNext());
}
}
use of java8.util.function.Function in project streamsupport by stefan-zobel.
the class FindAnyOpTest method exerciseIntStream.
void exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs) {
OptionalInt or = withData(data).terminal(fs, s -> s.findAny()).equalator(INT_VALID_ANSWER).exercise();
if (or.isPresent()) {
int r = or.getAsInt();
PrimitiveIterator.OfInt it = fs.apply(data.stream()).iterator();
boolean contained = false;
while (!contained && it.hasNext()) {
contained = r == it.nextInt();
}
assertTrue(contained);
} else {
assertFalse(fs.apply(data.stream()).iterator().hasNext());
}
}
use of java8.util.function.Function 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());
}
use of java8.util.function.Function 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);
}
}
}
Aggregations