changeset 7270:e572f1bdcf0b

More witness elimination, enabled by a311c0821e90
author briangoetz
date Sun, 03 Feb 2013 12:12:51 -0500
parents 1b8b4bf3f0a2
children dfabd7437f98
files src/share/classes/java/util/stream/Collectors.java src/share/classes/java/util/stream/ConcurrentCollectors.java test-ng/tests/org/openjdk/tests/java/util/stream/TabulatorsTest.java
diffstat 3 files changed, 15 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/Collectors.java	Sun Feb 03 11:47:19 2013 -0500
+++ b/src/share/classes/java/util/stream/Collectors.java	Sun Feb 03 12:12:51 2013 -0500
@@ -46,17 +46,14 @@
  * Collectors
  */
 public class Collectors {
-    public static final BinaryOperator<?> THROWING_MERGER
-            = (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); };
+
+    public static<T> BinaryOperator<T> throwingMerger() {
+        return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); };
+    }
 
     private static<T, R> Collector<T, R> leftCombining(Supplier<? extends R> supplier,
                                                        BiConsumer<R, T> accumulator,
-                                                       BiConsumer<R, R> reducer) {
-        BinaryOperator<R> operator = (r, other) -> {
-            reducer.accept(r, other);
-            return r;
-        };
-
+                                                       BiConsumer<R, R> merger) {
         return new Collector<T, R>() {
             @Override
             public R makeResult() {
@@ -70,7 +67,8 @@
 
             @Override
             public R combine(R result, R other) {
-                return operator.apply(result, other);
+                merger.accept(result, other);
+                return result;
             }
         };
     }
@@ -191,7 +189,7 @@
     }
 
     public static <T, U> Collector<T, Map<T,U>> joiningWith(Function<? super T, ? extends U> mapper) {
-        return joiningWith(mapper, (BinaryOperator<U>) THROWING_MERGER);
+        return joiningWith(mapper, throwingMerger());
     }
 
     public static <T, U> Collector<T, Map<T,U>> joiningWith(Function<? super T, ? extends U> mapper,
@@ -201,7 +199,7 @@
 
     public static <T, U, M extends Map<T, U>> Collector<T, M> joiningWith(Function<? super T, ? extends U> mapper,
                                                                           Supplier<M> mapSupplier) {
-        return joiningWith(mapper, (BinaryOperator<U>) THROWING_MERGER, mapSupplier);
+        return joiningWith(mapper, throwingMerger(), mapSupplier);
     }
     public static <T, U, M extends Map<T, U>> Collector<T, M> joiningWith(Function<? super T, ? extends U> mapper,
                                                                           BinaryOperator<U> mergeFunction,
--- a/src/share/classes/java/util/stream/ConcurrentCollectors.java	Sun Feb 03 11:47:19 2013 -0500
+++ b/src/share/classes/java/util/stream/ConcurrentCollectors.java	Sun Feb 03 12:12:51 2013 -0500
@@ -140,7 +140,7 @@
 
 
     public static <T, U> Collector<T, ConcurrentMap<T,U>> mappedTo(Function<? super T, ? extends U> mapper) {
-        return mappedTo(mapper, (BinaryOperator<U>) Collectors.THROWING_MERGER);
+        return mappedTo(mapper, Collectors.throwingMerger());
     }
 
     public static <T, U> Collector<T, ConcurrentMap<T,U>> mappedTo(Function<? super T, ? extends U> mapper,
@@ -150,7 +150,7 @@
 
     public static <T, U, M extends ConcurrentMap<T, U>> Collector<T, M> mappedTo(Function<? super T, ? extends U> mapper,
                                                                                Supplier<M> mapSupplier) {
-        return mappedTo(mapper, (BinaryOperator<U>) Collectors.THROWING_MERGER, mapSupplier);
+        return mappedTo(mapper, Collectors.throwingMerger(), mapSupplier);
     }
     public static <T, U, M extends ConcurrentMap<T, U>> Collector<T, M> mappedTo(Function<? super T, ? extends U> mapper,
                                                                                BinaryOperator<U> mergeFunction,
--- a/test-ng/tests/org/openjdk/tests/java/util/stream/TabulatorsTest.java	Sun Feb 03 11:47:19 2013 -0500
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/TabulatorsTest.java	Sun Feb 03 12:12:51 2013 -0500
@@ -189,7 +189,7 @@
 
         // With explicit constructors
         exerciseTabulation(data,
-                           Collectors.<Integer, Integer, Map<Integer, Collection<Integer>>, Map<Integer, Map<Integer, Collection<Integer>>>>groupingBy(classifier, TreeMap::new, groupingBy(classifier2, TreeMap::new, HashSet::new)),
+                           groupingBy(classifier, TreeMap::new, groupingBy(classifier2, TreeMap::new, HashSet::new)),
                            new GroupedMapAssertion<>(classifier, TreeMap.class,
                                                      new GroupedMapAssertion<>(classifier2, TreeMap.class,
                                                                                new CollectionAssertion<Integer>(HashSet.class, false))));
@@ -207,19 +207,19 @@
 
         // With explicit constructors
         exerciseTabulation(data,
-                           Collectors.<Integer, Integer, Map<Integer, Integer>>groupingReduce(classifier, TreeMap::new, Integer::sum),
+                           groupingReduce(classifier, () -> new TreeMap<>(), Integer::sum),
                            new GroupedMapAssertion<>(classifier, TreeMap.class,
                                                      new ReduceAssertion<>(Functions.identity(), Integer::sum)));
 
         // Single-level map-reduce
         exerciseTabulation(data,
-                           Collectors.<Integer, Integer, Integer>groupingReduce(classifier, mDoubler, Integer::sum),
+                           groupingReduce(classifier, mDoubler, Integer::sum),
                            new GroupedMapAssertion<>(classifier, HashMap.class,
                                                      new ReduceAssertion<>(mDoubler, Integer::sum)));
 
         // With explicit constructors
         exerciseTabulation(data,
-                           Collectors.<Integer, Integer, Integer, Map<Integer, Integer>>groupingReduce(classifier, TreeMap::new, mDoubler, Integer::sum),
+                           groupingReduce(classifier, TreeMap::new, mDoubler, Integer::sum),
                            new GroupedMapAssertion<>(classifier, TreeMap.class,
                                                      new ReduceAssertion<>(mDoubler, Integer::sum)));
     }