jdk/src/share/classes/java/util/stream/Collectors.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 22101 231247ddf41a
child 24123 7c35c9e15f8e
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     1
/*
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     4
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    10
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    15
 * accompanied this code).
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    16
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    20
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    23
 * questions.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    24
 */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    25
package java.util.stream;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    26
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    27
import java.util.AbstractMap;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    28
import java.util.AbstractSet;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    29
import java.util.ArrayList;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    30
import java.util.Arrays;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    31
import java.util.Collection;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    32
import java.util.Collections;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    33
import java.util.Comparator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    34
import java.util.DoubleSummaryStatistics;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    35
import java.util.EnumSet;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    36
import java.util.HashMap;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    37
import java.util.HashSet;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    38
import java.util.IntSummaryStatistics;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    39
import java.util.Iterator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    40
import java.util.List;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    41
import java.util.LongSummaryStatistics;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    42
import java.util.Map;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    43
import java.util.Objects;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    44
import java.util.Optional;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    45
import java.util.Set;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    46
import java.util.StringJoiner;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    47
import java.util.concurrent.ConcurrentHashMap;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    48
import java.util.concurrent.ConcurrentMap;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    49
import java.util.function.BiConsumer;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    50
import java.util.function.BiFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    51
import java.util.function.BinaryOperator;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    52
import java.util.function.Consumer;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    53
import java.util.function.Function;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    54
import java.util.function.Predicate;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    55
import java.util.function.Supplier;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    56
import java.util.function.ToDoubleFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    57
import java.util.function.ToIntFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    58
import java.util.function.ToLongFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    59
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    60
/**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    61
 * Implementations of {@link Collector} that implement various useful reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    62
 * operations, such as accumulating elements into collections, summarizing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    63
 * elements according to various criteria, etc.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    64
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    65
 * <p>The following are examples of using the predefined collectors to perform
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    66
 * common mutable reduction tasks:
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    67
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    68
 * <pre>{@code
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    69
 *     // Accumulate names into a List
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    70
 *     List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    71
 *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    72
 *     // Accumulate names into a TreeSet
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    73
 *     Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    74
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    75
 *     // Convert elements to strings and concatenate them, separated by commas
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    76
 *     String joined = things.stream()
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    77
 *                           .map(Object::toString)
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    78
 *                           .collect(Collectors.joining(", "));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    79
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    80
 *     // Compute sum of salaries of employee
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    81
 *     int total = employees.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    82
 *                          .collect(Collectors.summingInt(Employee::getSalary)));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    83
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    84
 *     // Group employees by department
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    85
 *     Map<Department, List<Employee>> byDept
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    86
 *         = employees.stream()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    87
 *                    .collect(Collectors.groupingBy(Employee::getDepartment));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    88
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    89
 *     // Compute sum of salaries by department
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    90
 *     Map<Department, Integer> totalByDept
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    91
 *         = employees.stream()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    92
 *                    .collect(Collectors.groupingBy(Employee::getDepartment,
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
    93
 *                                                   Collectors.summingInt(Employee::getSalary)));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    94
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    95
 *     // Partition students into passing and failing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    96
 *     Map<Boolean, List<Student>> passingFailing =
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    97
 *         students.stream()
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
    98
 *                 .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    99
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   100
 * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   101
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   102
 * @since 1.8
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   103
 */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   104
