src/share/classes/java/util/stream/ReferencePipeline.java

Print this page
rev 7485 : 8009736: Comparator API cleanup
Reviewed-by:
Contributed-by: henry.jen@oracle.com


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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.Comparator;
  28 import java.util.Comparators;
  29 import java.util.Iterator;
  30 import java.util.Objects;
  31 import java.util.Optional;
  32 import java.util.Spliterator;
  33 import java.util.Spliterators;
  34 import java.util.function.BiConsumer;
  35 import java.util.function.BiFunction;
  36 import java.util.function.BinaryOperator;
  37 import java.util.function.Consumer;
  38 import java.util.function.DoubleConsumer;
  39 import java.util.function.Function;
  40 import java.util.function.IntConsumer;
  41 import java.util.function.IntFunction;
  42 import java.util.function.LongConsumer;
  43 import java.util.function.Predicate;
  44 import java.util.function.Supplier;
  45 import java.util.function.ToDoubleFunction;
  46 import java.util.function.ToIntFunction;
  47 import java.util.function.ToLongFunction;
  48 


 495         if (isParallel()
 496                 && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))
 497                 && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {
 498             R container = collector.resultSupplier().get();
 499             BiFunction<R, ? super P_OUT, R> accumulator = collector.accumulator();
 500             forEach(u -> accumulator.apply(container, u));
 501             return container;
 502         }
 503         return evaluate(ReduceOps.makeRef(collector));
 504     }
 505 
 506     @Override
 507     public final <R> R collect(Supplier<R> resultFactory,
 508                                BiConsumer<R, ? super P_OUT> accumulator,
 509                                BiConsumer<R, R> combiner) {
 510         return evaluate(ReduceOps.makeRef(resultFactory, accumulator, combiner));
 511     }
 512 
 513     @Override
 514     public final Optional<P_OUT> max(Comparator<? super P_OUT> comparator) {
 515         return reduce(Comparators.greaterOf(comparator));
 516     }
 517 
 518     @Override
 519     public final Optional<P_OUT> min(Comparator<? super P_OUT> comparator) {
 520         return reduce(Comparators.lesserOf(comparator));
 521 
 522     }
 523 
 524     @Override
 525     public final long count() {
 526         return mapToLong(e -> 1L).sum();
 527     }
 528 
 529 
 530     //
 531 
 532     /**
 533      * Source stage of a ReferencePipeline.
 534      *
 535      * @param <E_IN> type of elements in the upstream source
 536      * @param <E_OUT> type of elements in produced by this stage
 537      * @since 1.8
 538      */
 539     static class Head<E_IN, E_OUT> extends ReferencePipeline<E_IN, E_OUT> {
 540         /**




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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.Comparator;

  28 import java.util.Iterator;
  29 import java.util.Objects;
  30 import java.util.Optional;
  31 import java.util.Spliterator;
  32 import java.util.Spliterators;
  33 import java.util.function.BiConsumer;
  34 import java.util.function.BiFunction;
  35 import java.util.function.BinaryOperator;
  36 import java.util.function.Consumer;
  37 import java.util.function.DoubleConsumer;
  38 import java.util.function.Function;
  39 import java.util.function.IntConsumer;
  40 import java.util.function.IntFunction;
  41 import java.util.function.LongConsumer;
  42 import java.util.function.Predicate;
  43 import java.util.function.Supplier;
  44 import java.util.function.ToDoubleFunction;
  45 import java.util.function.ToIntFunction;
  46 import java.util.function.ToLongFunction;
  47 


 494         if (isParallel()
 495                 && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))
 496                 && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {
 497             R container = collector.resultSupplier().get();
 498             BiFunction<R, ? super P_OUT, R> accumulator = collector.accumulator();
 499             forEach(u -> accumulator.apply(container, u));
 500             return container;
 501         }
 502         return evaluate(ReduceOps.makeRef(collector));
 503     }
 504 
 505     @Override
 506     public final <R> R collect(Supplier<R> resultFactory,
 507                                BiConsumer<R, ? super P_OUT> accumulator,
 508                                BiConsumer<R, R> combiner) {
 509         return evaluate(ReduceOps.makeRef(resultFactory, accumulator, combiner));
 510     }
 511 
 512     @Override
 513     public final Optional<P_OUT> max(Comparator<? super P_OUT> comparator) {
 514         return reduce(BinaryOperator.maxBy(comparator));
 515     }
 516 
 517     @Override
 518     public final Optional<P_OUT> min(Comparator<? super P_OUT> comparator) {
 519         return reduce(BinaryOperator.minBy(comparator));
 520 
 521     }
 522 
 523     @Override
 524     public final long count() {
 525         return mapToLong(e -> 1L).sum();
 526     }
 527 
 528 
 529     //
 530 
 531     /**
 532      * Source stage of a ReferencePipeline.
 533      *
 534      * @param <E_IN> type of elements in the upstream source
 535      * @param <E_OUT> type of elements in produced by this stage
 536      * @since 1.8
 537      */
 538     static class Head<E_IN, E_OUT> extends ReferencePipeline<E_IN, E_OUT> {
 539         /**