jdk/src/share/classes/java/util/stream/Collectors.java
author psandoz
Tue, 28 May 2013 15:22:30 +0200
changeset 17914 91e138d3b298
parent 17196 ef8b87b1597c
child 18571 8e3cb3c46ae8
permissions -rw-r--r--
8014393: Minor typo in the spec for j.u.stream.Stream.findFirst() Reviewed-by: alanb, chegar
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;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    30
import java.util.Collection;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    31
import java.util.Collections;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    32
import java.util.Comparator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    33
import java.util.Comparators;
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.NoSuchElementException;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    44
import java.util.Objects;
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;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    49
import java.util.function.BiFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    50
import java.util.function.BinaryOperator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    51
import java.util.function.Function;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    52
import java.util.function.Predicate;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    53
import java.util.function.Supplier;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    54
import java.util.function.ToDoubleFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    55
import java.util.function.ToIntFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    56
import java.util.function.ToLongFunction;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    57
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    58
/**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    59
 * Implementations of {@link Collector} that implement various useful reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    60
 * operations, such as accumulating elements into collections, summarizing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    61
 * elements according to various criteria, etc.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    62
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    63
 * <p>The following are examples of using the predefined {@code Collector}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    64
 * implementations in {@link Collectors} with the {@code Stream} API to perform
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    65
 * mutable reduction tasks:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    66
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    67
 * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    68
 *     // Accumulate elements into a List
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    69
 *     List<Person> list = people.collect(Collectors.toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    70
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    71
 *     // Accumulate elements into a TreeSet
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    72
 *     List<Person> list = people.collect(Collectors.toCollection(TreeSet::new));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    73
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    74
 *     // Convert elements to strings and concatenate them, separated by commas
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    75
 *     String joined = stream.map(Object::toString)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    76
 *                           .collect(Collectors.toStringJoiner(", "))
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    77
 *                           .toString();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    78
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    79
 *     // Find highest-paid employee
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    80
 *     Employee highestPaid = employees.stream()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    81
 *                                     .collect(Collectors.maxBy(Comparators.comparing(Employee::getSalary)));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    82
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    83
 *     // Group employees by department
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    84
 *     Map<Department, List<Employee>> byDept
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    85
 *         = employees.stream()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    86
 *                    .collect(Collectors.groupingBy(Employee::getDepartment));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    87
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    88
 *     // Find highest-paid employee by department
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    89
 *     Map<Department, Employee> highestPaidByDept
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    90
 *         = employees.stream()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    91
 *                    .collect(Collectors.groupingBy(Employee::getDepartment,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    92
 *                                                   Collectors.maxBy(Comparators.comparing(Employee::getSalary))));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    93
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    94
 *     // Partition students into passing and failing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    95
 *     Map<Boolean, List<Student>> passingFailing =
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    96
 *         students.stream()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    97
 *                 .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    98
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
    99
 * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   100
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   101
 * TODO explanation of parallel collection
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   102
 *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   103
 * @since 1.8
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   104
 */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   105
