|
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;
|
32 | 35 | import java.util.TreeMap;
|
| 36 | +import java.util.function.BiConsumer; |
33 | 37 | import java.util.function.BinaryOperator;
|
34 | 38 | import java.util.function.Function;
|
| 39 | +import java.util.function.LongFunction; |
35 | 40 | import java.util.function.Supplier;
|
36 | 41 | import java.util.function.ToIntFunction;
|
37 | 42 | import java.util.stream.Collector;
|
| 43 | +import java.util.stream.Collector.Characteristics; |
38 | 44 | import java.util.stream.Stream;
|
39 | 45 | import org.jspecify.annotations.Nullable;
|
40 | 46 |
|
|
43 | 49 | @SuppressWarnings("Java7ApiChecker")
|
44 | 50 | @IgnoreJRERequirement // used only from APIs with Java 8 types in them
|
45 | 51 | final class CollectCollectors {
|
| 52 | + private static < |
| 53 | + T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object> |
| 54 | + Collector<T, A, R> sizedCollector( |
| 55 | + Supplier<A> supplier, |
| 56 | + LongFunction<A> sizedSupplier, |
| 57 | + BiConsumer<A, T> accumulator, |
| 58 | + BinaryOperator<A> combiner, |
| 59 | + Function<A, R> finisher, |
| 60 | + Characteristics... characteristics) { |
| 61 | + ImmutableSet<Characteristics> characteristicsSet = immutableEnumSet(asList(characteristics)); |
| 62 | + return new Collector<T, A, R>() { |
| 63 | + @Override |
| 64 | + public Supplier<A> supplier() { |
| 65 | + return supplier; |
| 66 | + } |
| 67 | + |
| 68 | + // only an override under some future version of Java? |
| 69 | + @SuppressWarnings({ |
| 70 | + "MissingOverride", |
| 71 | + "UnusedMethod", |
| 72 | + }) |
| 73 | + public LongFunction<A> sizedSupplier() { |
| 74 | + return sizedSupplier; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public BiConsumer<A, T> accumulator() { |
| 79 | + return accumulator; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public BinaryOperator<A> combiner() { |
| 84 | + return combiner; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Function<A, R> finisher() { |
| 89 | + return finisher; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public ImmutableSet<Characteristics> characteristics() { |
| 94 | + return characteristicsSet; |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
46 | 98 |
|
47 | 99 | private static final Collector<Object, ?, ImmutableList<Object>> TO_IMMUTABLE_LIST =
|
48 |
| - Collector.of( |
| 100 | + sizedCollector( |
49 | 101 | ImmutableList::builder,
|
| 102 | + size -> |
| 103 | + size == -1 |
| 104 | + ? ImmutableList.builder() |
| 105 | + : ImmutableList.builderWithExpectedSize(Ints.checkedCast(size)), |
50 | 106 | ImmutableList.Builder::add,
|
51 | 107 | ImmutableList.Builder::combine,
|
52 | 108 | ImmutableList.Builder::build);
|
@@ -191,8 +247,12 @@ ImmutableSet<E> toImmutableSet() {
|
191 | 247 | Function<? super T, ? extends V> valueFunction) {
|
192 | 248 | checkNotNull(keyFunction);
|
193 | 249 | checkNotNull(valueFunction);
|
194 |
| - return Collector.of( |
| 250 | + return sizedCollector( |
195 | 251 | ImmutableMap.Builder<K, V>::new,
|
| 252 | + size -> |
| 253 | + size == -1 |
| 254 | + ? new ImmutableMap.Builder<K, V>() |
| 255 | + : new ImmutableMap.Builder<K, V>(Ints.checkedCast(size)), |
196 | 256 | (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)),
|
197 | 257 | ImmutableMap.Builder::combine,
|
198 | 258 | ImmutableMap.Builder::buildOrThrow);
|
@@ -249,8 +309,12 @@ ImmutableSet<E> toImmutableSet() {
|
249 | 309 | Function<? super T, ? extends V> valueFunction) {
|
250 | 310 | checkNotNull(keyFunction);
|
251 | 311 | checkNotNull(valueFunction);
|
252 |
| - return Collector.of( |
| 312 | + return sizedCollector( |
253 | 313 | ImmutableBiMap.Builder<K, V>::new,
|
| 314 | + size -> |
| 315 | + size == -1 |
| 316 | + ? new ImmutableBiMap.Builder<K, V>() |
| 317 | + : new ImmutableBiMap.Builder<K, V>(Ints.checkedCast(size)), |
254 | 318 | (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)),
|
255 | 319 | ImmutableBiMap.Builder::combine,
|
256 | 320 | ImmutableBiMap.Builder::buildOrThrow,
|
|
0 commit comments