Fix failing Collectors.partitionBy test for JDK
Collectors.partitionBy must return Map with two entries.
Change-Id: I1d1ac90221c853c97add98d7abb4550a5b672c80
diff --git a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java
index bc7bd14..751f231 100644
--- a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java
+++ b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java
@@ -93,8 +93,16 @@
Function<? super T, ? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T, A, D> downstream) {
- return Collector.<T, Map<K, List<T>>, M>of(
- LinkedHashMap::new,
+ return groupingBy0(LinkedHashMap::new, classifier, mapFactory, downstream);
+ }
+
+ private static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy0(
+ Supplier<Map<K, List<T>>> supplier,
+ Function<? super T, ? extends K> classifier,
+ Supplier<M> mapFactory,
+ Collector<? super T, A, D> downstream) {
+ return Collector.of(
+ supplier,
(m, o) -> {
K k = classifier.apply(o);
List<T> l = m.get(k);
@@ -172,13 +180,21 @@
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(
Predicate<? super T> predicate) {
- // calling groupBy directly rather than partioningBy so that it can be optimized later
- return groupingBy(predicate::test);
+ return partitioningBy(predicate, toList());
}
public static <T, D, A> Collector<T, ?, Map<Boolean, D>> partitioningBy(
Predicate<? super T> predicate, Collector<? super T, A, D> downstream) {
- return groupingBy(predicate::test, downstream);
+ return groupingBy0(partitionSupplier(), predicate::test, HashMap::new, downstream);
+ }
+
+ private static <T> Supplier<Map<Boolean, List<T>>> partitionSupplier() {
+ return () -> {
+ Map<Boolean, List<T>> partition = new LinkedHashMap<>();
+ partition.put(false, new ArrayList<>());
+ partition.put(true, new ArrayList<>());
+ return partition;
+ };
}
public static <T> Collector<T,?,Optional<T>> reducing(BinaryOperator<T> op) {
diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java
index 93e1d28..bfafad0 100644
--- a/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java
+++ b/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java
@@ -256,18 +256,31 @@
mapOfLists.put(true, Collections.singletonList(true));
mapOfLists.put(false, Collections.singletonList(false));
applyItems(mapOfLists, c1, true, false);
+
mapOfLists.clear();
+ mapOfLists.put(false, Collections.emptyList());
mapOfLists.put(true, Arrays.asList(true, true));
applyItems(mapOfLists, c1, true, true);
+
mapOfLists.clear();
mapOfLists.put(false, Arrays.asList(false, false));
+ mapOfLists.put(true, Collections.emptyList());
applyItems(mapOfLists, c1, false, false);
- assertZeroItemsCollectedAs(Collections.emptyMap(), c1);
- assertSingleItemCollectedAs(
- Collections.singletonMap(true, Collections.singletonList(true)), c1, true);
- assertSingleItemCollectedAs(
- Collections.singletonMap(false, Collections.singletonList(false)), c1, false);
+ mapOfLists.clear();
+ mapOfLists.put(false, Collections.emptyList());
+ mapOfLists.put(true, Collections.emptyList());
+ assertZeroItemsCollectedAs(mapOfLists, c1);
+
+ mapOfLists.clear();
+ mapOfLists.put(false, Collections.emptyList());
+ mapOfLists.put(true, Collections.singletonList(true));
+ assertSingleItemCollectedAs(mapOfLists, c1, true);
+
+ mapOfLists.clear();
+ mapOfLists.put(false, Collections.singletonList(false));
+ mapOfLists.put(true, Collections.emptyList());
+ assertSingleItemCollectedAs(mapOfLists, c1, false);
Collector<Boolean, ?, Map<Boolean, Set<Boolean>>> c2 =
partitioningBy(Boolean::valueOf, toSet());
@@ -276,11 +289,15 @@
mapOfSets.put(true, Collections.singleton(true));
mapOfSets.put(false, Collections.singleton(false));
applyItems(mapOfSets, c2, true, false);
+
mapOfSets.clear();
+ mapOfSets.put(false, Collections.emptySet());
mapOfSets.put(true, Collections.singleton(true));
applyItems(mapOfSets, c2, true, true);
+
mapOfSets.clear();
mapOfSets.put(false, Collections.singleton(false));
+ mapOfSets.put(true, Collections.emptySet());
applyItems(mapOfSets, c2, false, false);
}