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