public final class Collectors {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   105
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   106
    static final Set<Collector.Characteristics> CH_CONCURRENT_ID
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   107
            = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.CONCURRENT,
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   108
                                                     Collector.Characteristics.UNORDERED,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   109
                                                     Collector.Characteristics.IDENTITY_FINISH));
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   110
    static final Set<Collector.Characteristics> CH_CONCURRENT_NOID
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   111
            = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.CONCURRENT,
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   112
                                                     Collector.Characteristics.UNORDERED));
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   113
    static final Set<Collector.Characteristics> CH_ID
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   114
            = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   115
    static final Set<Collector.Characteristics> CH_UNORDERED_ID
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   116
            = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   117
                                                     Collector.Characteristics.IDENTITY_FINISH));
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   118
    static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet();
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   119
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   120
    private Collectors() { }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   121
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   122
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   123
     * Returns a merge function, suitable for use in
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   124
     * {@link Map#merge(Object, Object, BiFunction) Map.merge()} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   125
     * {@link #toMap(Function, Function, BinaryOperator) toMap()}, which always
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   126
     * throws {@code IllegalStateException}.  This can be used to enforce the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   127
     * assumption that the elements being collected are distinct.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   128
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   129
     * @param <T> the type of input arguments to the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   130
     * @return a merge function which always throw {@code IllegalStateException}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   131
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   132
    private static <T> BinaryOperator<T> throwingMerger() {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   133
        return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   134
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   135
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
   136
    @SuppressWarnings("unchecked")
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
   137
    private static <I, R> Function<I, R> castingIdentity() {
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
   138
        return i -> (R) i;
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
   139
    }
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
   140
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   141
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   142
     * Simple implementation class for {@code Collector}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   143
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   144
     * @param <T> the type of elements to be collected
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   145
     * @param <R> the type of the result
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   146
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   147
    static class CollectorImpl<T, A, R> implements Collector<T, A, R> {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   148
        private final Supplier<A> supplier;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   149
        private final BiConsumer<A, T> accumulator;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   150
        private final BinaryOperator<A> combiner;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   151
        private final Function<A, R> finisher;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   152
        private final Set<Characteristics> characteristics;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   153
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   154
        CollectorImpl(Supplier<A> supplier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   155
                      BiConsumer<A, T> accumulator,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   156
                      BinaryOperator<A> combiner,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   157
                      Function<A,R> finisher,
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   158
                      Set<Characteristics> characteristics) {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   159
            this.supplier = supplier;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   160
            this.accumulator = accumulator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   161
            this.combiner = combiner;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   162
            this.finisher = finisher;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   163
            this.characteristics = characteristics;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   164
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   165
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   166
        CollectorImpl(Supplier<A> supplier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   167
                      BiConsumer<A, T> accumulator,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   168
                      BinaryOperator<A> combiner,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   169
                      Set<Characteristics> characteristics) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
   170
            this(supplier, accumulator, combiner, castingIdentity(), characteristics);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   171
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   172
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   173
        @Override
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   174
        public BiConsumer<A, T> accumulator() {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   175
            return accumulator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   176
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   177
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   178
        @Override
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   179
        public Supplier<A> supplier() {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   180
            return supplier;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   181
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   182
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   183
        @Override
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   184
        public BinaryOperator<A> combiner() {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   185
            return combiner;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   186
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   187
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   188
        @Override
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   189
        public Function<A, R> finisher() {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   190
            return finisher;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   191
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   192
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   193
        @Override
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   194
        public Set<Characteristics> characteristics() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   195
            return characteristics;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   196
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   197
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   198
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   199
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   200
     * Returns a {@code Collector} that accumulates the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   201
     * new {@code Collection}, in encounter order.  The {@code Collection} is
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   202
     * created by the provided factory.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   203
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   204
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   205
     * @param <C> the type of the resulting {@code Collection}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   206
     * @param collectionFactory a {@code Supplier} which returns a new, empty
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   207
     * {@code Collection} of the appropriate type
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   208
     * @return a {@code Collector} which collects all the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   209
     * {@code Collection}, in encounter order
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   210
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   211
    public static <T, C extends Collection<T>>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   212
    Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
   213
        return new CollectorImpl<>(collectionFactory, Collection<T>::add,
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   214
                                   (r1, r2) -> { r1.addAll(r2); return r1; },
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   215
                                   CH_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   216
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   217
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   218
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   219
     * Returns a {@code Collector} that accumulates the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   220
     * new {@code List}. There are no guarantees on the type, mutability,
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
   221
     * serializability, or thread-safety of the {@code List} returned; if more
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
   222
     * control over the returned {@code List} is required, use {@link #toCollection(Supplier)}.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   223
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   224
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   225
     * @return a {@code Collector} which collects all the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   226
     * {@code List}, in encounter order
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   227
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   228
    public static <T>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   229
    Collector<T, ?, List<T>> toList() {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   230
        return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   231
                                   (left, right) -> { left.addAll(right); return left; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   232
                                   CH_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   233
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   234
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   235
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   236
     * Returns a {@code Collector} that accumulates the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   237
     * new {@code Set}. There are no guarantees on the type, mutability,
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
   238
     * serializability, or thread-safety of the {@code Set} returned; if more
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
   239
     * control over the returned {@code Set} is required, use
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
   240
     * {@link #toCollection(Supplier)}.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   241
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   242
     * <p>This is an {@link Collector.Characteristics#UNORDERED unordered}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   243
     * Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   244
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   245
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   246
     * @return a {@code Collector} which collects all the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   247
     * {@code Set}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   248
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   249
    public static <T>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   250
    Collector<T, ?, Set<T>> toSet() {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   251
        return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   252
                                   (left, right) -> { left.addAll(right); return left; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   253
                                   CH_UNORDERED_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   254
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   255
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   256
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   257
     * Returns a {@code Collector} that concatenates the input elements into a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   258
     * {@code String}, in encounter order.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   259
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   260
     * @return a {@code Collector} that concatenates the input elements into a
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   261
     * {@code String}, in encounter order
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   262
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   263
    public static Collector<CharSequence, ?, String> joining() {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   264
        return new CollectorImpl<CharSequence, StringBuilder, String>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   265
                StringBuilder::new, StringBuilder::append,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   266
                (r1, r2) -> { r1.append(r2); return r1; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   267
                StringBuilder::toString, CH_NOID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   268
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   269
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   270
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   271
     * Returns a {@code Collector} that concatenates the input elements,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   272
     * separated by the specified delimiter, in encounter order.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   273
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   274
     * @param delimiter the delimiter to be used between each element
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   275
     * @return A {@code Collector} which concatenates CharSequence elements,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   276
     * separated by the specified delimiter, in encounter order
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   277
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   278
    public static Collector<CharSequence, ?, String> joining(CharSequence delimiter) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   279
        return joining(delimiter, "", "");
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   280
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   281
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   282
    /**
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   283
     * Returns a {@code Collector} that concatenates the input elements,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   284
     * separated by the specified delimiter, with the specified prefix and
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   285
     * suffix, in encounter order.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   286
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   287
     * @param delimiter the delimiter to be used between each element
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   288
     * @param  prefix the sequence of characters to be used at the beginning
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   289
     *                of the joined result
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   290
     * @param  suffix the sequence of characters to be used at the end
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   291
     *                of the joined result
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   292
     * @return A {@code Collector} which concatenates CharSequence elements,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   293
     * separated by the specified delimiter, in encounter order
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   294
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   295
    public static Collector<CharSequence, ?, String> joining(CharSequence delimiter,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   296
                                                             CharSequence prefix,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   297
                                                             CharSequence suffix) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   298
        return new CollectorImpl<>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   299
                () -> new StringJoiner(delimiter, prefix, suffix),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   300
                StringJoiner::add, StringJoiner::merge,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   301
                StringJoiner::toString, CH_NOID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   302
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   303
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   304
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   305
     * {@code BinaryOperator<Map>} that merges the contents of its right
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   306
     * argument into its left argument, using the provided merge function to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   307
     * handle duplicate keys.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   308
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   309
     * @param <K> type of the map keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   310
     * @param <V> type of the map values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   311
     * @param <M> type of the map
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   312
     * @param mergeFunction A merge function suitable for
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   313
     * {@link Map#merge(Object, Object, BiFunction) Map.merge()}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   314
     * @return a merge function for two maps
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   315
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   316
    private static <K, V, M extends Map<K,V>>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   317
    BinaryOperator<M> mapMerger(BinaryOperator<V> mergeFunction) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   318
        return (m1, m2) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   319
            for (Map.Entry<K,V> e : m2.entrySet())
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   320
                m1.merge(e.getKey(), e.getValue(), mergeFunction);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   321
            return m1;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   322
        };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   323
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   324
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   325
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   326
     * Adapts a {@code Collector} accepting elements of type {@code U} to one
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   327
     * accepting elements of type {@code T} by applying a mapping function to
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   328
     * each input element before accumulation.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   329
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   330
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   331
     * The {@code mapping()} collectors are most useful when used in a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   332
     * multi-level reduction, such as downstream of a {@code groupingBy} or
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   333
     * {@code partitioningBy}.  For example, given a stream of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   334
     * {@code Person}, to accumulate the set of last names in each city:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   335
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   336
     *     Map<City, Set<String>> lastNamesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   337
     *         = people.stream().collect(groupingBy(Person::getCity,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   338
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   339
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   340
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   341
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   342
     * @param <U> type of elements accepted by downstream collector
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   343
     * @param <A> intermediate accumulation type of the downstream collector
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   344
     * @param <R> result type of collector
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   345
     * @param mapper a function to be applied to the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   346
     * @param downstream a collector which will accept mapped values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   347
     * @return a collector which applies the mapping function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   348
     * elements and provides the mapped results to the downstream collector
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   349
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   350
    public static <T, U, A, R>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   351
    Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   352
                               Collector<? super U, A, R> downstream) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   353
        BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   354
        return new CollectorImpl<>(downstream.supplier(),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   355
                                   (r, t) -> downstreamAccumulator.accept(r, mapper.apply(t)),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   356
                                   downstream.combiner(), downstream.finisher(),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   357
                                   downstream.characteristics());
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   358
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   359
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   360
    /**
19420
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   361
     * Adapts a {@code Collector} to perform an additional finishing
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   362
     * transformation.  For example, one could adapt the {@link #toList()}
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   363
     * collector to always produce an immutable list with:
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   364
     * <pre>{@code
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   365
     *     List<String> people
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   366
     *         = people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   367
     * }</pre>
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   368
     *
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   369
     * @param <T> the type of the input elements
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   370
     * @param <A> intermediate accumulation type of the downstream collector
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   371
     * @param <R> result type of the downstream collector
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   372
     * @param <RR> result type of the resulting collector
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   373
     * @param downstream a collector
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   374
     * @param finisher a function to be applied to the final result of the downstream collector
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   375
     * @return a collector which performs the action of the downstream collector,
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   376
     * followed by an additional finishing step
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   377
     */
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   378
    public static<T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream,
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   379
                                                                Function<R,RR> finisher) {
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   380
        Set<Collector.Characteristics> characteristics = downstream.characteristics();
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   381
        if (characteristics.contains(Collector.Characteristics.IDENTITY_FINISH)) {
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   382
            if (characteristics.size() == 1)
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   383
                characteristics = Collectors.CH_NOID;
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   384
            else {
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   385
                characteristics = EnumSet.copyOf(characteristics);
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   386
                characteristics.remove(Collector.Characteristics.IDENTITY_FINISH);
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   387
                characteristics = Collections.unmodifiableSet(characteristics);
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   388
            }
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   389
        }
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   390
        return new CollectorImpl<>(downstream.supplier(),
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   391
                                   downstream.accumulator(),
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   392
                                   downstream.combiner(),
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   393
                                   downstream.finisher().andThen(finisher),
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   394
                                   characteristics);
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   395
    }
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   396
a95fae98f735 8019401: Collectors.collectingAndThen
briangoetz
parents: 19214
diff changeset
   397
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   398
     * Returns a {@code Collector} accepting elements of type {@code T} that
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   399
     * counts the number of input elements.  If no elements are present, the
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   400
     * result is 0.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   401
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   402
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   403
     * This produces a result equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   404
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   405
     *     reducing(0L, e -> 1L, Long::sum)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   406
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   407
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   408
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   409
     * @return a {@code Collector} that counts the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   410
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   411
    public static <T> Collector<T, ?, Long>
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   412
    counting() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   413
        return reducing(0L, e -> 1L, Long::sum);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   414
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   415
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   416
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   417
     * Returns a {@code Collector} that produces the minimal element according
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   418
     * to a given {@code Comparator}, described as an {@code Optional<T>}.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   419
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   420
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   421
     * This produces a result equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   422
     * <pre>{@code
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17196
diff changeset
   423
     *     reducing(BinaryOperator.minBy(comparator))
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   424
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   425
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   426
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   427
     * @param comparator a {@code Comparator} for comparing elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   428
     * @return a {@code Collector} that produces the minimal value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   429
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   430
    public static <T> Collector<T, ?, Optional<T>>
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   431
    minBy(Comparator<? super T> comparator) {
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17196
diff changeset
   432
        return reducing(BinaryOperator.minBy(comparator));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   433
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   434
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   435
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   436
     * Returns a {@code Collector} that produces the maximal element according
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   437
     * to a given {@code Comparator}, described as an {@code Optional<T>}.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   438
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   439
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   440
     * This produces a result equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   441
     * <pre>{@code
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17196
diff changeset
   442
     *     reducing(BinaryOperator.maxBy(comparator))
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   443
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   444
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   445
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   446
     * @param comparator a {@code Comparator} for comparing elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   447
     * @return a {@code Collector} that produces the maximal value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   448
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   449
    public static <T> Collector<T, ?, Optional<T>>
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   450
    maxBy(Comparator<? super T> comparator) {
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17196
diff changeset
   451
        return reducing(BinaryOperator.maxBy(comparator));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   452
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   453
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   454
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   455
     * Returns a {@code Collector} that produces the sum of a integer-valued
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   456
     * function applied to the input elements.  If no elements are present,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   457
     * the result is 0.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   458
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   459
     * @param <T> the type of the input elements
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   460
     * @param mapper a function extracting the property to be summed
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   461
     * @return a {@code Collector} that produces the sum of a derived property
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   462
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   463
    public static <T> Collector<T, ?, Integer>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   464
    summingInt(ToIntFunction<? super T> mapper) {
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   465
        return new CollectorImpl<>(
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   466
                () -> new int[1],
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   467
                (a, t) -> { a[0] += mapper.applyAsInt(t); },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   468
                (a, b) -> { a[0] += b[0]; return a; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   469
                a -> a[0], CH_NOID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   470
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   471
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   472
    /**
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   473
     * Returns a {@code Collector} that produces the sum of a long-valued
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   474
     * function applied to the input elements.  If no elements are present,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   475
     * the result is 0.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   476
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   477
     * @param <T> the type of the input elements
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   478
     * @param mapper a function extracting the property to be summed
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   479
     * @return a {@code Collector} that produces the sum of a derived property
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   480
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   481
    public static <T> Collector<T, ?, Long>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   482
    summingLong(ToLongFunction<? super T> mapper) {
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   483
        return new CollectorImpl<>(
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   484
                () -> new long[1],
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   485
                (a, t) -> { a[0] += mapper.applyAsLong(t); },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   486
                (a, b) -> { a[0] += b[0]; return a; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   487
                a -> a[0], CH_NOID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   488
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   489
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   490
    /**
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   491
     * Returns a {@code Collector} that produces the sum of a double-valued
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   492
     * function applied to the input elements.  If no elements are present,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   493
     * the result is 0.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   494
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   495
     * <p>The sum returned can vary depending upon the order in which
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   496
     * values are recorded, due to accumulated rounding error in
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   497
     * addition of values of differing magnitudes. Values sorted by increasing
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   498
     * absolute magnitude tend to yield more accurate results.  If any recorded
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   499
     * value is a {@code NaN} or the sum is at any point a {@code NaN} then the
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   500
     * sum will be {@code NaN}.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   501
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   502
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   503
     * @param mapper a function extracting the property to be summed
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   504
     * @return a {@code Collector} that produces the sum of a derived property
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   505
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   506
    public static <T> Collector<T, ?, Double>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   507
    summingDouble(ToDoubleFunction<? super T> mapper) {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   508
        /*
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   509
         * In the arrays allocated for the collect operation, index 0
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   510
         * holds the high-order bits of the running sum, index 1 holds
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   511
         * the low-order bits of the sum computed via compensated
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   512
         * summation, and index 2 holds the simple sum used to compute
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   513
         * the proper result if the stream contains infinite values of
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   514
         * the same sign.
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   515
         */
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   516
        return new CollectorImpl<>(
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   517
                () -> new double[3],
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   518
                (a, t) -> { sumWithCompensation(a, mapper.applyAsDouble(t));
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   519
                            a[2] += mapper.applyAsDouble(t);},
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   520
                (a, b) -> { sumWithCompensation(a, b[0]);
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   521
                            a[2] += b[2];
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   522
                            return sumWithCompensation(a, b[1]); },
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   523
                a -> computeFinalSum(a),
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   524
                CH_NOID);
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   525
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   526
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   527
    /**
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   528
     * Incorporate a new double value using Kahan summation /
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   529
     * compensation summation.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   530
     *
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   531
     * High-order bits of the sum are in intermediateSum[0], low-order
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   532
     * bits of the sum are in intermediateSum[1], any additional
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   533
     * elements are application-specific.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   534
     *
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   535
     * @param intermediateSum the high-order and low-order words of the intermediate sum
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   536
     * @param value the name value to be included in the running sum
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   537
     */
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   538
    static double[] sumWithCompensation(double[] intermediateSum, double value) {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   539
        double tmp = value - intermediateSum[1];
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   540
        double sum = intermediateSum[0];
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   541
        double velvel = sum + tmp; // Little wolf of rounding error
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   542
        intermediateSum[1] = (velvel - sum) - tmp;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   543
        intermediateSum[0] = velvel;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   544
        return intermediateSum;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   545
    }
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   546
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   547
    /**
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   548
     * If the compensated sum is spuriously NaN from accumulating one
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   549
     * or more same-signed infinite values, return the
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   550
     * correctly-signed infinity stored in the simple sum.
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   551
     */
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   552
    static double computeFinalSum(double[] summands) {
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   553
        // Better error bounds to add both terms as the final sum
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   554
        double tmp = summands[0] + summands[1];
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   555
        double simpleSum = summands[summands.length - 1];
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   556
        if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   557
            return simpleSum;
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   558
        else
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   559
            return tmp;
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   560
    }
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   561
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   562
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   563
     * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   564
     * function applied to the input elements.  If no elements are present,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   565
     * the result is 0.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   566
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   567
     * @param <T> the type of the input elements
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   568
     * @param mapper a function extracting the property to be summed
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   569
     * @return a {@code Collector} that produces the sum of a derived property
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   570
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   571
    public static <T> Collector<T, ?, Double>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   572
    averagingInt(ToIntFunction<? super T> mapper) {
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   573
        return new CollectorImpl<>(
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   574
                () -> new long[2],
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   575
                (a, t) -> { a[0] += mapper.applyAsInt(t); a[1]++; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   576
                (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   577
                a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   578
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   579
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   580
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   581
     * Returns a {@code Collector} that produces the arithmetic mean of a long-valued
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   582
     * function applied to the input elements.  If no elements are present,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   583
     * the result is 0.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   584
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   585
     * @param <T> the type of the input elements
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   586
     * @param mapper a function extracting the property to be summed
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   587
     * @return a {@code Collector} that produces the sum of a derived property
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   588
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   589
    public static <T> Collector<T, ?, Double>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   590
    averagingLong(ToLongFunction<? super T> mapper) {
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   591
        return new CollectorImpl<>(
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   592
                () -> new long[2],
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   593
                (a, t) -> { a[0] += mapper.applyAsLong(t); a[1]++; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   594
                (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   595
                a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   596
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   597
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   598
    /**
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   599
     * Returns a {@code Collector} that produces the arithmetic mean of a double-valued
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   600
     * function applied to the input elements.  If no elements are present,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   601
     * the result is 0.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   602
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   603
     * <p>The average returned can vary depending upon the order in which
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   604
     * values are recorded, due to accumulated rounding error in
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   605
     * addition of values of differing magnitudes. Values sorted by increasing
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   606
     * absolute magnitude tend to yield more accurate results.  If any recorded
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   607
     * value is a {@code NaN} or the sum is at any point a {@code NaN} then the
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   608
     * average will be {@code NaN}.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   609
     *
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   610
     * @implNote The {@code double} format can represent all
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   611
     * consecutive integers in the range -2<sup>53</sup> to
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   612
     * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   613
     * values, the divisor in the average computation will saturate at
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   614
     * 2<sup>53</sup>, leading to additional numerical errors.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   615
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   616
     * @param <T> the type of the input elements
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   617
     * @param mapper a function extracting the property to be summed
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   618
     * @return a {@code Collector} that produces the sum of a derived property
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   619
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   620
    public static <T> Collector<T, ?, Double>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   621
    averagingDouble(ToDoubleFunction<? super T> mapper) {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   622
        /*
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   623
         * In the arrays allocated for the collect operation, index 0
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   624
         * holds the high-order bits of the running sum, index 1 holds
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   625
         * the low-order bits of the sum computed via compensated
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   626
         * summation, and index 2 holds the number of values seen.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   627
         */
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   628
        return new CollectorImpl<>(
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   629
                () -> new double[4],
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   630
                (a, t) -> { sumWithCompensation(a, mapper.applyAsDouble(t)); a[2]++; a[3]+= mapper.applyAsDouble(t);},
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   631
                (a, b) -> { sumWithCompensation(a, b[0]); sumWithCompensation(a, b[1]); a[2] += b[2]; a[3] += b[3]; return a; },
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   632
                a -> (a[2] == 0) ? 0.0d : (computeFinalSum(a) / a[2]),
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 21339
diff changeset
   633
                CH_NOID);
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   634
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   635
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   636
    /**
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   637
     * Returns a {@code Collector} which performs a reduction of its
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   638
     * input elements under a specified {@code BinaryOperator} using the
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   639
     * provided identity.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   640
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   641
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   642
     * The {@code reducing()} collectors are most useful when used in a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   643
     * multi-level reduction, downstream of {@code groupingBy} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   644
     * {@code partitioningBy}.  To perform a simple reduction on a stream,
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   645
     * use {@link Stream#reduce(Object, BinaryOperator)}} instead.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   646
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   647
     * @param <T> element type for the input and output of the reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   648
     * @param identity the identity value for the reduction (also, the value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   649
     *                 that is returned when there are no input elements)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   650
     * @param op a {@code BinaryOperator<T>} used to reduce the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   651
     * @return a {@code Collector} which implements the reduction operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   652
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   653
     * @see #reducing(BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   654
     * @see #reducing(Object, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   655
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   656
    public static <T> Collector<T, ?, T>
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   657
    reducing(T identity, BinaryOperator<T> op) {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   658
        return new CollectorImpl<>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   659
                boxSupplier(identity),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   660
                (a, t) -> { a[0] = op.apply(a[0], t); },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   661
                (a, b) -> { a[0] = op.apply(a[0], b[0]); return a; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   662
                a -> a[0],
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   663
                CH_NOID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   664
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   665
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   666
    @SuppressWarnings("unchecked")
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   667
    private static <T> Supplier<T[]> boxSupplier(T identity) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   668
        return () -> (T[]) new Object[] { identity };
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   669
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   670
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   671
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   672
     * Returns a {@code Collector} which performs a reduction of its
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   673
     * input elements under a specified {@code BinaryOperator}.  The result
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   674
     * is described as an {@code Optional<T>}.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   675
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   676
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   677
     * The {@code reducing()} collectors are most useful when used in a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   678
     * multi-level reduction, downstream of {@code groupingBy} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   679
     * {@code partitioningBy}.  To perform a simple reduction on a stream,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   680
     * use {@link Stream#reduce(BinaryOperator)} instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   681
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   682
     * <p>For example, given a stream of {@code Person}, to calculate tallest
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   683
     * person in each city:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   684
     * <pre>{@code
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17196
diff changeset
   685
     *     Comparator<Person> byHeight = Comparator.comparing(Person::getHeight);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   686
     *     Map<City, Person> tallestByCity
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   687
     *         = people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight))));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   688
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   689
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   690
     * @param <T> element type for the input and output of the reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   691
     * @param op a {@code BinaryOperator<T>} used to reduce the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   692
     * @return a {@code Collector} which implements the reduction operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   693
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   694
     * @see #reducing(Object, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   695
     * @see #reducing(Object, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   696
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   697
    public static <T> Collector<T, ?, Optional<T>>
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   698
    reducing(BinaryOperator<T> op) {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   699
        class OptionalBox implements Consumer<T> {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   700
            T value = null;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   701
            boolean present = false;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   702
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   703
            @Override
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   704
            public void accept(T t) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   705
                if (present) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   706
                    value = op.apply(value, t);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   707
                }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   708
                else {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   709
                    value = t;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   710
                    present = true;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   711
                }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   712
            }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   713
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   714
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   715
        return new CollectorImpl<T, OptionalBox, Optional<T>>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   716
                OptionalBox::new, OptionalBox::accept,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   717
                (a, b) -> { if (b.present) a.accept(b.value); return a; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   718
                a -> Optional.ofNullable(a.value), CH_NOID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   719
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   720
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   721
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   722
     * Returns a {@code Collector} which performs a reduction of its
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   723
     * input elements under a specified mapping function and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   724
     * {@code BinaryOperator}. This is a generalization of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   725
     * {@link #reducing(Object, BinaryOperator)} which allows a transformation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   726
     * of the elements before reduction.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   727
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   728
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   729
     * The {@code reducing()} collectors are most useful when used in a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   730
     * multi-level reduction, downstream of {@code groupingBy} or
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   731
     * {@code partitioningBy}.  To perform a simple map-reduce on a stream,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   732
     * use {@link Stream#map(Function)} and {@link Stream#reduce(Object, BinaryOperator)}
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   733
     * instead.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   734
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   735
     * <p>For example, given a stream of {@code Person}, to calculate the longest
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   736
     * last name of residents in each city:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   737
     * <pre>{@code
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17196
diff changeset
   738
     *     Comparator<String> byLength = Comparator.comparing(String::length);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   739
     *     Map<City, String> longestLastNameByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   740
     *         = people.stream().collect(groupingBy(Person::getCity,
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   741
     *                                              reducing(Person::getLastName, BinaryOperator.maxBy(byLength))));
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   742
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   743
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   744
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   745
     * @param <U> the type of the mapped values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   746
     * @param identity the identity value for the reduction (also, the value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   747
     *                 that is returned when there are no input elements)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   748
     * @param mapper a mapping function to apply to each input value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   749
     * @param op a {@code BinaryOperator<U>} used to reduce the mapped values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   750
     * @return a {@code Collector} implementing the map-reduce operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   751
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   752
     * @see #reducing(Object, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   753
     * @see #reducing(BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   754
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   755
    public static <T, U>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   756
    Collector<T, ?, U> reducing(U identity,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   757
                                Function<? super T, ? extends U> mapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   758
                                BinaryOperator<U> op) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   759
        return new CollectorImpl<>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   760
                boxSupplier(identity),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   761
                (a, t) -> { a[0] = op.apply(a[0], mapper.apply(t)); },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   762
                (a, b) -> { a[0] = op.apply(a[0], b[0]); return a; },
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   763
                a -> a[0], CH_NOID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   764
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   765
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   766
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   767
     * Returns a {@code Collector} implementing a "group by" operation on
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   768
     * input elements of type {@code T}, grouping elements according to a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   769
     * classification function, and returning the results in a {@code Map}.
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   770
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   771
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   772
     * The collector produces a {@code Map<K, List<T>>} whose keys are the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   773
     * values resulting from applying the classification function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   774
     * elements, and whose corresponding values are {@code List}s containing the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   775
     * input elements which map to the associated key under the classification
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   776
     * function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   777
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   778
     * <p>There are no guarantees on the type, mutability, serializability, or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   779
     * thread-safety of the {@code Map} or {@code List} objects returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   780
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   781
     * This produces a result similar to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   782
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   783
     *     groupingBy(classifier, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   784
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   785
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   786
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   787
     * The returned {@code Collector} is not concurrent.  For parallel stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   788
     * pipelines, the {@code combiner} function operates by merging the keys
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   789
     * from one map into another, which can be an expensive operation.  If
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   790
     * preservation of the order in which elements appear in the resulting {@code Map}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   791
     * collector is not required, using {@link #groupingByConcurrent(Function)}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   792
     * may offer better parallel performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   793
     *
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   794
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   795
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   796
     * @param classifier the classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   797
     * @return a {@code Collector} implementing the group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   798
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   799
     * @see #groupingBy(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   800
     * @see #groupingBy(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   801
     * @see #groupingByConcurrent(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   802
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   803
    public static <T, K> Collector<T, ?, Map<K, List<T>>>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   804
    groupingBy(Function<? super T, ? extends K> classifier) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   805
        return groupingBy(classifier, toList());
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   806
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   807
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   808
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   809
     * Returns a {@code Collector} implementing a cascaded "group by" operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   810
     * on input elements of type {@code T}, grouping elements according to a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   811
     * classification function, and then performing a reduction operation on
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   812
     * the values associated with a given key using the specified downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   813
     * {@code Collector}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   814
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   815
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   816
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   817
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   818
     * {@code Map<K, D>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   819
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   820
     * <p>There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   821
     * serializability, or thread-safety of the {@code Map} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   822
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   823
     * <p>For example, to compute the set of last names of people in each city:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   824
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   825
     *     Map<City, Set<String>> namesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   826
     *         = people.stream().collect(groupingBy(Person::getCity,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   827
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   828
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   829
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   830
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   831
     * The returned {@code Collector} is not concurrent.  For parallel stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   832
     * pipelines, the {@code combiner} function operates by merging the keys
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   833
     * from one map into another, which can be an expensive operation.  If
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   834
     * preservation of the order in which elements are presented to the downstream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   835
     * collector is not required, using {@link #groupingByConcurrent(Function, Collector)}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   836
     * may offer better parallel performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   837
     *
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   838
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   839
     * @param <K> the type of the keys
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   840
     * @param <A> the intermediate accumulation type of the downstream collector
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   841
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   842
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   843
     * @param downstream a {@code Collector} implementing the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   844
     * @return a {@code Collector} implementing the cascaded group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   845
     * @see #groupingBy(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   846
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   847
     * @see #groupingBy(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   848
     * @see #groupingByConcurrent(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   849
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   850
    public static <T, K, A, D>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   851
    Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   852
                                          Collector<? super T, A, D> downstream) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   853
        return groupingBy(classifier, HashMap::new, downstream);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   854
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   855
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   856
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   857
     * Returns a {@code Collector} implementing a cascaded "group by" operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   858
     * on input elements of type {@code T}, grouping elements according to a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   859
     * classification function, and then performing a reduction operation on
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   860
     * the values associated with a given key using the specified downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   861
     * {@code Collector}.  The {@code Map} produced by the Collector is created
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   862
     * with the supplied factory function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   863
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   864
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   865
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   866
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   867
     * {@code Map<K, D>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   868
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   869
     * <p>For example, to compute the set of last names of people in each city,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   870
     * where the city names are sorted:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   871
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   872
     *     Map<City, Set<String>> namesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   873
     *         = people.stream().collect(groupingBy(Person::getCity, TreeMap::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   874
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   875
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   876
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   877
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   878
     * The returned {@code Collector} is not concurrent.  For parallel stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   879
     * pipelines, the {@code combiner} function operates by merging the keys
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   880
     * from one map into another, which can be an expensive operation.  If
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   881
     * preservation of the order in which elements are presented to the downstream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   882
     * collector is not required, using {@link #groupingByConcurrent(Function, Supplier, Collector)}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   883
     * may offer better parallel performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   884
     *
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   885
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   886
     * @param <K> the type of the keys
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   887
     * @param <A> the intermediate accumulation type of the downstream collector
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   888
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   889
     * @param <M> the type of the resulting {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   890
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   891
     * @param downstream a {@code Collector} implementing the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   892
     * @param mapFactory a function which, when called, produces a new empty
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   893
     *                   {@code Map} of the desired type
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   894
     * @return a {@code Collector} implementing the cascaded group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   895
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   896
     * @see #groupingBy(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   897
     * @see #groupingBy(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   898
     * @see #groupingByConcurrent(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   899
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   900
    public static <T, K, D, A, M extends Map<K, D>>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   901
    Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   902
                                  Supplier<M> mapFactory,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   903
                                  Collector<? super T, A, D> downstream) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   904
        Supplier<A> downstreamSupplier = downstream.supplier();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   905
        BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   906
        BiConsumer<Map<K, A>, T> accumulator = (m, t) -> {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   907
            K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   908
            A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   909
            downstreamAccumulator.accept(container, t);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   910
        };
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   911
        BinaryOperator<Map<K, A>> merger = Collectors.<K, A, Map<K, A>>mapMerger(downstream.combiner());
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   912
        @SuppressWarnings("unchecked")
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   913
        Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   914
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   915
        if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   916
            return new CollectorImpl<>(mangledFactory, accumulator, merger, CH_ID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   917
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   918
        else {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   919
            @SuppressWarnings("unchecked")
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   920
            Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   921
            Function<Map<K, A>, M> finisher = intermediate -> {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   922
                intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v));
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   923
                @SuppressWarnings("unchecked")
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   924
                M castResult = (M) intermediate;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   925
                return castResult;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   926
            };
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   927
            return new CollectorImpl<>(mangledFactory, accumulator, merger, finisher, CH_NOID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   928
        }
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   929
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   930
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   931
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   932
     * Returns a concurrent {@code Collector} implementing a "group by"
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   933
     * operation on input elements of type {@code T}, grouping elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   934
     * according to a classification function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   935
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   936
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   937
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   938
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   939
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   940
     * The collector produces a {@code ConcurrentMap<K, List<T>>} whose keys are the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   941
     * values resulting from applying the classification function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   942
     * elements, and whose corresponding values are {@code List}s containing the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   943
     * input elements which map to the associated key under the classification
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   944
     * function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   945
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   946
     * <p>There are no guarantees on the type, mutability, or serializability
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   947
     * of the {@code Map} or {@code List} objects returned, or of the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   948
     * thread-safety of the {@code List} objects returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   949
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   950
     * This produces a result similar to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   951
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   952
     *     groupingByConcurrent(classifier, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   953
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   954
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   955
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   956
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   957
     * @param classifier a classifier function mapping input elements to keys
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   958
     * @return a concurrent, unordered {@code Collector} implementing the group-by operation
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   959
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   960
     * @see #groupingBy(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   961
     * @see #groupingByConcurrent(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   962
     * @see #groupingByConcurrent(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   963
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   964
    public static <T, K>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   965
    Collector<T, ?, ConcurrentMap<K, List<T>>>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   966
    groupingByConcurrent(Function<? super T, ? extends K> classifier) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   967
        return groupingByConcurrent(classifier, ConcurrentHashMap::new, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   968
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   969
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   970
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   971
     * Returns a concurrent {@code Collector} implementing a cascaded "group by"
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   972
     * operation on input elements of type {@code T}, grouping elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   973
     * according to a classification function, and then performing a reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   974
     * operation on the values associated with a given key using the specified
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   975
     * downstream {@code Collector}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   976
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   977
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   978
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   979
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   980
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   981
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   982
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   983
     * {@code Map<K, D>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   984
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   985
     * <p>For example, to compute the set of last names of people in each city,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   986
     * where the city names are sorted:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   987
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   988
     *     ConcurrentMap<City, Set<String>> namesByCity
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19593
diff changeset
   989
     *         = people.stream().collect(groupingByConcurrent(Person::getCity,
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   990
     *                                                        mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   991
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   992
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   993
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   994
     * @param <K> the type of the keys
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
   995
     * @param <A> the intermediate accumulation type of the downstream collector
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   996
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   997
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   998
     * @param downstream a {@code Collector} implementing the downstream reduction
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
   999
     * @return a concurrent, unordered {@code Collector} implementing the cascaded group-by operation
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1000
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1001
     * @see #groupingBy(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1002
     * @see #groupingByConcurrent(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1003
     * @see #groupingByConcurrent(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1004
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1005
    public static <T, K, A, D>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1006
    Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1007
                                                              Collector<? super T, A, D> downstream) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1008
        return groupingByConcurrent(classifier, ConcurrentHashMap::new, downstream);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1009
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1010
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1011
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1012
     * Returns a concurrent {@code Collector} implementing a cascaded "group by"
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1013
     * operation on input elements of type {@code T}, grouping elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1014
     * according to a classification function, and then performing a reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1015
     * operation on the values associated with a given key using the specified
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1016
     * downstream {@code Collector}.  The {@code ConcurrentMap} produced by the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1017
     * Collector is created with the supplied factory function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1018
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1019
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1020
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1021
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1022
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1023
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1024
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1025
     * {@code Map<K, D>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1026
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1027
     * <p>For example, to compute the set of last names of people in each city,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1028
     * where the city names are sorted:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1029
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1030
     *     ConcurrentMap<City, Set<String>> namesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1031
     *         = people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1032
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1033
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1034
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1035
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1036
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1037
     * @param <K> the type of the keys
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1038
     * @param <A> the intermediate accumulation type of the downstream collector
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1039
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1040
     * @param <M> the type of the resulting {@code ConcurrentMap}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1041
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1042
     * @param downstream a {@code Collector} implementing the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1043
     * @param mapFactory a function which, when called, produces a new empty
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1044
     *                   {@code ConcurrentMap} of the desired type
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1045
     * @return a concurrent, unordered {@code Collector} implementing the cascaded group-by operation
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1046
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1047
     * @see #groupingByConcurrent(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1048
     * @see #groupingByConcurrent(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1049
     * @see #groupingBy(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1050
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1051
    public static <T, K, A, D, M extends ConcurrentMap<K, D>>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1052
    Collector<T, ?, M> groupingByConcurrent(Function<? super T, ? extends K> classifier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1053
                                            Supplier<M> mapFactory,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1054
                                            Collector<? super T, A, D> downstream) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1055
        Supplier<A> downstreamSupplier = downstream.supplier();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1056
        BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1057
        BinaryOperator<ConcurrentMap<K, A>> merger = Collectors.<K, A, ConcurrentMap<K, A>>mapMerger(downstream.combiner());
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1058
        @SuppressWarnings("unchecked")
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1059
        Supplier<ConcurrentMap<K, A>> mangledFactory = (Supplier<ConcurrentMap<K, A>>) mapFactory;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1060
        BiConsumer<ConcurrentMap<K, A>, T> accumulator;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1061
        if (downstream.characteristics().contains(Collector.Characteristics.CONCURRENT)) {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1062
            accumulator = (m, t) -> {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1063
                K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1064
                A resultContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get());
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1065
                downstreamAccumulator.accept(resultContainer, t);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1066
            };
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1067
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1068
        else {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1069
            accumulator = (m, t) -> {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1070
                K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1071
                A resultContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get());
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1072
                synchronized (resultContainer) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1073
                    downstreamAccumulator.accept(resultContainer, t);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1074
                }
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1075
            };
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1076
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1077
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1078
        if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1079
            return new CollectorImpl<>(mangledFactory, accumulator, merger, CH_CONCURRENT_ID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1080
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1081
        else {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1082
            @SuppressWarnings("unchecked")
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1083
            Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1084
            Function<ConcurrentMap<K, A>, M> finisher = intermediate -> {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1085
                intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v));
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1086
                @SuppressWarnings("unchecked")
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1087
                M castResult = (M) intermediate;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1088
                return castResult;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1089
            };
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1090
            return new CollectorImpl<>(mangledFactory, accumulator, merger, finisher, CH_CONCURRENT_NOID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1091
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1092
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1093
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1094
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1095
     * Returns a {@code Collector} which partitions the input elements according
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1096
     * to a {@code Predicate}, and organizes them into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1097
     * {@code Map<Boolean, List<T>>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1098
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1099
     * There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1100
     * serializability, or thread-safety of the {@code Map} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1101
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1102
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1103
     * @param predicate a predicate used for classifying input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1104
     * @return a {@code Collector} implementing the partitioning operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1105
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1106
     * @see #partitioningBy(Predicate, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1107
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1108
    public static <T>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1109
    Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1110
        return partitioningBy(predicate, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1111
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1112
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1113
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1114
     * Returns a {@code Collector} which partitions the input elements according
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1115
     * to a {@code Predicate}, reduces the values in each partition according to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1116
     * another {@code Collector}, and organizes them into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1117
     * {@code Map<Boolean, D>} whose values are the result of the downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1118
     * reduction.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1119
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1120
     * <p>There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1121
     * serializability, or thread-safety of the {@code Map} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1122
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1123
     * @param <T> the type of the input elements
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1124
     * @param <A> the intermediate accumulation type of the downstream collector
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1125
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1126
     * @param predicate a predicate used for classifying input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1127
     * @param downstream a {@code Collector} implementing the downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1128
     *                   reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1129
     * @return a {@code Collector} implementing the cascaded partitioning
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1130
     *         operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1131
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1132
     * @see #partitioningBy(Predicate)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1133
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1134
    public static <T, D, A>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1135
    Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1136
                                                    Collector<? super T, A, D> downstream) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1137
        BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1138
        BiConsumer<Partition<A>, T> accumulator = (result, t) ->
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1139
                downstreamAccumulator.accept(predicate.test(t) ? result.forTrue : result.forFalse, t);
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1140
        BinaryOperator<A> op = downstream.combiner();
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1141
        BinaryOperator<Partition<A>> merger = (left, right) ->
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1142
                new Partition<>(op.apply(left.forTrue, right.forTrue),
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1143
                                op.apply(left.forFalse, right.forFalse));
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1144
        Supplier<Partition<A>> supplier = () ->
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1145
                new Partition<>(downstream.supplier().get(),
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1146
                                downstream.supplier().get());
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1147
        if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1148
            return new CollectorImpl<>(supplier, accumulator, merger, CH_ID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1149
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1150
        else {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1151
            Function<Partition<A>, Map<Boolean, D>> finisher = par ->
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1152
                    new Partition<>(downstream.finisher().apply(par.forTrue),
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19420
diff changeset
  1153
                                    downstream.finisher().apply(par.forFalse));
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1154
            return new CollectorImpl<>(supplier, accumulator, merger, finisher, CH_NOID);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1155
        }
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1156
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1157
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1158
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1159
     * Returns a {@code Collector} that accumulates elements into a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1160
     * {@code Map} whose keys and values are the result of applying the provided
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1161
     * mapping functions to the input elements.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1162
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1163
     * <p>If the mapped keys contains duplicates (according to
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1164
     * {@link Object#equals(Object)}), an {@code IllegalStateException} is
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1165
     * thrown when the collection operation is performed.  If the mapped keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1166
     * may have duplicates, use {@link #toMap(Function, Function, BinaryOperator)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1167
     * instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1168
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1169
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1170
     * It is common for either the key or the value to be the input elements.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1171
     * In this case, the utility method
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1172
     * {@link java.util.function.Function#identity()} may be helpful.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1173
     * For example, the following produces a {@code Map} mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1174
     * students to their grade point average:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1175
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1176
     *     Map<Student, Double> studentToGPA
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1177
     *         students.stream().collect(toMap(Functions.identity(),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1178
     *                                         student -> computeGPA(student)));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1179
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1180
     * And the following produces a {@code Map} mapping a unique identifier to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1181
     * students:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1182
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1183
     *     Map<String, Student> studentIdToStudent
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1184
     *         students.stream().collect(toMap(Student::getId,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1185
     *                                         Functions.identity());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1186
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1187
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1188
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1189
     * The returned {@code Collector} is not concurrent.  For parallel stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1190
     * pipelines, the {@code combiner} function operates by merging the keys
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1191
     * from one map into another, which can be an expensive operation.  If it is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1192
     * not required that results are inserted into the {@code Map} in encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1193
     * order, using {@link #toConcurrentMap(Function, Function)}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1194
     * may offer better parallel performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1195
     *
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1196
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1197
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1198
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1199
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1200
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1201
     * @return a {@code Collector} which collects elements into a {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1202
     * whose keys and values are the result of applying mapping functions to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1203
     * the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1204
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1205
     * @see #toMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1206
     * @see #toMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1207
     * @see #toConcurrentMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1208
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1209
    public static <T, K, U>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1210
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1211
                                    Function<? super T, ? extends U> valueMapper) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1212
        return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1213
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1214
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1215
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1216
     * Returns a {@code Collector} that accumulates elements into a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1217
     * {@code Map} whose keys and values are the result of applying the provided
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1218
     * mapping functions to the input elements.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1219
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1220
     * <p>If the mapped
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1221
     * keys contains duplicates (according to {@link Object#equals(Object)}),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1222
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1223
     * results are merged using the provided merging function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1224
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1225
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1226
     * There are multiple ways to deal with collisions between multiple elements
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1227
     * mapping to the same key.  The other forms of {@code toMap} simply use
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1228
     * a merge function that throws unconditionally, but you can easily write
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1229
     * more flexible merge policies.  For example, if you have a stream
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1230
     * of {@code Person}, and you want to produce a "phone book" mapping name to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1231
     * address, but it is possible that two persons have the same name, you can
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1232
     * do as follows to gracefully deals with these collisions, and produce a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1233
     * {@code Map} mapping names to a concatenated list of addresses:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1234
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1235
     *     Map<String, String> phoneBook
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1236
     *         people.stream().collect(toMap(Person::getName,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1237
     *                                       Person::getAddress,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1238
     *                                       (s, a) -> s + ", " + a));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1239
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1240
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1241
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1242
     * The returned {@code Collector} is not concurrent.  For parallel stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1243
     * pipelines, the {@code combiner} function operates by merging the keys
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1244
     * from one map into another, which can be an expensive operation.  If it is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1245
     * not required that results are merged into the {@code Map} in encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1246
     * order, using {@link #toConcurrentMap(Function, Function, BinaryOperator)}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1247
     * may offer better parallel performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1248
     *
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1249
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1250
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1251
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1252
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1253
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1254
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1255
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1256
     *                      to {@link Map#merge(Object, Object, BiFunction)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1257
     * @return a {@code Collector} which collects elements into a {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1258
     * whose keys are the result of applying a key mapping function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1259
     * elements, and whose values are the result of applying a value mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1260
     * function to all input elements equal to the key and combining them
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1261
     * using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1262
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1263
     * @see #toMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1264
     * @see #toMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1265
     * @see #toConcurrentMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1266
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1267
    public static <T, K, U>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1268
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1269
                                    Function<? super T, ? extends U> valueMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1270
                                    BinaryOperator<U> mergeFunction) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1271
        return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1272
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1273
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1274
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1275
     * Returns a {@code Collector} that accumulates elements into a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1276
     * {@code Map} whose keys and values are the result of applying the provided
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1277
     * mapping functions to the input elements.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1278
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1279
     * <p>If the mapped
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1280
     * keys contains duplicates (according to {@link Object#equals(Object)}),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1281
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1282
     * results are merged using the provided merging function.  The {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1283
     * is created by a provided supplier function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1284
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1285
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1286
     * The returned {@code Collector} is not concurrent.  For parallel stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1287
     * pipelines, the {@code combiner} function operates by merging the keys
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1288
     * from one map into another, which can be an expensive operation.  If it is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1289
     * not required that results are merged into the {@code Map} in encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1290
     * order, using {@link #toConcurrentMap(Function, Function, BinaryOperator, Supplier)}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1291
     * may offer better parallel performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1292
     *
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1293
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1294
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1295
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1296
     * @param <M> the type of the resulting {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1297
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1298
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1299
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1300
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1301
     *                      to {@link Map#merge(Object, Object, BiFunction)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1302
     * @param mapSupplier a function which returns a new, empty {@code Map} into
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1303
     *                    which the results will be inserted
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1304
     * @return a {@code Collector} which collects elements into a {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1305
     * whose keys are the result of applying a key mapping function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1306
     * elements, and whose values are the result of applying a value mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1307
     * function to all input elements equal to the key and combining them
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1308
     * using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1309
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1310
     * @see #toMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1311
     * @see #toMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1312
     * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1313
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1314
    public static <T, K, U, M extends Map<K, U>>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1315
    Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1316
                                Function<? super T, ? extends U> valueMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1317
                                BinaryOperator<U> mergeFunction,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1318
                                Supplier<M> mapSupplier) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1319
        BiConsumer<M, T> accumulator
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1320
                = (map, element) -> map.merge(keyMapper.apply(element),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1321
                                              valueMapper.apply(element), mergeFunction);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1322
        return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1323
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1324
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1325
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1326
     * Returns a concurrent {@code Collector} that accumulates elements into a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1327
     * {@code ConcurrentMap} whose keys and values are the result of applying
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1328
     * the provided mapping functions to the input elements.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1329
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1330
     * <p>If the mapped keys contains duplicates (according to
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1331
     * {@link Object#equals(Object)}), an {@code IllegalStateException} is
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1332
     * thrown when the collection operation is performed.  If the mapped keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1333
     * may have duplicates, use
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1334
     * {@link #toConcurrentMap(Function, Function, BinaryOperator)} instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1335
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1336
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1337
     * It is common for either the key or the value to be the input elements.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1338
     * In this case, the utility method
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1339
     * {@link java.util.function.Function#identity()} may be helpful.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1340
     * For example, the following produces a {@code Map} mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1341
     * students to their grade point average:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1342
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1343
     *     Map<Student, Double> studentToGPA
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1344
     *         students.stream().collect(toMap(Functions.identity(),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1345
     *                                         student -> computeGPA(student)));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1346
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1347
     * And the following produces a {@code Map} mapping a unique identifier to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1348
     * students:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1349
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1350
     *     Map<String, Student> studentIdToStudent
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1351
     *         students.stream().collect(toConcurrentMap(Student::getId,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1352
     *                                                   Functions.identity());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1353
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1354
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1355
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1356
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1357
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1358
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1359
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1360
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1361
     * @param keyMapper the mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1362
     * @param valueMapper the mapping function to produce values
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1363
     * @return a concurrent, unordered {@code Collector} which collects elements into a
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1364
     * {@code ConcurrentMap} whose keys are the result of applying a key mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1365
     * function to the input elements, and whose values are the result of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1366
     * applying a value mapping function to the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1367
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1368
     * @see #toMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1369
     * @see #toConcurrentMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1370
     * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1371
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1372
    public static <T, K, U>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1373
    Collector<T, ?, ConcurrentMap<K,U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1374
                                                        Function<? super T, ? extends U> valueMapper) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1375
        return toConcurrentMap(keyMapper, valueMapper, throwingMerger(), ConcurrentHashMap::new);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1376
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1377
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1378
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1379
     * Returns a concurrent {@code Collector} that accumulates elements into a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1380
     * {@code ConcurrentMap} whose keys and values are the result of applying
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1381
     * the provided mapping functions to the input elements.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1382
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1383
     * <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}),
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1384
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1385
     * results are merged using the provided merging function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1386
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1387
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1388
     * There are multiple ways to deal with collisions between multiple elements
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1389
     * mapping to the same key.  The other forms of {@code toConcurrentMap} simply use
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1390
     * a merge function that throws unconditionally, but you can easily write
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1391
     * more flexible merge policies.  For example, if you have a stream
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1392
     * of {@code Person}, and you want to produce a "phone book" mapping name to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1393
     * address, but it is possible that two persons have the same name, you can
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1394
     * do as follows to gracefully deals with these collisions, and produce a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1395
     * {@code Map} mapping names to a concatenated list of addresses:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1396
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1397
     *     Map<String, String> phoneBook
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1398
     *         people.stream().collect(toConcurrentMap(Person::getName,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1399
     *                                                 Person::getAddress,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1400
     *                                                 (s, a) -> s + ", " + a));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1401
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1402
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1403
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1404
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1405
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1406
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1407
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1408
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1409
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1410
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1411
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1412
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1413
     *                      to {@link Map#merge(Object, Object, BiFunction)}
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1414
     * @return a concurrent, unordered {@code Collector} which collects elements into a
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1415
     * {@code ConcurrentMap} whose keys are the result of applying a key mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1416
     * function to the input elements, and whose values are the result of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1417
     * applying a value mapping function to all input elements equal to the key
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1418
     * and combining them using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1419
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1420
     * @see #toConcurrentMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1421
     * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1422
     * @see #toMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1423
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1424
    public static <T, K, U>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1425
    Collector<T, ?, ConcurrentMap<K,U>>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1426
    toConcurrentMap(Function<? super T, ? extends K> keyMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1427
                    Function<? super T, ? extends U> valueMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1428
                    BinaryOperator<U> mergeFunction) {
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1429
        return toConcurrentMap(keyMapper, valueMapper, mergeFunction, ConcurrentHashMap::new);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1430
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1431
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1432
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1433
     * Returns a concurrent {@code Collector} that accumulates elements into a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1434
     * {@code ConcurrentMap} whose keys and values are the result of applying
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1435
     * the provided mapping functions to the input elements.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1436
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1437
     * <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}),
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1438
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1439
     * results are merged using the provided merging function.  The
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1440
     * {@code ConcurrentMap} is created by a provided supplier function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1441
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1442
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1443
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1444
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1445
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1446
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1447
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1448
     * @param <M> the type of the resulting {@code ConcurrentMap}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1449
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1450
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1451
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1452
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1453
     *                      to {@link Map#merge(Object, Object, BiFunction)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1454
     * @param mapSupplier a function which returns a new, empty {@code Map} into
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1455
     *                    which the results will be inserted
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
  1456
     * @return a concurrent, unordered {@code Collector} which collects elements into a
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1457
     * {@code ConcurrentMap} whose keys are the result of applying a key mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1458
     * function to the input elements, and whose values are the result of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1459
     * applying a value mapping function to all input elements equal to the key
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1460
     * and combining them using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1461
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1462
     * @see #toConcurrentMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1463
     * @see #toConcurrentMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1464
     * @see #toMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1465
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1466
    public static <T, K, U, M extends ConcurrentMap<K, U>>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1467
    Collector<T, ?, M> toConcurrentMap(Function<? super T, ? extends K> keyMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1468
                                       Function<? super T, ? extends U> valueMapper,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1469
                                       BinaryOperator<U> mergeFunction,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1470
                                       Supplier<M> mapSupplier) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1471
        BiConsumer<M, T> accumulator
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1472
                = (map, element) -> map.merge(keyMapper.apply(element),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1473
                                              valueMapper.apply(element), mergeFunction);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1474
        return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_CONCURRENT_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1475
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1476
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1477
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1478
     * Returns a {@code Collector} which applies an {@code int}-producing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1479
     * mapping function to each input element, and returns summary statistics
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1480
     * for the resulting values.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1481
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1482
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1483
     * @param mapper a mapping function to apply to each element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1484
     * @return a {@code Collector} implementing the summary-statistics reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1485
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1486
     * @see #summarizingDouble(ToDoubleFunction)
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1487
     * @see #summarizingLong(ToLongFunction)
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1488
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1489
    public static <T>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1490
    Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1491
        return new CollectorImpl<T, IntSummaryStatistics, IntSummaryStatistics>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1492
                IntSummaryStatistics::new,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1493
                (r, t) -> r.accept(mapper.applyAsInt(t)),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1494
                (l, r) -> { l.combine(r); return l; }, CH_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1495
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1496
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1497
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1498
     * Returns a {@code Collector} which applies an {@code long}-producing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1499
     * mapping function to each input element, and returns summary statistics
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1500
     * for the resulting values.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1501
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1502
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1503
     * @param mapper the mapping function to apply to each element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1504
     * @return a {@code Collector} implementing the summary-statistics reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1505
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1506
     * @see #summarizingDouble(ToDoubleFunction)
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1507
     * @see #summarizingInt(ToIntFunction)
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1508
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1509
    public static <T>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1510
    Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1511
        return new CollectorImpl<T, LongSummaryStatistics, LongSummaryStatistics>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1512
                LongSummaryStatistics::new,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1513
                (r, t) -> r.accept(mapper.applyAsLong(t)),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1514
                (l, r) -> { l.combine(r); return l; }, CH_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1515
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1516
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1517
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1518
     * Returns a {@code Collector} which applies an {@code double}-producing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1519
     * mapping function to each input element, and returns summary statistics
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1520
     * for the resulting values.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1521
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1522
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1523
     * @param mapper a mapping function to apply to each element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1524
     * @return a {@code Collector} implementing the summary-statistics reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1525
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1526
     * @see #summarizingLong(ToLongFunction)
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1527
     * @see #summarizingInt(ToIntFunction)
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1528
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1529
    public static <T>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1530
    Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1531
        return new CollectorImpl<T, DoubleSummaryStatistics, DoubleSummaryStatistics>(
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1532
                DoubleSummaryStatistics::new,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1533
                (r, t) -> r.accept(mapper.applyAsDouble(t)),
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1534
                (l, r) -> { l.combine(r); return l; }, CH_ID);
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1535
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1536
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1537
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1538
     * Implementation class used by partitioningBy.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1539
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1540
    private static final class Partition<T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1541
            extends AbstractMap<Boolean, T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1542
            implements Map<Boolean, T> {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1543
        final T forTrue;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1544
        final T forFalse;
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1545
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1546
        Partition(T forTrue, T forFalse) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1547
            this.forTrue = forTrue;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1548
            this.forFalse = forFalse;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1549
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1550
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1551
        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1552
        public Set<Map.Entry<Boolean, T>> entrySet() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1553
            return new AbstractSet<Map.Entry<Boolean, T>>() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1554
                @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1555
                public Iterator<Map.Entry<Boolean, T>> iterator() {
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1556
                    Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1557
                    Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18571
diff changeset
  1558
                    return Arrays.asList(falseEntry, trueEntry).iterator();
17196
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1559
                }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1560
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1561
                @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1562
                public int size() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1563
                    return 2;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1564
                }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1565
            };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1566
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1567
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1568
}