< prev index next >

src/java.base/share/classes/java/util/stream/ReduceOps.java

Print this page
rev 53968 : enable Collector pre-sizing
rev 53969 : added map loadfactor and collector nullchecks


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.stream;
  26 
  27 import java.util.Objects;
  28 import java.util.Optional;
  29 import java.util.OptionalDouble;
  30 import java.util.OptionalInt;
  31 import java.util.OptionalLong;
  32 import java.util.Spliterator;
  33 import java.util.concurrent.CountedCompleter;
  34 import java.util.function.BiConsumer;
  35 import java.util.function.BiFunction;
  36 import java.util.function.BinaryOperator;
  37 import java.util.function.DoubleBinaryOperator;
  38 import java.util.function.IntBinaryOperator;
  39 import java.util.function.LongBinaryOperator;
  40 import java.util.function.ObjDoubleConsumer;
  41 import java.util.function.ObjIntConsumer;
  42 import java.util.function.ObjLongConsumer;
  43 import java.util.function.Supplier;
  44 
  45 /**
  46  * Factory for creating instances of {@code TerminalOp} that implement
  47  * reductions.
  48  *
  49  * @since 1.8
  50  */
  51 final class ReduceOps {
  52 
  53     private ReduceOps() { }
  54 
  55     /**
  56      * Constructs a {@code TerminalOp} that implements a functional reduce on
  57      * reference values.
  58      *
  59      * @param <T> the type of the input elements
  60      * @param <U> the type of the result
  61      * @param seed the identity element for the reduction
  62      * @param reducer the accumulating function that incorporates an additional
  63      *        input element into the result


 138         return new ReduceOp<T, Optional<T>, ReducingSink>(StreamShape.REFERENCE) {
 139             @Override
 140             public ReducingSink makeSink() {
 141                 return new ReducingSink();
 142             }
 143         };
 144     }
 145 
 146     /**
 147      * Constructs a {@code TerminalOp} that implements a mutable reduce on
 148      * reference values.
 149      *
 150      * @param <T> the type of the input elements
 151      * @param <I> the type of the intermediate reduction result
 152      * @param collector a {@code Collector} defining the reduction
 153      * @return a {@code ReduceOp} implementing the reduction
 154      */
 155     public static <T, I> TerminalOp<T, I>
 156     makeRef(Collector<? super T, I, ?> collector) {
 157         Supplier<I> supplier = Objects.requireNonNull(collector).supplier();

 158         BiConsumer<I, ? super T> accumulator = collector.accumulator();
 159         BinaryOperator<I> combiner = collector.combiner();
 160         class ReducingSink extends Box<I>
 161                 implements AccumulatingSink<T, I, ReducingSink> {
 162             @Override
 163             public void begin(long size) {

 164                 state = supplier.get();


 165             }
 166 
 167             @Override
 168             public void accept(T t) {
 169                 accumulator.accept(state, t);
 170             }
 171 
 172             @Override
 173             public void combine(ReducingSink other) {
 174                 state = combiner.apply(state, other.state);
 175             }
 176         }
 177         return new ReduceOp<T, I, ReducingSink>(StreamShape.REFERENCE) {
 178             @Override
 179             public ReducingSink makeSink() {
 180                 return new ReducingSink();
 181             }
 182 
 183             @Override
 184             public int getOpFlags() {




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.stream;
  26 
  27 import java.util.Objects;
  28 import java.util.Optional;
  29 import java.util.OptionalDouble;
  30 import java.util.OptionalInt;
  31 import java.util.OptionalLong;
  32 import java.util.Spliterator;
  33 import java.util.concurrent.CountedCompleter;
  34 import java.util.function.*;









  35 
  36 /**
  37  * Factory for creating instances of {@code TerminalOp} that implement
  38  * reductions.
  39  *
  40  * @since 1.8
  41  */
  42 final class ReduceOps {
  43 
  44     private ReduceOps() { }
  45 
  46     /**
  47      * Constructs a {@code TerminalOp} that implements a functional reduce on
  48      * reference values.
  49      *
  50      * @param <T> the type of the input elements
  51      * @param <U> the type of the result
  52      * @param seed the identity element for the reduction
  53      * @param reducer the accumulating function that incorporates an additional
  54      *        input element into the result


 129         return new ReduceOp<T, Optional<T>, ReducingSink>(StreamShape.REFERENCE) {
 130             @Override
 131             public ReducingSink makeSink() {
 132                 return new ReducingSink();
 133             }
 134         };
 135     }
 136 
 137     /**
 138      * Constructs a {@code TerminalOp} that implements a mutable reduce on
 139      * reference values.
 140      *
 141      * @param <T> the type of the input elements
 142      * @param <I> the type of the intermediate reduction result
 143      * @param collector a {@code Collector} defining the reduction
 144      * @return a {@code ReduceOp} implementing the reduction
 145      */
 146     public static <T, I> TerminalOp<T, I>
 147     makeRef(Collector<? super T, I, ?> collector) {
 148         Supplier<I> supplier = Objects.requireNonNull(collector).supplier();
 149         IntFunction<I> sizedSupplier = collector.sizedSupplier();
 150         BiConsumer<I, ? super T> accumulator = collector.accumulator();
 151         BinaryOperator<I> combiner = collector.combiner();
 152         class ReducingSink extends Box<I>
 153                 implements AccumulatingSink<T, I, ReducingSink> {
 154             @Override
 155             public void begin(long size) {
 156                 if (size < 0 || size > Integer.MAX_VALUE)
 157                     state = supplier.get();
 158                 else
 159                     state = sizedSupplier.apply((int) size);
 160             }
 161 
 162             @Override
 163             public void accept(T t) {
 164                 accumulator.accept(state, t);
 165             }
 166 
 167             @Override
 168             public void combine(ReducingSink other) {
 169                 state = combiner.apply(state, other.state);
 170             }
 171         }
 172         return new ReduceOp<T, I, ReducingSink>(StreamShape.REFERENCE) {
 173             @Override
 174             public ReducingSink makeSink() {
 175                 return new ReducingSink();
 176             }
 177 
 178             @Override
 179             public int getOpFlags() {


< prev index next >