|
17 | 17 | package com.google.common.collect;
|
18 | 18 |
|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull;
|
| 20 | +import static com.google.common.collect.Sets.immutableEnumSet; |
| 21 | +import static java.util.Arrays.asList; |
20 | 22 | import static java.util.Collections.singletonMap;
|
21 | 23 | import static java.util.stream.Collectors.collectingAndThen;
|
22 | 24 | import static java.util.stream.Collectors.toMap;
|
23 | 25 |
|
24 | 26 | import com.google.common.annotations.GwtCompatible;
|
25 | 27 | import com.google.common.annotations.GwtIncompatible;
|
26 | 28 | import com.google.common.base.Preconditions;
|
| 29 | +import com.google.common.primitives.Ints; |
27 | 30 | import java.util.Collection;
|
28 | 31 | import java.util.Comparator;
|
29 | 32 | import java.util.EnumMap;
|
30 | 33 | import java.util.EnumSet;
|
31 | 34 | import java.util.LinkedHashMap;
|
| 35 | +import java.util.Set; |
32 | 36 | import java.util.TreeMap;
|
| 37 | +import java.util.function.BiConsumer; |
33 | 38 | import java.util.function.BinaryOperator;
|
34 | 39 | import java.util.function.Function;
|
| 40 | +import java.util.function.LongFunction; |
35 | 41 | import java.util.function.Supplier;
|
36 | 42 | import java.util.function.ToIntFunction;
|
37 | 43 | import java.util.stream.Collector;
|
| 44 | +import java.util.stream.Collector.Characteristics; |
38 | 45 | import java.util.stream.Stream;
|
39 | 46 | import org.jspecify.annotations.Nullable;
|
40 | 47 |
|
|
43 | 50 | @SuppressWarnings("Java7ApiChecker")
|
44 | 51 | @IgnoreJRERequirement // used only from APIs with Java 8 types in them
|
45 | 52 | final class CollectCollectors {
|
| 53 | + private static < |
| 54 | + T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object> |
| 55 | + Collector<T, A, R> sizedCollector( |
| 56 | + Supplier<A> supplier, |
| 57 | + LongFunction<A> sizedSupplier, |
| 58 | + BiConsumer<A, T> accumulator, |
| 59 | + BinaryOperator<A> combiner, |
| 60 | + Function<A, R> finisher, |
| 61 | + Characteristics... characteristics) { |
| 62 | + Set<Characteristics> characteristicsSet = immutableEnumSet(asList(characteristics)); |
| 63 | + return new Collector<T, A, R>() { |
| 64 | + @Override |
| 65 | + public Supplier<A> supplier() { |
| 66 | + return supplier; |
| 67 | + } |
| 68 | + |
| 69 | + @SuppressWarnings("MissingOverride") // only under some future version of Java? |
| 70 | + public LongFunction<A> sizedSupplier() { |
| 71 | + return sizedSupplier; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public BiConsumer<A, T> accumulator() { |
| 76 | + return accumulator; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public BinaryOperator<A> combiner() { |
| 81 | + return combiner; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Function<A, R> finisher() { |
| 86 | + return finisher; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Set<Characteristics> characteristics() { |
| 91 | + return characteristicsSet; |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
46 | 95 |
|
47 | 96 | private static final Collector<Object, ?, ImmutableList<Object>> TO_IMMUTABLE_LIST =
|
48 |
| - Collector.of( |
| 97 | + sizedCollector( |
49 | 98 | ImmutableList::builder,
|
| 99 | + size -> |
| 100 | + size == -1 |
| 101 | + ? ImmutableList.builder() |
| 102 | + : ImmutableList.builderWithExpectedSize(Ints.checkedCast(size)), |
50 | 103 | ImmutableList.Builder::add,
|
51 | 104 | ImmutableList.Builder::combine,
|
52 | 105 | ImmutableList.Builder::build);
|
@@ -191,8 +244,12 @@ ImmutableSet<E> toImmutableSet() {
|
191 | 244 | Function<? super T, ? extends V> valueFunction) {
|
192 | 245 | checkNotNull(keyFunction);
|
193 | 246 | checkNotNull(valueFunction);
|
194 |
| - return Collector.of( |
| 247 | + return sizedCollector( |
195 | 248 | ImmutableMap.Builder<K, V>::new,
|
| 249 | + size -> |
| 250 | + size == -1 |
| 251 | + ? new ImmutableMap.Builder<K, V>() |
| 252 | + : new ImmutableMap.Builder<K, V>(Ints.checkedCast(size)), |
196 | 253 | (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)),
|
197 | 254 | ImmutableMap.Builder::combine,
|
198 | 255 | ImmutableMap.Builder::buildOrThrow);
|
@@ -249,8 +306,12 @@ ImmutableSet<E> toImmutableSet() {
|
249 | 306 | Function<? super T, ? extends V> valueFunction) {
|
250 | 307 | checkNotNull(keyFunction);
|
251 | 308 | checkNotNull(valueFunction);
|
252 |
| - return Collector.of( |
| 309 | + return sizedCollector( |
253 | 310 | ImmutableBiMap.Builder<K, V>::new,
|
| 311 | + size -> |
| 312 | + size == -1 |
| 313 | + ? new ImmutableBiMap.Builder<K, V>() |
| 314 | + : new ImmutableBiMap.Builder<K, V>(Ints.checkedCast(size)), |
254 | 315 | (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)),
|
255 | 316 | ImmutableBiMap.Builder::combine,
|
256 | 317 | ImmutableBiMap.Builder::buildOrThrow,
|
|
0 commit comments