src/java.base/share/classes/java/util/stream/Collector.java
author jboes
Thu, 03 Oct 2019 18:59:56 +0100
changeset 58459 e25b317d0350
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231161: Wrong return type in code sample in Collector API documentation Summary: Correct declaration of container from R to A and add compilation test Reviewed-by: smarks, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     1
/*
58459
e25b317d0350 8231161: Wrong return type in code sample in Collector API documentation
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     4
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    10
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    16
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    20
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    23
 * questions.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    24
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    25
package java.util.stream;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    26
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    27
import java.util.Collections;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    28
import java.util.EnumSet;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    29
import java.util.Objects;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    30
import java.util.Set;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    31
import java.util.function.BiConsumer;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    32
import java.util.function.BinaryOperator;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    33
import java.util.function.Function;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    34
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    35
58459
e25b317d0350 8231161: Wrong return type in code sample in Collector API documentation
jboes
parents: 47216
diff changeset
    36
// A compilation test for the code snippets in this class-level javadoc can be found at:
e25b317d0350 8231161: Wrong return type in code sample in Collector API documentation
jboes
parents: 47216
diff changeset
    37
// test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectorExample.java
e25b317d0350 8231161: Wrong return type in code sample in Collector API documentation
jboes
parents: 47216
diff changeset
    38
// The test needs to be updated if the examples in this javadoc change or new examples are added.
e25b317d0350 8231161: Wrong return type in code sample in Collector API documentation
jboes
parents: 47216
diff changeset
    39
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
/**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    41
 * A <a href="package-summary.html#Reduction">mutable reduction operation</a> that
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    42
 * accumulates input elements into a mutable result container, optionally transforming
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    43
 * the accumulated result into a final representation after all input elements
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    44
 * have been processed.  Reduction operations can be performed either sequentially
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    45
 * or in parallel.
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    46
 *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    47
 * <p>Examples of mutable reduction operations include:
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    48
 * accumulating elements into a {@code Collection}; concatenating
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    49
 * strings using a {@code StringBuilder}; computing summary information about
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    50
 * elements such as sum, min, max, or average; computing "pivot table" summaries
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    51
 * such as "maximum valued transaction by seller", etc.  The class {@link Collectors}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    52
 * provides implementations of many common mutable reductions.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    53
 *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    54
 * <p>A {@code Collector} is specified by four functions that work together to
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    55
 * accumulate entries into a mutable result container, and optionally perform
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    56
 * a final transform on the result.  They are: <ul>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    57
 *     <li>creation of a new result container ({@link #supplier()})</li>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    58
 *     <li>incorporating a new data element into a result container ({@link #accumulator()})</li>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    59
 *     <li>combining two result containers into one ({@link #combiner()})</li>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    60
 *     <li>performing an optional final transform on the container ({@link #finisher()})</li>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    61
 * </ul>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    62
 *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
    63
 * <p>Collectors also have a set of characteristics, such as
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    64
 * {@link Characteristics#CONCURRENT}, that provide hints that can be used by a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    65
 * reduction implementation to provide better performance.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    66
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    67
 * <p>A sequential implementation of a reduction using a collector would
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    68
 * create a single result container using the supplier function, and invoke the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    69
 * accumulator function once for each input element.  A parallel implementation
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    70
 * would partition the input, create a result container for each partition,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    71
 * accumulate the contents of each partition into a subresult for that partition,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    72
 * and then use the combiner function to merge the subresults into a combined
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    73
 * result.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    74
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    75
 * <p>To ensure that sequential and parallel executions produce equivalent
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    76
 * results, the collector functions must satisfy an <em>identity</em> and an
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    77
 * <a href="package-summary.html#Associativity">associativity</a> constraints.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    78
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    79
 * <p>The identity constraint says that for any partially accumulated result,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    80
 * combining it with an empty result container must produce an equivalent
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    81
 * result.  That is, for a partially accumulated result {@code a} that is the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    82
 * result of any series of accumulator and combiner invocations, {@code a} must
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    83
 * be equivalent to {@code combiner.apply(a, supplier.get())}.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    84
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    85
 * <p>The associativity constraint says that splitting the computation must
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    86
 * produce an equivalent result.  That is, for any input elements {@code t1}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    87
 * and {@code t2}, the results {@code r1} and {@code r2} in the computation
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    88
 * below must be equivalent:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    89
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    90
 *     A a1 = supplier.get();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    91
 *     accumulator.accept(a1, t1);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    92
 *     accumulator.accept(a1, t2);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    93
 *     R r1 = finisher.apply(a1);  // result without splitting
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    94
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    95
 *     A a2 = supplier.get();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    96
 *     accumulator.accept(a2, t1);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    97
 *     A a3 = supplier.get();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    98
 *     accumulator.accept(a3, t2);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
    99
 *     R r2 = finisher.apply(combiner.apply(a2, a3));  // result with splitting
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   100
 * } </pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   101
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   102
 * <p>For collectors that do not have the {@code UNORDERED} characteristic,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   103
 * two accumulated results {@code a1} and {@code a2} are equivalent if
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   104
 * {@code finisher.apply(a1).equals(finisher.apply(a2))}.  For unordered
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   105
 * collectors, equivalence is relaxed to allow for non-equality related to
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   106
 * differences in order.  (For example, an unordered collector that accumulated
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   107
 * elements to a {@code List} would consider two lists equivalent if they
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   108
 * contained the same elements, ignoring order.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   109
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   110
 * <p>Libraries that implement reduction based on {@code Collector}, such as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   111
 * {@link Stream#collect(Collector)}, must adhere to the following constraints:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   112
 * <ul>
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   113
 *     <li>The first argument passed to the accumulator function, both
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   114
 *     arguments passed to the combiner function, and the argument passed to the
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   115
 *     finisher function must be the result of a previous invocation of the
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   116
 *     result supplier, accumulator, or combiner functions.</li>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   117
 *     <li>The implementation should not do anything with the result of any of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   118
 *     the result supplier, accumulator, or combiner functions other than to
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   119
 *     pass them again to the accumulator, combiner, or finisher functions,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   120
 *     or return them to the caller of the reduction operation.</li>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   121
 *     <li>If a result is passed to the combiner or finisher
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   122
 *     function, and the same object is not returned from that function, it is
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   123
 *     never used again.</li>
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   124
 *     <li>Once a result is passed to the combiner or finisher function, it
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   125
 *     is never passed to the accumulator function again.</li>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   126
 *     <li>For non-concurrent collectors, any result returned from the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   127
 *     supplier, accumulator, or combiner functions must be serially
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   128
 *     thread-confined.  This enables collection to occur in parallel without
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   129
 *     the {@code Collector} needing to implement any additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   130
 *     The reduction implementation must manage that the input is properly
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   131
 *     partitioned, that partitions are processed in isolation, and combining
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   132
 *     happens only after accumulation is complete.</li>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   133
 *     <li>For concurrent collectors, an implementation is free to (but not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   134
 *     required to) implement reduction concurrently.  A concurrent reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   135
 *     is one where the accumulator function is called concurrently from
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   136
 *     multiple threads, using the same concurrently-modifiable result container,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   137
 *     rather than keeping the result isolated during accumulation.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   138
 *     A concurrent reduction should only be applied if the collector has the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   139
 *     {@link Characteristics#UNORDERED} characteristics or if the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   140
 *     originating data is unordered.</li>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   141
 * </ul>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   142
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   143
 * <p>In addition to the predefined implementations in {@link Collectors}, the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   144
 * static factory methods {@link #of(Supplier, BiConsumer, BinaryOperator, Characteristics...)}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   145
 * can be used to construct collectors.  For example, you could create a collector
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   146
 * that accumulates widgets into a {@code TreeSet} with:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   147
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   148
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   149
 *     Collector<Widget, ?, TreeSet<Widget>> intoSet =
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   150
 *         Collector.of(TreeSet::new, TreeSet::add,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   151
 *                      (left, right) -> { left.addAll(right); return left; });
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   152
 * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   153
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   154
 * (This behavior is also implemented by the predefined collector
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   155
 * {@link Collectors#toCollection(Supplier)}).
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   156
 *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   157
 * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   158
 * Performing a reduction operation with a {@code Collector} should produce a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   159
 * result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   160
 * <pre>{@code
58459
e25b317d0350 8231161: Wrong return type in code sample in Collector API documentation
jboes
parents: 47216
diff changeset
   161
 *     A container = collector.supplier().get();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   162
 *     for (T t : data)
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   163
 *         collector.accumulator().accept(container, t);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   164
 *     return collector.finisher().apply(container);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   165
 * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   166
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
 * <p>However, the library is free to partition the input, perform the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
 * on the partitions, and then use the combiner function to combine the partial
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   169
 * results to achieve a parallel reduction.  (Depending on the specific reduction
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
 * operation, this may perform better or worse, depending on the relative cost
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   171
 * of the accumulator and combiner functions.)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   172
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   173
 * <p>Collectors are designed to be <em>composed</em>; many of the methods
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   174
 * in {@link Collectors} are functions that take a collector and produce
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   175
 * a new collector.  For example, given the following collector that computes
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   176
 * the sum of the salaries of a stream of employees:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   177
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   178
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   179
 *     Collector<Employee, ?, Integer> summingSalaries
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   180
 *         = Collectors.summingInt(Employee::getSalary))
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   181
 * }</pre>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   183
 * If we wanted to create a collector to tabulate the sum of salaries by
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   184
 * department, we could reuse the "sum of salaries" logic using
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   185
 * {@link Collectors#groupingBy(Function, Collector)}:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   186
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   187
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   188
 *     Collector<Employee, ?, Map<Department, Integer>> summingSalariesByDept
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   189
 *         = Collectors.groupingBy(Employee::getDepartment, summingSalaries);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   190
 * }</pre>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   192
 * @see Stream#collect(Collector)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   193
 * @see Collectors
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   194
 *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   195
 * @param <T> the type of input elements to the reduction operation
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   196
 * @param <A> the mutable accumulation type of the reduction operation (often
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   197
 *            hidden as an implementation detail)
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   198
 * @param <R> the result type of the reduction operation
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   199
 * @since 1.8
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
 */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   201