public final class Collectors {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   106
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   107
    private static final Set<Collector.Characteristics> CH_CONCURRENT
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   108
            = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.CONCURRENT,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   109
                                                     Collector.Characteristics.STRICTLY_MUTATIVE,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   110
                                                     Collector.Characteristics.UNORDERED));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   111
    private static final Set<Collector.Characteristics> CH_STRICT
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   112
            = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.STRICTLY_MUTATIVE));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   113
    private static final Set<Collector.Characteristics> CH_STRICT_UNORDERED
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   114
            = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.STRICTLY_MUTATIVE,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   115
                                                     Collector.Characteristics.UNORDERED));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   116
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   117
    private Collectors() { }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   118
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
     * Returns a merge function, suitable for use in
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   121
     * {@link Map#merge(Object, Object, BiFunction) Map.merge()} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   122
     * {@link #toMap(Function, Function, BinaryOperator) toMap()}, which always
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   123
     * throws {@code IllegalStateException}.  This can be used to enforce the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   124
     * assumption that the elements being collected are distinct.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   125
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   126
     * @param <T> the type of input arguments to the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   127
     * @return a merge function which always throw {@code IllegalStateException}
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
     * @see #firstWinsMerger()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   130
     * @see #lastWinsMerger()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   131
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   132
    public static <T> BinaryOperator<T> throwingMerger() {
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
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   136
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   137
     * Returns a merge function, suitable for use in
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   138
     * {@link Map#merge(Object, Object, BiFunction) Map.merge()} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   139
     * {@link #toMap(Function, Function, BinaryOperator) toMap()},
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   140
     * which implements a "first wins" policy.
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
     * @param <T> the type of input arguments to the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   143
     * @return a merge function which always returns its first argument
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   144
     * @see #lastWinsMerger()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   145
     * @see #throwingMerger()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   146
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   147
    public static <T> BinaryOperator<T> firstWinsMerger() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   148
        return (u,v) -> u;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   149
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   150
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   151
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   152
     * Returns a merge function, suitable for use in
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   153
     * {@link Map#merge(Object, Object, BiFunction) Map.merge()} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   154
     * {@link #toMap(Function, Function, BinaryOperator) toMap()},
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   155
     * which implements a "last wins" policy.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   156
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   157
     * @param <T> the type of input arguments to the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   158
     * @return a merge function which always returns its second argument
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   159
     * @see #firstWinsMerger()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   160
     * @see #throwingMerger()
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   161
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   162
    public static <T> BinaryOperator<T> lastWinsMerger() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   163
        return (u,v) -> v;
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
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   166
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   167
     * Simple implementation class for {@code Collector}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   168
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   169
     * @param <T> the type of elements to be collected
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   170
     * @param <R> the type of the result
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
    private static final class CollectorImpl<T, R> implements Collector<T,R> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   173
        private final Supplier<R> resultSupplier;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   174
        private final BiFunction<R, T, R> accumulator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   175
        private final BinaryOperator<R> combiner;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   176
        private final Set<Characteristics> characteristics;
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
        CollectorImpl(Supplier<R> resultSupplier,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   179
                      BiFunction<R, T, R> accumulator,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   180
                      BinaryOperator<R> combiner,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   181
                      Set<Characteristics> characteristics) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   182
            this.resultSupplier = resultSupplier;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   183
            this.accumulator = accumulator;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   184
            this.combiner = combiner;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   185
            this.characteristics = characteristics;
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
        CollectorImpl(Supplier<R> resultSupplier,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   189
                      BiFunction<R, T, R> accumulator,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   190
                      BinaryOperator<R> combiner) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   191
            this(resultSupplier, accumulator, combiner, Collections.emptySet());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   192
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   193
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   194
        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   195
        public BiFunction<R, T, R> accumulator() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   196
            return accumulator;
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
        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   200
        public Supplier<R> resultSupplier() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   201
            return resultSupplier;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   202
        }
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
        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   205
        public BinaryOperator<R> combiner() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   206
            return combiner;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   207
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   208
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   209
        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   210
        public Set<Characteristics> characteristics() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   211
            return characteristics;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   212
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   213
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   214
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   215
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   216
     * Returns a {@code Collector} that accumulates the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   217
     * new {@code Collection}, in encounter order.  The {@code Collection} is
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   218
     * created by the provided factory.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   219
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   220
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   221
     * @param <C> the type of the resulting {@code Collection}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   222
     * @param collectionFactory a {@code Supplier} which returns a new, empty
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   223
     * {@code Collection} of the appropriate type
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   224
     * @return a {@code Collector} which collects all the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   225
     * {@code Collection}, in encounter order
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   226
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   227
    public static <T, C extends Collection<T>>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   228
    Collector<T, C> toCollection(Supplier<C> collectionFactory) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   229
        return new CollectorImpl<>(collectionFactory,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   230
                                   (r, t) -> { r.add(t); return r; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   231
                                   (r1, r2) -> { r1.addAll(r2); return r1; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   232
                                   CH_STRICT);
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 List}. There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   238
     * serializability, or thread-safety of the {@code List} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   239
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   240
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   241
     * @return a {@code Collector} which collects all the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   242
     * {@code List}, in encounter order
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   243
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   244
    public static <T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   245
    Collector<T, List<T>> toList() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   246
        BiFunction<List<T>, T, List<T>> accumulator = (list, t) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   247
            switch (list.size()) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   248
                case 0:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   249
                    return Collections.singletonList(t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   250
                case 1:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   251
                    List<T> newList = new ArrayList<>();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   252
                    newList.add(list.get(0));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   253
                    newList.add(t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   254
                    return newList;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   255
                default:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   256
                    list.add(t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   257
                    return list;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   258
            }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   259
        };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   260
        BinaryOperator<List<T>> combiner = (left, right) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   261
            switch (left.size()) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   262
                case 0:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   263
                    return right;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   264
                case 1:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   265
                    List<T> newList = new ArrayList<>(left.size() + right.size());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   266
                    newList.addAll(left);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   267
                    newList.addAll(right);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   268
                    return newList;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   269
                default:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   270
                    left.addAll(right);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   271
                    return left;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   272
            }
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
        return new CollectorImpl<>(Collections::emptyList, accumulator, combiner);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   275
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   276
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   277
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   278
     * Returns a {@code Collector} that accumulates the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   279
     * new {@code Set}. There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   280
     * serializability, or thread-safety of the {@code Set} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   281
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   282
     * <p>This is an {@link Collector.Characteristics#UNORDERED unordered}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   283
     * Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   284
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   285
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   286
     * @return a {@code Collector} which collects all the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   287
     * {@code Set}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   288
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   289
    public static <T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   290
    Collector<T, Set<T>> toSet() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   291
        return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   292
                                   (r, t) -> { r.add(t); return r; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   293
                                   (r1, r2) -> { r1.addAll(r2); return r1; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   294
                                   CH_STRICT_UNORDERED);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   295
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   296
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   297
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   298
     * Returns a {@code Collector} that concatenates the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   299
     * new {@link StringBuilder}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   300
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   301
     * @return a {@code Collector} which collects String elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   302
     * {@code StringBuilder}, in encounter order
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
    public static Collector<String, StringBuilder> toStringBuilder() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   305
        return new CollectorImpl<>(StringBuilder::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   306
                                   (r, t) -> { r.append(t); return r; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   307
                                   (r1, r2) -> { r1.append(r2); return r1; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   308
                                   CH_STRICT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   309
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   310
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   311
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   312
     * Returns a {@code Collector} that concatenates the input elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   313
     * new {@link StringJoiner}, using the specified delimiter.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   314
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   315
     * @param delimiter the delimiter to be used between each element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   316
     * @return A {@code Collector} which collects String elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   317
     * {@code StringJoiner}, in encounter order
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   318
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   319
    public static Collector<CharSequence, StringJoiner> toStringJoiner(CharSequence delimiter) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   320
        BinaryOperator<StringJoiner> merger = (sj, other) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   321
            if (other.length() > 0)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   322
                sj.add(other.toString());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   323
            return sj;
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
        return new CollectorImpl<>(() -> new StringJoiner(delimiter),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   326
                                   (r, t) -> { r.add(t); return r; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   327
                                   merger, CH_STRICT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   328
    }
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
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   331
     * {@code BinaryOperator<Map>} that merges the contents of its right
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   332
     * argument into its left argument, using the provided merge function to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   333
     * handle duplicate keys.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   334
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   335
     * @param <K> type of the map keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   336
     * @param <V> type of the map values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   337
     * @param <M> type of the map
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   338
     * @param mergeFunction A merge function suitable for
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   339
     * {@link Map#merge(Object, Object, BiFunction) Map.merge()}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   340
     * @return a merge function for two maps
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   341
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   342
    private static <K, V, M extends Map<K,V>>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   343
    BinaryOperator<M> mapMerger(BinaryOperator<V> mergeFunction) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   344
        return (m1, m2) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   345
            for (Map.Entry<K,V> e : m2.entrySet())
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   346
                m1.merge(e.getKey(), e.getValue(), mergeFunction);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   347
            return m1;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   348
        };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   349
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   350
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   351
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   352
     * Adapts a {@code Collector<U,R>} to a {@code Collector<T,R>} by applying
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   353
     * a mapping function to each input element before accumulation.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   354
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   355
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   356
     * The {@code mapping()} collectors are most useful when used in a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   357
     * multi-level reduction, downstream of {@code groupingBy} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   358
     * {@code partitioningBy}.  For example, given a stream of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   359
     * {@code Person}, to accumulate the set of last names in each city:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   360
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   361
     *     Map<City, Set<String>> lastNamesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   362
     *         = people.stream().collect(groupingBy(Person::getCity,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   363
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   364
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   365
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   366
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   367
     * @param <U> type of elements accepted by downstream collector
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   368
     * @param <R> result type of collector
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   369
     * @param mapper a function to be applied to the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   370
     * @param downstream a collector which will accept mapped values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   371
     * @return a collector which applies the mapping function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   372
     * elements and provides the mapped results to the downstream collector
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   373
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   374
    public static <T, U, R> Collector<T, R>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   375
    mapping(Function<? super T, ? extends U> mapper, Collector<? super U, R> downstream) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   376
        BiFunction<R, ? super U, R> downstreamAccumulator = downstream.accumulator();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   377
        return new CollectorImpl<>(downstream.resultSupplier(),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   378
                                   (r, t) -> downstreamAccumulator.apply(r, mapper.apply(t)),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   379
                                   downstream.combiner(), downstream.characteristics());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   380
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   381
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   382
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   383
     * Returns a {@code Collector<T, Long>} that counts the number of input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   384
     * elements.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   385
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   386
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   387
     * This produces a result equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   388
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   389
     *     reducing(0L, e -> 1L, Long::sum)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   390
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   391
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   392
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   393
     * @return a {@code Collector} that counts the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   394
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   395
    public static <T> Collector<T, Long>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   396
    counting() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   397
        return reducing(0L, e -> 1L, Long::sum);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   398
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   399
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   400
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   401
     * Returns a {@code Collector<T, T>} that produces the minimal element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   402
     * according to a given {@code Comparator}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   403
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   404
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   405
     * This produces a result equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   406
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   407
     *     reducing(Comparators.lesserOf(comparator))
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   408
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   409
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   410
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   411
     * @param comparator a {@code Comparator} for comparing elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   412
     * @return a {@code Collector} that produces the minimal value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   413
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   414
    public static <T> Collector<T, T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   415
    minBy(Comparator<? super T> comparator) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   416
        return reducing(Comparators.lesserOf(comparator));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   417
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   418
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
     * Returns a {@code Collector<T, T>} that produces the maximal element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   421
     * according to a given {@code Comparator}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   422
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   423
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   424
     * This produces a result equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   425
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   426
     *     reducing(Comparators.greaterOf(comparator))
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   427
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   428
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   429
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   430
     * @param comparator a {@code Comparator} for comparing elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   431
     * @return a {@code Collector} that produces the maximal value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   432
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   433
    public static <T> Collector<T, T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   434
    maxBy(Comparator<? super T> comparator) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   435
        return reducing(Comparators.greaterOf(comparator));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   436
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   437
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
     * Returns a {@code Collector<T, Long>} that produces the sum of a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   440
     * long-valued function applied to the input element.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   441
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   442
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   443
     * This produces a result equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   444
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   445
     *     reducing(0L, mapper, Long::sum)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   446
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   447
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   448
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   449
     * @param mapper a function extracting the property to be summed
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   450
     * @return a {@code Collector} that produces the sum of a derived property
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   451
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   452
    public static <T> Collector<T, Long>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   453
    sumBy(Function<? super T, Long> mapper) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   454
        return reducing(0L, mapper, Long::sum);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   455
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   456
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   457
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   458
     * Returns a {@code Collector<T,T>} which performs a reduction of its
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   459
     * input elements under a specified {@code BinaryOperator}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   460
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   461
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   462
     * The {@code reducing()} collectors are most useful when used in a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   463
     * multi-level reduction, downstream of {@code groupingBy} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   464
     * {@code partitioningBy}.  To perform a simple reduction on a stream,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   465
     * use {@link Stream#reduce(BinaryOperator)} instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   466
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   467
     * @param <T> element type for the input and output of the reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   468
     * @param identity the identity value for the reduction (also, the value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   469
     *                 that is returned when there are no input elements)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   470
     * @param op a {@code BinaryOperator<T>} used to reduce the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   471
     * @return a {@code Collector} which implements the reduction operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   472
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   473
     * @see #reducing(BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   474
     * @see #reducing(Object, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   475
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   476
    public static <T> Collector<T, T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   477
    reducing(T identity, BinaryOperator<T> op) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   478
        return new CollectorImpl<>(() -> identity, (r, t) -> (r == null ? t : op.apply(r, t)), op);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   479
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   480
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   481
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   482
     * Returns a {@code Collector<T,T>} which performs a reduction of its
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   483
     * input elements under a specified {@code BinaryOperator}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   484
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   485
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   486
     * The {@code reducing()} collectors are most useful when used in a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   487
     * multi-level reduction, downstream of {@code groupingBy} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   488
     * {@code partitioningBy}.  To perform a simple reduction on a stream,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   489
     * use {@link Stream#reduce(BinaryOperator)} instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   490
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   491
     * <p>For example, given a stream of {@code Person}, to calculate tallest
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   492
     * person in each city:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   493
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   494
     *     Comparator<Person> byHeight = Comparators.comparing(Person::getHeight);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   495
     *     BinaryOperator<Person> tallerOf = Comparators.greaterOf(byHeight);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   496
     *     Map<City, Person> tallestByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   497
     *         = people.stream().collect(groupingBy(Person::getCity, reducing(tallerOf)));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   498
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   499
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   500
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   501
     * The default implementation is equivalent to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   502
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   503
     *     reducing(null, op);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   504
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   505
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   506
     * @param <T> element type for the input and output of the reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   507
     * @param op a {@code BinaryOperator<T>} used to reduce the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   508
     * @return a {@code Collector} which implements the reduction operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   509
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   510
     * @see #reducing(Object, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   511
     * @see #reducing(Object, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   512
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   513
    public static <T> Collector<T, T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   514
    reducing(BinaryOperator<T> op) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   515
        return reducing(null, op);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   516
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   517
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   518
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   519
     * Returns a {@code Collector<T,U>} which performs a reduction of its
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   520
     * input elements under a specified mapping function and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   521
     * {@code BinaryOperator}. This is a generalization of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   522
     * {@link #reducing(Object, BinaryOperator)} which allows a transformation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   523
     * of the elements before reduction.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   524
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   525
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   526
     * The {@code reducing()} collectors are most useful when used in a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   527
     * multi-level reduction, downstream of {@code groupingBy} or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   528
     * {@code partitioningBy}.  To perform a simple reduction on a stream,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   529
     * use {@link Stream#reduce(BinaryOperator)} instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   530
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   531
     * <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
   532
     * last name of residents in each city:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   533
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   534
     *     Comparator<String> byLength = Comparators.comparing(String::length);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   535
     *     BinaryOperator<String> longerOf = Comparators.greaterOf(byLength);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   536
     *     Map<City, String> longestLastNameByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   537
     *         = people.stream().collect(groupingBy(Person::getCity,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   538
     *                                              reducing(Person::getLastName, longerOf)));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   539
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   540
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   541
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   542
     * @param <U> the type of the mapped values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   543
     * @param identity the identity value for the reduction (also, the value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   544
     *                 that is returned when there are no input elements)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   545
     * @param mapper a mapping function to apply to each input value
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   546
     * @param op a {@code BinaryOperator<U>} used to reduce the mapped values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   547
     * @return a {@code Collector} implementing the map-reduce operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   548
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   549
     * @see #reducing(Object, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   550
     * @see #reducing(BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   551
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   552
    public static <T, U>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   553
    Collector<T, U> reducing(U identity,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   554
                             Function<? super T, ? extends U> mapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   555
                             BinaryOperator<U> op) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   556
        return new CollectorImpl<>(() -> identity,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   557
                                   (r, t) -> (r == null ? mapper.apply(t) : op.apply(r, mapper.apply(t))),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   558
                                   op);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   559
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   560
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   561
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   562
     * Returns a {@code Collector} implementing a "group by" operation on
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   563
     * input elements of type {@code T}, grouping elements according to a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   564
     * classification function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   565
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   566
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   567
     * 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
   568
     * values resulting from applying the classification function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   569
     * elements, and whose corresponding values are {@code List}s containing the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   570
     * input elements which map to the associated key under the classification
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   571
     * function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   572
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   573
     * <p>There are no guarantees on the type, mutability, serializability, or
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   574
     * thread-safety of the {@code Map} or {@code List} objects returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   575
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   576
     * This produces a result similar to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   577
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   578
     *     groupingBy(classifier, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   579
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   580
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   581
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   582
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   583
     * @param classifier the classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   584
     * @return a {@code Collector} implementing the group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   585
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   586
     * @see #groupingBy(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   587
     * @see #groupingBy(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   588
     * @see #groupingByConcurrent(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   589
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   590
    public static <T, K>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   591
    Collector<T, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   592
        return groupingBy(classifier, HashMap::new, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   593
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   594
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   595
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   596
     * Returns a {@code Collector} implementing a cascaded "group by" operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   597
     * on input elements of type {@code T}, grouping elements according to a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   598
     * classification function, and then performing a reduction operation on
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   599
     * the values associated with a given key using the specified downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   600
     * {@code Collector}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   601
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   602
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   603
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   604
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   605
     * {@code Map<K, D>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   606
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   607
     * <p>There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   608
     * serializability, or thread-safety of the {@code Map} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   609
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   610
     * <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
   611
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   612
     *     Map<City, Set<String>> namesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   613
     *         = people.stream().collect(groupingBy(Person::getCity,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   614
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   615
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   616
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   617
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   618
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   619
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   620
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   621
     * @param downstream a {@code Collector} implementing the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   622
     * @return a {@code Collector} implementing the cascaded group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   623
     * @see #groupingBy(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   624
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   625
     * @see #groupingBy(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   626
     * @see #groupingByConcurrent(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   627
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   628
    public static <T, K, D>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   629
    Collector<T, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   630
                                       Collector<? super T, D> downstream) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   631
        return groupingBy(classifier, HashMap::new, downstream);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   632
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   633
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   634
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   635
     * Returns a {@code Collector} implementing a cascaded "group by" operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   636
     * on input elements of type {@code T}, grouping elements according to a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   637
     * classification function, and then performing a reduction operation on
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   638
     * the values associated with a given key using the specified downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   639
     * {@code Collector}.  The {@code Map} produced by the Collector is created
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   640
     * with the supplied factory function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   641
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   642
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   643
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   644
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   645
     * {@code Map<K, D>}.
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
     * <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
   648
     * where the city names are sorted:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   649
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   650
     *     Map<City, Set<String>> namesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   651
     *         = people.stream().collect(groupingBy(Person::getCity, TreeMap::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   652
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   653
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   654
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   655
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   656
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   657
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   658
     * @param <M> the type of the resulting {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   659
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   660
     * @param downstream a {@code Collector} implementing the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   661
     * @param mapFactory a function which, when called, produces a new empty
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   662
     *                   {@code Map} of the desired type
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   663
     * @return a {@code Collector} implementing the cascaded group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   664
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   665
     * @see #groupingBy(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   666
     * @see #groupingBy(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   667
     * @see #groupingByConcurrent(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   668
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   669
    public static <T, K, D, M extends Map<K, D>>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   670
    Collector<T, M> groupingBy(Function<? super T, ? extends K> classifier,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   671
                               Supplier<M> mapFactory,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   672
                               Collector<? super T, D> downstream) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   673
        Supplier<D> downstreamSupplier = downstream.resultSupplier();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   674
        BiFunction<D, ? super T, D> downstreamAccumulator = downstream.accumulator();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   675
        BiFunction<M, T, M> accumulator = (m, t) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   676
            K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   677
            D oldContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   678
            D newContainer = downstreamAccumulator.apply(oldContainer, t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   679
            if (newContainer != oldContainer)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   680
                m.put(key, newContainer);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   681
            return m;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   682
        };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   683
        return new CollectorImpl<>(mapFactory, accumulator, mapMerger(downstream.combiner()), CH_STRICT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   684
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   685
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   686
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   687
     * Returns a {@code Collector} implementing a concurrent "group by"
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   688
     * operation on input elements of type {@code T}, grouping elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   689
     * according to a classification function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   690
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   691
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   692
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
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
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   695
     * 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
   696
     * values resulting from applying the classification function to the input
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   697
     * elements, and whose corresponding values are {@code List}s containing the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   698
     * input elements which map to the associated key under the classification
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   699
     * function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   700
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   701
     * <p>There are no guarantees on the type, mutability, or serializability
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   702
     * of the {@code Map} or {@code List} objects returned, or of the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   703
     * thread-safety of the {@code List} objects returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   704
     * @implSpec
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   705
     * This produces a result similar to:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   706
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   707
     *     groupingByConcurrent(classifier, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   708
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   709
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   710
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   711
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   712
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   713
     * @return a {@code Collector} implementing the group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   714
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   715
     * @see #groupingBy(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   716
     * @see #groupingByConcurrent(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   717
     * @see #groupingByConcurrent(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   718
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   719
    public static <T, K>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   720
    Collector<T, ConcurrentMap<K, List<T>>> groupingByConcurrent(Function<? super T, ? extends K> classifier) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   721
        return groupingByConcurrent(classifier, ConcurrentHashMap::new, toList());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   722
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   723
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   724
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   725
     * Returns a {@code Collector} implementing a concurrent cascaded "group by"
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   726
     * operation on input elements of type {@code T}, grouping elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   727
     * according to a classification function, and then performing a reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   728
     * operation on the values associated with a given key using the specified
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   729
     * downstream {@code Collector}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   730
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   731
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   732
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   733
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   734
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   735
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   736
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   737
     * {@code Map<K, D>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   738
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   739
     * <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
   740
     * where the city names are sorted:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   741
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   742
     *     ConcurrentMap<City, Set<String>> namesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   743
     *         = people.stream().collect(groupingByConcurrent(Person::getCity, TreeMap::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   744
     *                                                        mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   745
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   746
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   747
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   748
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   749
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   750
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   751
     * @param downstream a {@code Collector} implementing the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   752
     * @return a {@code Collector} implementing the cascaded group-by operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   753
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   754
     * @see #groupingBy(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   755
     * @see #groupingByConcurrent(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   756
     * @see #groupingByConcurrent(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   757
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   758
    public static <T, K, D>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   759
    Collector<T, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   760
                                                           Collector<? super T, D> downstream) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   761
        return groupingByConcurrent(classifier, ConcurrentHashMap::new, downstream);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   762
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   763
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
     * Returns a concurrent {@code Collector} implementing a cascaded "group by"
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   766
     * operation on input elements of type {@code T}, grouping elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   767
     * according to a classification function, and then performing a reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   768
     * operation on the values associated with a given key using the specified
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   769
     * downstream {@code Collector}.  The {@code ConcurrentMap} produced by the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   770
     * Collector is created with the supplied factory function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   771
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   772
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   773
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   774
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   775
     * <p>The classification function maps elements to some key type {@code K}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   776
     * The downstream collector operates on elements of type {@code T} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   777
     * produces a result of type {@code D}. The resulting collector produces a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   778
     * {@code Map<K, D>}.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   779
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   780
     * <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
   781
     * where the city names are sorted:
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
     *     ConcurrentMap<City, Set<String>> namesByCity
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   784
     *         = people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   785
     *                                              mapping(Person::getLastName, toSet())));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   786
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   787
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   788
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   789
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   790
     * @param <K> the type of the keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   791
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   792
     * @param <M> the type of the resulting {@code ConcurrentMap}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   793
     * @param classifier a classifier function mapping input elements to keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   794
     * @param downstream a {@code Collector} implementing the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   795
     * @param mapFactory a function which, when called, produces a new empty
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   796
     *                   {@code ConcurrentMap} of the desired type
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   797
     * @return a {@code Collector} implementing the cascaded 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 #groupingByConcurrent(Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   800
     * @see #groupingByConcurrent(Function, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   801
     * @see #groupingBy(Function, Supplier, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   802
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   803
    public static <T, K, D, M extends ConcurrentMap<K, D>>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   804
    Collector<T, M> groupingByConcurrent(Function<? super T, ? extends K> classifier,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   805
                                         Supplier<M> mapFactory,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   806
                                         Collector<? super T, D> downstream) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   807
        Supplier<D> downstreamSupplier = downstream.resultSupplier();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   808
        BiFunction<D, ? super T, D> downstreamAccumulator = downstream.accumulator();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   809
        BinaryOperator<M> combiner = mapMerger(downstream.combiner());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   810
        if (downstream.characteristics().contains(Collector.Characteristics.CONCURRENT)) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   811
            BiFunction<M, T, M> accumulator = (m, t) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   812
                K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   813
                downstreamAccumulator.apply(m.computeIfAbsent(key, k -> downstreamSupplier.get()), t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   814
                return m;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   815
            };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   816
            return new CollectorImpl<>(mapFactory, accumulator, combiner, CH_CONCURRENT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   817
        } else if (downstream.characteristics().contains(Collector.Characteristics.STRICTLY_MUTATIVE)) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   818
            BiFunction<M, T, M> accumulator = (m, t) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   819
                K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   820
                D resultContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   821
                synchronized (resultContainer) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   822
                    downstreamAccumulator.apply(resultContainer, t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   823
                }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   824
                return m;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   825
            };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   826
            return new CollectorImpl<>(mapFactory, accumulator, combiner, CH_CONCURRENT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   827
        } else {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   828
            BiFunction<M, T, M> accumulator = (m, t) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   829
                K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   830
                do {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   831
                    D oldResult = m.computeIfAbsent(key, k -> downstreamSupplier.get());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   832
                    if (oldResult == null) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   833
                        if (m.putIfAbsent(key, downstreamAccumulator.apply(null, t)) == null)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   834
                            return m;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   835
                    } else {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   836
                        synchronized (oldResult) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   837
                            if (m.get(key) != oldResult)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   838
                                continue;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   839
                            D newResult = downstreamAccumulator.apply(oldResult, t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   840
                            if (oldResult != newResult)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   841
                                m.put(key, newResult);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   842
                            return m;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   843
                        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   844
                    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   845
                } while (true);
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
            return new CollectorImpl<>(mapFactory, accumulator, combiner, CH_CONCURRENT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   848
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   849
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   850
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   851
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   852
     * Returns a {@code Collector} which partitions the input elements according
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   853
     * to a {@code Predicate}, and organizes them into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   854
     * {@code Map<Boolean, List<T>>}.
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
     * There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   857
     * serializability, or thread-safety of the {@code Map} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   858
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   859
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   860
     * @param predicate a predicate used for classifying input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   861
     * @return a {@code Collector} implementing the partitioning operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   862
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   863
     * @see #partitioningBy(Predicate, Collector)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   864
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   865
    public static <T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   866
    Collector<T, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   867
        return partitioningBy(predicate, toList());
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
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   870
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   871
     * Returns a {@code Collector} which partitions the input elements according
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   872
     * to a {@code Predicate}, reduces the values in each partition according to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   873
     * another {@code Collector}, and organizes them into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   874
     * {@code Map<Boolean, D>} whose values are the result of the downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   875
     * reduction.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   876
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   877
     * <p>There are no guarantees on the type, mutability,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   878
     * serializability, or thread-safety of the {@code Map} returned.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   879
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   880
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   881
     * @param <D> the result type of the downstream reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   882
     * @param predicate a predicate used for classifying input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   883
     * @param downstream a {@code Collector} implementing the downstream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   884
     *                   reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   885
     * @return a {@code Collector} implementing the cascaded partitioning
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   886
     *         operation
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   887
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   888
     * @see #partitioningBy(Predicate)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   889
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   890
    public static <T, D>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   891
    Collector<T, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   892
                                                 Collector<? super T, D> downstream) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   893
        BiFunction<D, ? super T, D> downstreamAccumulator = downstream.accumulator();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   894
        BiFunction<Map<Boolean, D>, T, Map<Boolean, D>> accumulator = (result, t) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   895
            Partition<D> asPartition = ((Partition<D>) result);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   896
            if (predicate.test(t)) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   897
                D newResult = downstreamAccumulator.apply(asPartition.forTrue, t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   898
                if (newResult != asPartition.forTrue)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   899
                    asPartition.forTrue = newResult;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   900
            } else {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   901
                D newResult = downstreamAccumulator.apply(asPartition.forFalse, t);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   902
                if (newResult != asPartition.forFalse)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   903
                    asPartition.forFalse = newResult;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   904
            }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   905
            return result;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   906
        };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   907
        return new CollectorImpl<>(() -> new Partition<>(downstream.resultSupplier().get(),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   908
                                                         downstream.resultSupplier().get()),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   909
                                   accumulator, partitionMerger(downstream.combiner()), CH_STRICT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   910
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   911
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   912
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   913
     * Merge function for two partitions, given a merge function for the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   914
     * elements.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   915
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   916
    private static <D> BinaryOperator<Map<Boolean, D>> partitionMerger(BinaryOperator<D> op) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   917
        return (m1, m2) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   918
            Partition<D> left = (Partition<D>) m1;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   919
            Partition<D> right = (Partition<D>) m2;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   920
            if (left.forFalse == null)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   921
                left.forFalse = right.forFalse;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   922
            else if (right.forFalse != null)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   923
                left.forFalse = op.apply(left.forFalse, right.forFalse);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   924
            if (left.forTrue == null)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   925
                left.forTrue = right.forTrue;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   926
            else if (right.forTrue != null)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   927
                left.forTrue = op.apply(left.forTrue, right.forTrue);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   928
            return left;
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
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   932
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   933
     * Accumulate elements into a {@code Map} whose keys and values are the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   934
     * result of applying mapping functions to the input elements.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   935
     * If the mapped keys contains duplicates (according to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   936
     * {@link Object#equals(Object)}), an {@code IllegalStateException} is
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   937
     * thrown when the collection operation is performed.  If the mapped keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   938
     * may have duplicates, use {@link #toMap(Function, Function, BinaryOperator)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   939
     * instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   940
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   941
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   942
     * 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
   943
     * In this case, the utility method
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   944
     * {@link java.util.function.Function#identity()} may be helpful.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   945
     * For example, the following produces a {@code Map} mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   946
     * students to their grade point average:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   947
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   948
     *     Map<Student, Double> studentToGPA
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   949
     *         students.stream().collect(toMap(Functions.identity(),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   950
     *                                         student -> computeGPA(student)));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   951
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   952
     * And the following produces a {@code Map} mapping a unique identifier to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   953
     * students:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   954
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   955
     *     Map<String, Student> studentIdToStudent
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   956
     *         students.stream().collect(toMap(Student::getId,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   957
     *                                         Functions.identity());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   958
     * }</pre>
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
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   961
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   962
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   963
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   964
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   965
     * @return a {@code Collector} which collects elements into a {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   966
     * whose keys and values are the result of applying mapping functions to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   967
     * the input elements
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
     * @see #toMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   970
     * @see #toMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   971
     * @see #toConcurrentMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   972
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   973
    public static <T, K, U>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   974
    Collector<T, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   975
                                 Function<? super T, ? extends U> valueMapper) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   976
        return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   977
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   978
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
     * Accumulate elements into a {@code Map} whose keys and values are the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   981
     * result of applying mapping functions to the input elements. If the mapped
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   982
     * keys contains duplicates (according to {@link Object#equals(Object)}),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   983
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   984
     * results are merged using the provided merging function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   985
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   986
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   987
     * There are multiple ways to deal with collisions between multiple elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   988
     * mapping to the same key.  There are some predefined merging functions,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   989
     * such as {@link #throwingMerger()}, {@link #firstWinsMerger()}, and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   990
     * {@link #lastWinsMerger()}, that implement common policies, or you can
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   991
     * implement custom policies easily.  For example, if you have a stream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   992
     * 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
   993
     * 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
   994
     * do as follows to gracefully deals with these collisions, and produce a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   995
     * {@code Map} mapping names to a concatenated list of addresses:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   996
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   997
     *     Map<String, String> phoneBook
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   998
     *         people.stream().collect(toMap(Person::getName,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
   999
     *                                       Person::getAddress,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1000
     *                                       (s, a) -> s + ", " + a));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1001
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1002
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1003
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1004
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1005
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1006
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1007
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1008
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1009
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1010
     *                      to {@link Map#merge(Object, Object, BiFunction)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1011
     * @return a {@code Collector} which collects elements into a {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1012
     * 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
  1013
     * elements, and whose values are the result of applying a value mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1014
     * function to all input elements equal to the key and combining them
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1015
     * using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1016
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1017
     * @see #toMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1018
     * @see #toMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1019
     * @see #toConcurrentMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1020
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1021
    public static <T, K, U>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1022
    Collector<T, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1023
                                 Function<? super T, ? extends U> valueMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1024
                                 BinaryOperator<U> mergeFunction) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1025
        return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
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
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1028
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1029
     * Accumulate elements into a {@code Map} whose keys and values are the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1030
     * result of applying mapping functions to the input elements. If the mapped
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1031
     * keys contains duplicates (according to {@link Object#equals(Object)}),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1032
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1033
     * results are merged using the provided merging function.  The {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1034
     * is created by a provided supplier function.
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 output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1038
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1039
     * @param <M> the type of the resulting {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1040
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1041
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1042
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1043
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1044
     *                      to {@link Map#merge(Object, Object, BiFunction)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1045
     * @param mapSupplier a function which returns a new, empty {@code Map} into
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1046
     *                    which the results will be inserted
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1047
     * @return a {@code Collector} which collects elements into a {@code Map}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1048
     * 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
  1049
     * elements, and whose values are the result of applying a value mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1050
     * function to all input elements equal to the key and combining them
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1051
     * using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1052
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1053
     * @see #toMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1054
     * @see #toMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1055
     * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1056
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1057
    public static <T, K, U, M extends Map<K, U>>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1058
    Collector<T, M> toMap(Function<? super T, ? extends K> keyMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1059
                          Function<? super T, ? extends U> valueMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1060
                          BinaryOperator<U> mergeFunction,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1061
                          Supplier<M> mapSupplier) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1062
        BiFunction<M, T, M> accumulator
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1063
                = (map, element) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1064
                      map.merge(keyMapper.apply(element), valueMapper.apply(element), mergeFunction);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1065
                      return map;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1066
                  };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1067
        return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_STRICT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1068
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1069
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1070
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1071
     * Accumulate elements into a {@code ConcurrentMap} whose keys and values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1072
     * are the result of applying mapping functions to the input elements.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1073
     * If the mapped keys contains duplicates (according to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1074
     * {@link Object#equals(Object)}), an {@code IllegalStateException} is
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1075
     * thrown when the collection operation is performed.  If the mapped keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1076
     * may have duplicates, use
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1077
     * {@link #toConcurrentMap(Function, Function, BinaryOperator)} instead.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1078
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1079
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1080
     * 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
  1081
     * In this case, the utility method
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1082
     * {@link java.util.function.Function#identity()} may be helpful.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1083
     * For example, the following produces a {@code Map} mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1084
     * students to their grade point average:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1085
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1086
     *     Map<Student, Double> studentToGPA
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1087
     *         students.stream().collect(toMap(Functions.identity(),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1088
     *                                         student -> computeGPA(student)));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1089
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1090
     * And the following produces a {@code Map} mapping a unique identifier to
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1091
     * students:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1092
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1093
     *     Map<String, Student> studentIdToStudent
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1094
     *         students.stream().collect(toConcurrentMap(Student::getId,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1095
     *                                                   Functions.identity());
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1096
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1097
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1098
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1099
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1100
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1101
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1102
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1103
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1104
     * @param keyMapper the mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1105
     * @param valueMapper the mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1106
     * @return a concurrent {@code Collector} which collects elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1107
     * {@code ConcurrentMap} whose keys are the result of applying a key mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1108
     * function to the input elements, and whose values are the result of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1109
     * applying a value mapping function to the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1110
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1111
     * @see #toMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1112
     * @see #toConcurrentMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1113
     * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1114
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1115
    public static <T, K, U>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1116
    Collector<T, ConcurrentMap<K,U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1117
                                                     Function<? super T, ? extends U> valueMapper) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1118
        return toConcurrentMap(keyMapper, valueMapper, throwingMerger(), ConcurrentHashMap::new);
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
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1121
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1122
     * Accumulate elements into a {@code ConcurrentMap} whose keys and values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1123
     * are the result of applying mapping functions to the input elements. If
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1124
     * the mapped keys contains duplicates (according to {@link Object#equals(Object)}),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1125
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1126
     * results are merged using the provided merging function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1127
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1128
     * @apiNote
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1129
     * There are multiple ways to deal with collisions between multiple elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1130
     * mapping to the same key.  There are some predefined merging functions,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1131
     * such as {@link #throwingMerger()}, {@link #firstWinsMerger()}, and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1132
     * {@link #lastWinsMerger()}, that implement common policies, or you can
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1133
     * implement custom policies easily.  For example, if you have a stream
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1134
     * 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
  1135
     * 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
  1136
     * do as follows to gracefully deals with these collisions, and produce a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1137
     * {@code Map} mapping names to a concatenated list of addresses:
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1138
     * <pre>{@code
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1139
     *     Map<String, String> phoneBook
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1140
     *         people.stream().collect(toConcurrentMap(Person::getName,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1141
     *                                                 Person::getAddress,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1142
     *                                                 (s, a) -> s + ", " + a));
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1143
     * }</pre>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1144
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1145
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1146
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1147
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1148
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1149
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1150
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1151
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1152
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1153
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1154
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1155
     *                      to {@link Map#merge(Object, Object, BiFunction)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1156
     * @return a concurrent {@code Collector} which collects elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1157
     * {@code ConcurrentMap} whose keys are the result of applying a key mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1158
     * function to the input elements, and whose values are the result of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1159
     * 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
  1160
     * and combining them using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1161
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1162
     * @see #toConcurrentMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1163
     * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1164
     * @see #toMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1165
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1166
    public static <T, K, U>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1167
    Collector<T, ConcurrentMap<K,U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1168
                                                     Function<? super T, ? extends U> valueMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1169
                                                     BinaryOperator<U> mergeFunction) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1170
        return toConcurrentMap(keyMapper, valueMapper, mergeFunction, ConcurrentHashMap::new);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1171
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1172
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1173
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1174
     * Accumulate elements into a {@code ConcurrentMap} whose keys and values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1175
     * are the result of applying mapping functions to the input elements. If
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1176
     * the mapped keys contains duplicates (according to {@link Object#equals(Object)}),
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1177
     * the value mapping function is applied to each equal element, and the
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1178
     * results are merged using the provided merging function.  The
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1179
     * {@code ConcurrentMap} is created by a provided supplier function.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1180
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1181
     * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1182
     * {@link Collector.Characteristics#UNORDERED unordered} Collector.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1183
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1184
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1185
     * @param <K> the output type of the key mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1186
     * @param <U> the output type of the value mapping function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1187
     * @param <M> the type of the resulting {@code ConcurrentMap}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1188
     * @param keyMapper a mapping function to produce keys
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1189
     * @param valueMapper a mapping function to produce values
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1190
     * @param mergeFunction a merge function, used to resolve collisions between
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1191
     *                      values associated with the same key, as supplied
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1192
     *                      to {@link Map#merge(Object, Object, BiFunction)}
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1193
     * @param mapSupplier a function which returns a new, empty {@code Map} into
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1194
     *                    which the results will be inserted
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1195
     * @return a concurrent {@code Collector} which collects elements into a
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1196
     * {@code ConcurrentMap} whose keys are the result of applying a key mapping
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1197
     * function to the input elements, and whose values are the result of
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1198
     * 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
  1199
     * and combining them using the merge function
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1200
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1201
     * @see #toConcurrentMap(Function, Function)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1202
     * @see #toConcurrentMap(Function, Function, BinaryOperator)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1203
     * @see #toMap(Function, Function, BinaryOperator, Supplier)
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
    public static <T, K, U, M extends ConcurrentMap<K, U>>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1206
    Collector<T, M> toConcurrentMap(Function<? super T, ? extends K> keyMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1207
                                    Function<? super T, ? extends U> valueMapper,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1208
                                    BinaryOperator<U> mergeFunction,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1209
                                    Supplier<M> mapSupplier) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1210
        BiFunction<M, T, M> accumulator = (map, element) -> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1211
            map.merge(keyMapper.apply(element), valueMapper.apply(element), mergeFunction);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1212
            return map;
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
        return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_CONCURRENT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1215
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1216
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1217
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1218
     * Returns a {@code Collector} which applies an {@code int}-producing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1219
     * mapping function to each input element, and returns summary statistics
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1220
     * for the resulting values.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1221
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1222
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1223
     * @param mapper a mapping function to apply to each element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1224
     * @return a {@code Collector} implementing the summary-statistics reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1225
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1226
     * @see #toDoubleSummaryStatistics(ToDoubleFunction)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1227
     * @see #toLongSummaryStatistics(ToLongFunction)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1228
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1229
    public static <T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1230
    Collector<T, IntSummaryStatistics> toIntSummaryStatistics(ToIntFunction<? super T> mapper) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1231
        return new CollectorImpl<>(IntSummaryStatistics::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1232
                                   (r, t) -> { r.accept(mapper.applyAsInt(t)); return r; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1233
                                   (l, r) -> { l.combine(r); return l; }, CH_STRICT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1234
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1235
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1236
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1237
     * Returns a {@code Collector} which applies an {@code long}-producing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1238
     * mapping function to each input element, and returns summary statistics
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1239
     * for the resulting values.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1240
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1241
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1242
     * @param mapper the mapping function to apply to each element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1243
     * @return a {@code Collector} implementing the summary-statistics reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1244
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1245
     * @see #toDoubleSummaryStatistics(ToDoubleFunction)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1246
     * @see #toIntSummaryStatistics(ToIntFunction)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1247
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1248
    public static <T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1249
    Collector<T, LongSummaryStatistics> toLongSummaryStatistics(ToLongFunction<? super T> mapper) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1250
        return new CollectorImpl<>(LongSummaryStatistics::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1251
                                   (r, t) -> { r.accept(mapper.applyAsLong(t)); return r; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1252
                                   (l, r) -> { l.combine(r); return l; }, CH_STRICT);
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1253
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1254
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1255
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1256
     * Returns a {@code Collector} which applies an {@code double}-producing
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1257
     * mapping function to each input element, and returns summary statistics
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1258
     * for the resulting values.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1259
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1260
     * @param <T> the type of the input elements
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1261
     * @param mapper a mapping function to apply to each element
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1262
     * @return a {@code Collector} implementing the summary-statistics reduction
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1263
     *
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1264
     * @see #toLongSummaryStatistics(ToLongFunction)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1265
     * @see #toIntSummaryStatistics(ToIntFunction)
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>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1268
    Collector<T, DoubleSummaryStatistics> toDoubleSummaryStatistics(ToDoubleFunction<? super T> mapper) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1269
        return new CollectorImpl<>(DoubleSummaryStatistics::new,
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1270
                                   (r, t) -> { r.accept(mapper.applyAsDouble(t)); return r; },
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1271
                                   (l, r) -> { l.combine(r); return l; }, CH_STRICT);
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
    /**
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1275
     * Implementation class used by partitioningBy.
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1276
     */
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1277
    private static final class Partition<T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1278
            extends AbstractMap<Boolean, T>
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1279
            implements Map<Boolean, T> {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1280
        T forTrue;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1281
        T forFalse;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1282
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1283
        Partition(T forTrue, T forFalse) {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1284
            this.forTrue = forTrue;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1285
            this.forFalse = forFalse;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1286
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1287
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1288
        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1289
        public Set<Map.Entry<Boolean, T>> entrySet() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1290
            return new AbstractSet<Map.Entry<Boolean, T>>() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1291
                @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1292
                public Iterator<Map.Entry<Boolean, T>> iterator() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1293
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1294
                    return new Iterator<Map.Entry<Boolean, T>>() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1295
                        int state = 0;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1296
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1297
                        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1298
                        public boolean hasNext() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1299
                            return state < 2;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1300
                        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1301
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1302
                        @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1303
                        public Map.Entry<Boolean, T> next() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1304
                            if (state >= 2)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1305
                                throw new NoSuchElementException();
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1306
                            return (state++ == 0)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1307
                                   ? new SimpleImmutableEntry<>(false, forFalse)
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1308
                                   : new SimpleImmutableEntry<>(true, forTrue);
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
                    };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1311
                }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1312
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1313
                @Override
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1314
                public int size() {
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1315
                    return 2;
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1316
                }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1317
            };
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1318
        }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1319
    }
ef8b87b1597c 8011917: Add java.util.stream.Collectors utilities
mduigou
parents:
diff changeset
  1320
}