< prev index next >
test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectorsTest.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -27,20 +27,22 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.IntSummaryStatistics;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
@@ -94,11 +96,11 @@
}
@Override
void assertValue(R value, Supplier<Stream<T>> source, boolean ordered) throws ReflectiveOperationException {
downstream.assertValue(value,
- () -> source.get().map(mapper::apply),
+ () -> source.get().map(mapper),
ordered);
}
}
static class FlatMappingAssertion<T, V, R> extends CollectorAssertion<T, R> {
@@ -112,11 +114,11 @@
}
@Override
void assertValue(R value, Supplier<Stream<T>> source, boolean ordered) throws ReflectiveOperationException {
downstream.assertValue(value,
- () -> source.get().flatMap(mapper::apply),
+ () -> source.get().flatMap(mapper),
ordered);
}
}
static class FilteringAssertion<T, R> extends CollectorAssertion<T, R> {
@@ -285,10 +287,31 @@
assertEquals(value, reduced.get());
}
}
}
+ static class TeeingAndThenAssertion<T, R1, R2, RR> extends CollectorAssertion<T, RR> {
+ private final Collector<T, ?, R1> c1;
+ private final Collector<T, ?, R2> c2;
+ private final BiFunction<? super R1, ? super R2, ? extends RR> finisher;
+
+ TeeingAndThenAssertion(Collector<T, ?, R1> c1, Collector<T, ?, R2> c2,
+ BiFunction<? super R1, ? super R2, ? extends RR> finisher) {
+ this.c1 = c1;
+ this.c2 = c2;
+ this.finisher = finisher;
+ }
+
+ @Override
+ void assertValue(RR value, Supplier<Stream<T>> source, boolean ordered) {
+ R1 r1 = source.get().collect(c1);
+ R2 r2 = source.get().collect(c2);
+ RR expected = finisher.apply(r1, r2);
+ assertEquals(value, expected);
+ }
+ }
+
private <T> ResultAsserter<T> mapTabulationAsserter(boolean ordered) {
return (act, exp, ord, par) -> {
if (par && (!ordered || !ord)) {
CollectorsTest.nestedMapEqualityAssertion(act, exp);
}
@@ -744,6 +767,44 @@
fail("Expecting immutable result");
}
catch (UnsupportedOperationException ignored) { }
}
+ @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+ public void testTeeingAndThen(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
+ Collector<Integer, ?, Long> summing = Collectors.summingLong(Integer::valueOf);
+ Collector<Integer, ?, Long> counting = Collectors.counting();
+ Collector<Integer, ?, Integer> min = collectingAndThen(Collectors.<Integer>minBy(Comparator.naturalOrder()),
+ opt -> opt.orElse(Integer.MAX_VALUE));
+ Collector<Integer, ?, Integer> max = collectingAndThen(Collectors.<Integer>maxBy(Comparator.naturalOrder()),
+ opt -> opt.orElse(Integer.MIN_VALUE));
+ Collector<Integer, ?, String> joining = mapping(String::valueOf, Collectors.joining(", ", "[", "]"));
+
+ Collector<Integer, ?, Map.Entry<Long, Long>> sumAndCount = Collectors.teeingAndThen(summing, counting, Map::entry);
+ Collector<Integer, ?, Map.Entry<Integer, Integer>> minAndMax = Collectors.teeingAndThen(min, max, Map::entry);
+ Collector<Integer, ?, Double> averaging = Collectors.teeingAndThen(summing, counting,
+ (sum, count) -> ((double)sum) / count);
+ Collector<Integer, ?, String> summaryStatistics = Collectors.teeingAndThen(sumAndCount, minAndMax,
+ (sumCountEntry, minMaxEntry) -> new IntSummaryStatistics(
+ sumCountEntry.getValue(), minMaxEntry.getKey(),
+ minMaxEntry.getValue(), sumCountEntry.getKey()).toString());
+ Collector<Integer, ?, String> countAndContent = Collectors.teeingAndThen(counting, joining,
+ (count, content) -> count+": "+content);
+
+ assertCollect(data, sumAndCount, stream -> {
+ List<Integer> list = stream.collect(toList());
+ return Map.entry(list.stream().mapToLong(Integer::intValue).sum(), (long) list.size());
+ });
+ assertCollect(data, averaging, stream -> stream.mapToInt(Integer::intValue).average().orElse(Double.NaN));
+ assertCollect(data, summaryStatistics,
+ stream -> stream.mapToInt(Integer::intValue).summaryStatistics().toString());
+ assertCollect(data, countAndContent, stream -> {
+ List<Integer> list = stream.collect(toList());
+ return list.size()+": "+list;
+ });
+
+ Function<Integer, Integer> classifier = i -> i % 3;
+ exerciseMapCollection(data, groupingBy(classifier, sumAndCount),
+ new GroupingByAssertion<>(classifier, Map.class,
+ new TeeingAndThenAssertion<>(summing, counting, Map::entry)));
+ }
}
< prev index next >