public interface Collector<T, A, R> {
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   202
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   203
     * A function that creates and returns a new mutable result container.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   204
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   205
     * @return a function which returns a new, mutable result container
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   206
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   207
    Supplier<A> supplier();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   208
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   209
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   210
     * A function that folds a value into a mutable result container.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   211
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   212
     * @return a function which folds a value into a mutable result container
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   213
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   214
    BiConsumer<A, T> accumulator();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   215
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   216
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   217
     * A function that accepts two partial results and merges them.  The
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   218
     * combiner function may fold state from one argument into the other and
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   219
     * return that, or may return a new result container.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   220
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   221
     * @return a function which combines two partial results into a combined
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   222
     * result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   223
     */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   224
    BinaryOperator<A> combiner();
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   225
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   226
    /**
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   227
     * Perform the final transformation from the intermediate accumulation type
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   228
     * {@code A} to the final result type {@code R}.
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   229
     *
32002
e378c0dc767e 8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents: 25859
diff changeset
   230
     * <p>If the characteristic {@code IDENTITY_FINISH} is
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   231
     * set, this function may be presumed to be an identity transform with an
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   232
     * unchecked cast from {@code A} to {@code R}.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   233
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   234
     * @return a function which transforms the intermediate result to the final
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   235
     * result
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   236
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   237
    Function<A, R> finisher();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   238
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   239
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   240
     * Returns a {@code Set} of {@code Collector.Characteristics} indicating
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   241
     * the characteristics of this Collector.  This set should be immutable.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   242
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   243
     * @return an immutable set of collector characteristics
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   244
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   245
    Set<Characteristics> characteristics();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   246
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   247
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   248
     * Returns a new {@code Collector} described by the given {@code supplier},
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   249
     * {@code accumulator}, and {@code combiner} functions.  The resulting
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   250
     * {@code Collector} has the {@code Collector.Characteristics.IDENTITY_FINISH}
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   251
     * characteristic.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   252
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   253
     * @param supplier The supplier function for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   254
     * @param accumulator The accumulator function for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   255
     * @param combiner The combiner function for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   256
     * @param characteristics The collector characteristics for the new
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   257
     *                        collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   258
     * @param <T> The type of input elements for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   259
     * @param <R> The type of intermediate accumulation result, and final result,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   260
     *           for the new collector
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   261
     * @throws NullPointerException if any argument is null
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   262
     * @return the new {@code Collector}
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   263
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   264
    public static<T, R> Collector<T, R, R> of(Supplier<R> supplier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   265
                                              BiConsumer<R, T> accumulator,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   266
                                              BinaryOperator<R> combiner,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   267
                                              Characteristics... characteristics) {
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   268
        Objects.requireNonNull(supplier);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   269
        Objects.requireNonNull(accumulator);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   270
        Objects.requireNonNull(combiner);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   271
        Objects.requireNonNull(characteristics);
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   272
        Set<Characteristics> cs = (characteristics.length == 0)
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   273
                                  ? Collectors.CH_ID
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   274
                                  : Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   275
                                                                           characteristics));
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   276
        return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, cs);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   277
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   278
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   279
    /**
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   280
     * Returns a new {@code Collector} described by the given {@code supplier},
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   281
     * {@code accumulator}, {@code combiner}, and {@code finisher} functions.
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   282
     *
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   283
     * @param supplier The supplier function for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   284
     * @param accumulator The accumulator function for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   285
     * @param combiner The combiner function for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   286
     * @param finisher The finisher function for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   287
     * @param characteristics The collector characteristics for the new
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   288
     *                        collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   289
     * @param <T> The type of input elements for the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   290
     * @param <A> The intermediate accumulation type of the new collector
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   291
     * @param <R> The final result type of the new collector
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   292
     * @throws NullPointerException if any argument is null
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   293
     * @return the new {@code Collector}
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   294
     */
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   295
    public static<T, A, R> Collector<T, A, R> of(Supplier<A> supplier,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   296
                                                 BiConsumer<A, T> accumulator,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   297
                                                 BinaryOperator<A> combiner,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   298
                                                 Function<A, R> finisher,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   299
                                                 Characteristics... characteristics) {
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   300
        Objects.requireNonNull(supplier);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   301
        Objects.requireNonNull(accumulator);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   302
        Objects.requireNonNull(combiner);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   303
        Objects.requireNonNull(finisher);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   304
        Objects.requireNonNull(characteristics);
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   305
        Set<Characteristics> cs = Collectors.CH_NOID;
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   306
        if (characteristics.length > 0) {
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   307
            cs = EnumSet.noneOf(Characteristics.class);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   308
            Collections.addAll(cs, characteristics);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   309
            cs = Collections.unmodifiableSet(cs);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   310
        }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   311
        return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, finisher, cs);
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   312
    }
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   313
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   314
    /**
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   315
     * Characteristics indicating properties of a {@code Collector}, which can
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   316
     * be used to optimize reduction implementations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   317
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   318
    enum Characteristics {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   319
        /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   320
         * Indicates that this collector is <em>concurrent</em>, meaning that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   321
         * the result container can support the accumulator function being
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   322
         * called concurrently with the same result container from multiple
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   323
         * threads.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   324
         *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   325
         * <p>If a {@code CONCURRENT} collector is not also {@code UNORDERED},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   326
         * then it should only be evaluated concurrently if applied to an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   327
         * unordered data source.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   328
         */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   329
        CONCURRENT,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   330
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   331
        /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   332
         * Indicates that the collection operation does not commit to preserving
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   333
         * the encounter order of input elements.  (This might be true if the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19214
diff changeset
   334
         * result container has no intrinsic order, such as a {@link Set}.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   335
         */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   336
        UNORDERED,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   337
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   338
        /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   339
         * Indicates that the finisher function is the identity function and
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   340
         * can be elided.  If set, it must be the case that an unchecked cast
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   341
         * from A to R will succeed.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   342
         */
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17167
diff changeset
   343
        IDENTITY_FINISH
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   344
    }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   345
}