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
466 if (isParallel()
467 && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))
468 && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {
469 R container = collector.resultSupplier().get();
470 BiFunction<R, ? super P_OUT, R> accumulator = collector.accumulator();
471 forEach(u -> accumulator.apply(container, u));
472 return container;
473 }
474 return evaluate(ReduceOps.makeRef(collector));
475 }
476
477 @Override
478 public final <R> R collect(Supplier<R> resultFactory,
479 BiConsumer<R, ? super P_OUT> accumulator,
480 BiConsumer<R, R> combiner) {
481 return evaluate(ReduceOps.makeRef(resultFactory, accumulator, combiner));
482 }
483
484 @Override
485 public final Optional<P_OUT> max(Comparator<? super P_OUT> comparator) {
486 return reduce(Comparators.greaterOf(comparator));
487 }
488
489 @Override
490 public final Optional<P_OUT> min(Comparator<? super P_OUT> comparator) {
491 return reduce(Comparators.lesserOf(comparator));
492
493 }
494
495 @Override
496 public final long count() {
497 return mapToLong(e -> 1L).sum();
498 }
499
500
501 //
502
503 /**
504 * Source stage of a ReferencePipeline.
505 *
506 * @param <E_IN> type of elements in the upstream source
507 * @param <E_OUT> type of elements in produced by this stage
508 * @since 1.8
509 */
510 static class Head<E_IN, E_OUT> extends ReferencePipeline<E_IN, E_OUT> {
511 /**
|
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
465 if (isParallel()
466 && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))
467 && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {
468 R container = collector.resultSupplier().get();
469 BiFunction<R, ? super P_OUT, R> accumulator = collector.accumulator();
470 forEach(u -> accumulator.apply(container, u));
471 return container;
472 }
473 return evaluate(ReduceOps.makeRef(collector));
474 }
475
476 @Override
477 public final <R> R collect(Supplier<R> resultFactory,
478 BiConsumer<R, ? super P_OUT> accumulator,
479 BiConsumer<R, R> combiner) {
480 return evaluate(ReduceOps.makeRef(resultFactory, accumulator, combiner));
481 }
482
483 @Override
484 public final Optional<P_OUT> max(Comparator<? super P_OUT> comparator) {
485 return reduce(BinaryOperator.maxBy(comparator));
486 }
487
488 @Override
489 public final Optional<P_OUT> min(Comparator<? super P_OUT> comparator) {
490 return reduce(BinaryOperator.minBy(comparator));
491
492 }
493
494 @Override
495 public final long count() {
496 return mapToLong(e -> 1L).sum();
497 }
498
499
500 //
501
502 /**
503 * Source stage of a ReferencePipeline.
504 *
505 * @param <E_IN> type of elements in the upstream source
506 * @param <E_OUT> type of elements in produced by this stage
507 * @since 1.8
508 */
509 static class Head<E_IN, E_OUT> extends ReferencePipeline<E_IN, E_OUT> {
510 /**
|