changeset 7526:1ad34774128e

Inline away XxxBox classes in ReduceOp
author briangoetz
date Fri, 22 Feb 2013 17:51:40 -0500
parents f2d99a1cee33
children 50fa347587d2
files src/share/classes/java/util/stream/ReduceOp.java
diffstat 1 files changed, 79 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/ReduceOp.java	Fri Feb 22 16:59:34 2013 -0500
+++ b/src/share/classes/java/util/stream/ReduceOp.java	Fri Feb 22 17:51:40 2013 -0500
@@ -100,120 +100,6 @@
     }
 
     /**
-     * State box for an optional state element, used as a base class for {@code AccumulatingSink} instances
-     * @param <U> The type of the state element
-     */
-    private static abstract class OptionalBox<U> {
-        protected boolean empty;
-        protected U state;
-
-        public void begin(long size) {
-            empty = true;
-            state = null;
-        }
-
-        public Optional<U> get() {
-            return empty ? Optional.empty() : Optional.of(state);
-        }
-    }
-
-    /**
-     * State box for an int state element, used as a base class for {@code AccumulatingSink} instances
-     */
-    private static abstract class IntBox {
-        protected int state;
-
-        public void begin(long size) {
-            state = 0;
-        }
-
-        public Integer get() {
-            return state;
-        }
-    }
-
-    /**
-     * State box for an optional int state element, used as a base class for {@code AccumulatingSink} instances
-     */
-    private static abstract class OptionalIntBox {
-        protected boolean empty;
-        protected int state;
-
-        public void begin(long size) {
-            empty = true;
-            state = 0;
-        }
-
-        public OptionalInt get() {
-            return empty ? OptionalInt.empty() : OptionalInt.of(state);
-        }
-    }
-
-    /**
-     * State box for a long state element, used as a base class for {@code AccumulatingSink} instances
-     */
-    private static abstract class LongBox {
-        protected long state;
-
-        public void begin(long size) {
-            state = 0;
-        }
-
-        public Long get() {
-            return state;
-        }
-    }
-
-    /**
-     * State box for an optional long state element, used as a base class for {@code AccumulatingSink} instances
-     */
-    private static abstract class OptionalLongBox {
-        protected boolean empty;
-        protected long state;
-
-        public void begin(long size) {
-            empty = true;
-            state = 0;
-        }
-
-        public OptionalLong get() {
-            return empty ? OptionalLong.empty() : OptionalLong.of(state);
-        }
-    }
-
-    /**
-     * State box for a double state element, used as a base class for {@code AccumulatingSink} instances
-     */
-    private static abstract class DoubleBox {
-        protected double state;
-
-        public void begin(long size) {
-            state = 0;
-        }
-
-        public Double get() {
-            return state;
-        }
-    }
-
-    /**
-     * State box for an optional double state element, used as a base class for {@code AccumulatingSink} instances
-     */
-    private static abstract class OptionalDoubleBox {
-        protected boolean empty;
-        protected double state;
-
-        public void begin(long size) {
-            empty = true;
-            state = 0;
-        }
-
-        public OptionalDouble get() {
-            return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
-        }
-    }
-
-    /**
      * Construct a {@code ReduceOp} that implements a functional reduce on reference values
      * @param seed The identity element for the reduction
      * @param reducer The accumulating function that incorporates an additional input element into the result
@@ -256,7 +142,14 @@
      */
     public static<T> TerminalOp<T, Optional<T>>
     makeRef(BinaryOperator<T> operator) {
-        class ReducingSink extends OptionalBox<T> implements OpUtils.AccumulatingSink<T, Optional<T>, ReducingSink> {
+        class ReducingSink implements OpUtils.AccumulatingSink<T, Optional<T>, ReducingSink> {
+            private boolean empty;
+            private T state;
+
+            public void begin(long size) {
+                empty = true;
+                state = null;
+            }
 
             @Override
             public void accept(T t) {
@@ -269,6 +162,11 @@
             }
 
             @Override
+            public Optional<T> get() {
+                return empty ? Optional.empty() : Optional.of(state);
+            }
+
+            @Override
             public void combine(ReducingSink other) {
                 if (!other.empty)
                     accept(other.state);
@@ -355,7 +253,9 @@
      */
     public static TerminalOp<Integer, Integer>
     makeInt(int identity, IntBinaryOperator operator) {
-        class ReducingSink extends IntBox implements OpUtils.AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
+        class ReducingSink implements OpUtils.AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
+            private int state;
+
             @Override
             public void begin(long size) {
                 state = identity;
@@ -367,6 +267,11 @@
             }
 
             @Override
+            public Integer get() {
+                return state;
+            }
+
+            @Override
             public void combine(ReducingSink other) {
                 accept(other.state);
             }
@@ -387,7 +292,15 @@
      */
     public static TerminalOp<Integer, OptionalInt>
     makeInt(IntBinaryOperator operator) {
-        class ReducingSink extends OptionalIntBox implements OpUtils.AccumulatingSink<Integer, OptionalInt, ReducingSink>, Sink.OfInt {
+        class ReducingSink implements OpUtils.AccumulatingSink<Integer, OptionalInt, ReducingSink>, Sink.OfInt {
+            private boolean empty;
+            private int state;
+
+            public void begin(long size) {
+                empty = true;
+                state = 0;
+            }
+
             @Override
             public void accept(int t) {
                 if (empty) {
@@ -400,6 +313,11 @@
             }
 
             @Override
+            public OptionalInt get() {
+                return empty ? OptionalInt.empty() : OptionalInt.of(state);
+            }
+
+            @Override
             public void combine(ReducingSink other) {
                 if (!other.empty)
                     accept(other.state);
@@ -458,7 +376,9 @@
      */
     public static TerminalOp<Long, Long>
     makeLong(long identity, LongBinaryOperator operator) {
-        class ReducingSink extends LongBox implements OpUtils.AccumulatingSink<Long, Long, ReducingSink>, Sink.OfLong {
+        class ReducingSink implements OpUtils.AccumulatingSink<Long, Long, ReducingSink>, Sink.OfLong {
+            private long state;
+
             @Override
             public void begin(long size) {
                 state = identity;
@@ -470,6 +390,11 @@
             }
 
             @Override
+            public Long get() {
+                return state;
+            }
+
+            @Override
             public void combine(ReducingSink other) {
                 accept(other.state);
             }
@@ -489,7 +414,15 @@
      */
     public static TerminalOp<Long, OptionalLong>
     makeLong(LongBinaryOperator operator) {
-        class ReducingSink extends OptionalLongBox implements OpUtils.AccumulatingSink<Long, OptionalLong, ReducingSink>, Sink.OfLong {
+        class ReducingSink implements OpUtils.AccumulatingSink<Long, OptionalLong, ReducingSink>, Sink.OfLong {
+            private boolean empty;
+            private long state;
+
+            public void begin(long size) {
+                empty = true;
+                state = 0;
+            }
+
             @Override
             public void accept(long t) {
                 if (empty) {
@@ -502,6 +435,11 @@
             }
 
             @Override
+            public OptionalLong get() {
+                return empty ? OptionalLong.empty() : OptionalLong.of(state);
+            }
+
+            @Override
             public void combine(ReducingSink other) {
                 if (!other.empty)
                     accept(other.state);
@@ -560,7 +498,9 @@
      */
     public static TerminalOp<Double, Double>
     makeDouble(double identity, DoubleBinaryOperator operator) {
-        class ReducingSink extends DoubleBox implements OpUtils.AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
+        class ReducingSink implements OpUtils.AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
+            private double state;
+
             @Override
             public void begin(long size) {
                 state = identity;
@@ -572,6 +512,11 @@
             }
 
             @Override
+            public Double get() {
+                return state;
+            }
+
+            @Override
             public void combine(ReducingSink other) {
                 accept(other.state);
             }
@@ -592,7 +537,15 @@
      */
     public static TerminalOp<Double, OptionalDouble>
     makeDouble(DoubleBinaryOperator operator) {
-        class ReducingSink extends OptionalDoubleBox implements OpUtils.AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
+        class ReducingSink implements OpUtils.AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
+            private boolean empty;
+            private double state;
+
+            public void begin(long size) {
+                empty = true;
+                state = 0;
+            }
+
             @Override
             public void accept(double t) {
                 if (empty) {
@@ -605,6 +558,11 @@
             }
 
             @Override
+            public OptionalDouble get() {
+                return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
+            }
+
+            @Override
             public void combine(ReducingSink other) {
                 if (!other.empty)
                     accept(other.state);