changeset 14077:95fc5ae64549

Enhancement: use local classes in ReduceOps
author mcimadamore
date Mon, 04 Jul 2016 18:15:56 +0100
parents 62c3b03ef4ed
children 780c8eba356a
files src/java.base/share/classes/java/anyutil/stream/ReduceOps.java
diffstat 1 files changed, 26 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/anyutil/stream/ReduceOps.java	Fri Jul 01 14:51:23 2016 -0400
+++ b/src/java.base/share/classes/java/anyutil/stream/ReduceOps.java	Mon Jul 04 18:15:56 2016 +0100
@@ -62,23 +62,10 @@
          BinaryOperator<R> combiner) {
         Objects.requireNonNull(reducer);
         Objects.requireNonNull(combiner);
-        return new ReduceOp<>(() -> new ReducingSink$1<>(seed, reducer, combiner));
-    }
 
-    // @@@Valhalla: temporarily desugar local classes
-        static class ReducingSink$1<any T, any R> implements AccumulatingSink<T, R, ReducingSink$1<T, R>> {
+        class ReducingSink implements AccumulatingSink<T, R, ReducingSink> {
             R state;
 
-            R seed;
-            BiFunction<R, ? super T, R> reducer;
-            BinaryOperator<R> combiner;
-
-            ReducingSink$1(R seed, BiFunction<R, ? super T, R> reducer, BinaryOperator<R> combiner) {
-                this.seed = seed;
-                this.reducer = reducer;
-                this.combiner = combiner;
-            }
-
             public R get() {
                 return state;
             }
@@ -94,11 +81,14 @@
             }
 
             @Override
-            public void combine(ReducingSink$1<T, R> other) {
+            public void combine(ReducingSink other) {
                 state = combiner.apply(state, other.state);
             }
         }
 
+        return new ReduceOp<>(ReducingSink::new);
+    }
+
 
     /**
      * Constructs a {@code TerminalOp} that implements a functional reduce on
@@ -117,20 +107,9 @@
     make(T seed,
          BinaryOperator<T> combiner) {
         Objects.requireNonNull(combiner);
-        return new ReduceOp<>(() -> new ReducingSink$2<>(seed, combiner));
-    }
-    // @@@Valhalla: temporarily desugar local classes
-        static class ReducingSink$2<any T> implements AccumulatingSink<T, T, ReducingSink$2<T>> {
+        class ReducingSink implements AccumulatingSink<T, T, ReducingSink> {
             T state;
 
-            T seed;
-            BinaryOperator<T> combiner;
-
-            ReducingSink$2(T seed, BinaryOperator<T> combiner) {
-                this.seed = seed;
-                this.combiner = combiner;
-            }
-
             public T get() {
                 return state;
             }
@@ -146,10 +125,12 @@
             }
 
             @Override
-            public void combine(ReducingSink$2<T> other) {
+            public void combine(ReducingSink other) {
                 state = combiner.apply(state, other.state);
             }
         }
+        return new ReduceOp<>(ReducingSink::new);
+    }
 
     /**
      * Constructs a {@code TerminalOp} that implements a functional reduce on
@@ -162,19 +143,10 @@
     public static <any E_SRC, any T> TerminalOp<E_SRC, T, Optional<T>>
     make(BinaryOperator<T> operator) {
         Objects.requireNonNull(operator);
-        return new ReduceOp<>(()->new ReducingSink$3<>(operator));
-    }
-    // @@@Valhalla: temporarily desugar local classes
-        static class ReducingSink$3<any T> implements AccumulatingSink<T, Optional<T>, ReducingSink$3<T>> {
+        class ReducingSink implements AccumulatingSink<T, Optional<T>, ReducingSink> {
             private boolean empty;
             private T state;
 
-            BinaryOperator<T> operator;
-
-            ReducingSink$3(BinaryOperator<T> operator) {
-                this.operator = operator;
-            }
-
             public void begin(long size) {
                 empty = true;
                 state = T.default;
@@ -196,12 +168,13 @@
             }
 
             @Override
-            public void combine(ReducingSink$3<T> other) {
+            public void combine(ReducingSink other) {
                 if (!other.empty)
                     accept(other.state);
             }
         }
-
+        return new ReduceOp<>(ReducingSink::new);
+    }
 
     /**
      * Constructs a {@code TerminalOp} that implements a mutable reduce on
@@ -217,22 +190,9 @@
         Supplier<I> supplier = Objects.requireNonNull(collector).supplier();
         BiConsumer<I, ? super T> accumulator = collector.accumulator();
         BinaryOperator<I> combiner = collector.combiner();
-        return new ReduceOp$1<>(() -> new ReducingSink$4<>(supplier, accumulator, combiner), collector);
-    }
-    // @@@Valhalla: temporarily desugar local classes
-        static class ReducingSink$4<any T, I> implements AccumulatingSink<T, I, ReducingSink$4<T, I>> {
+        class ReducingSink implements AccumulatingSink<T, I, ReducingSink> {
             I state;
 
-            Supplier<I> supplier;
-            BiConsumer<I, ? super T> accumulator;
-            BinaryOperator<I> combiner;
-
-            ReducingSink$4(Supplier<I> supplier, BiConsumer<I, ? super T> accumulator, BinaryOperator<I> combiner) {
-                this.supplier = supplier;
-                this.accumulator = accumulator;
-                this.combiner = combiner;
-            }
-
             @Override
             public I get() {
                 return state;
@@ -249,23 +209,20 @@
             }
 
             @Override
-            public void combine(ReducingSink$4<T, I> other) {
+            public void combine(ReducingSink other) {
                 state = combiner.apply(state, other.state);
             }
         }
+        // @@@Valhalla: can this be simplified further?
+        class ReduceOp$1<any E_SRC> extends ReduceOp<E_SRC, T, I, ReducingSink> {
 
-        static class ReduceOp$1<any E_SRC, any T, I> extends ReduceOp<E_SRC, T, I, ReducingSink$4<T, I>> {
-
-            Collector<? super T, I, ?> collector;
-
-            ReduceOp$1(Supplier<ReducingSink$4<T,I>> sinkFactory, Collector<? super T, I, ?> collector) {
+            ReduceOp$1(Supplier<ReducingSink> sinkFactory) {
                 super(sinkFactory);
-                this.collector = collector;
             }
 
             @Override
             public <any NEWSRC> TerminalOp<NEWSRC, T, I> reparent() {
-                return new ReduceOp$1<>(sinkFactory, collector);
+                return new ReduceOp$1<>(sinkFactory);
             }
 
             @Override
@@ -274,7 +231,9 @@
                        ? StreamOpFlag.NOT_ORDERED
                        : 0;
             }
-        };
+        }
+        return new ReduceOp$1<>(ReducingSink::new);
+    }
 
     /**
      * Constructs a {@code TerminalOp} that implements a mutable reduce on
@@ -295,23 +254,9 @@
         Objects.requireNonNull(seedFactory);
         Objects.requireNonNull(accumulator);
         Objects.requireNonNull(combiner);
-
-        return new ReduceOp<>(() -> new ReducingSink$5<>(seedFactory, accumulator, combiner));
-    }
-    // @@@Valhalla: temporarily desugar local classes
-        static class ReducingSink$5<any T, any R> implements AccumulatingSink<T, R, ReducingSink$5<T, R>> {
+        class ReducingSink implements AccumulatingSink<T, R, ReducingSink> {
             R state;
 
-            Supplier<R> seedFactory;
-            BiConsumer<R, ? super T> accumulator;
-            BiConsumer<R,R> combiner;
-
-            ReducingSink$5(Supplier<R> seedFactory, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) {
-                this.seedFactory = seedFactory;
-                this.accumulator = accumulator;
-                this.combiner = combiner;
-            }
-
             public R get() {
                 return state;
             }
@@ -327,10 +272,12 @@
             }
 
             @Override
-            public void combine(ReducingSink$5<T, R> other) {
+            public void combine(ReducingSink other) {
                 combiner.accept(state, other.state);
             }
         }
+        return new ReduceOp<>(ReducingSink::new);
+    }
 
     /**
      * A type of {@code TerminalSink} that implements an associative reducing