|
14 | 14 | *****************************************************************************/ |
15 | 15 | package com.google.mu.util.stream; |
16 | 16 |
|
| 17 | +import static com.google.mu.util.stream.MoreStreams.mapping; |
17 | 18 | import static java.util.Objects.requireNonNull; |
18 | 19 | import static java.util.function.Function.identity; |
19 | 20 |
|
|
35 | 36 | import com.google.common.collect.Multimap; |
36 | 37 | import com.google.common.collect.Multimaps; |
37 | 38 | import com.google.common.collect.Tables; |
| 39 | +import com.google.mu.util.Both; |
38 | 40 |
|
39 | 41 | /** |
40 | 42 | * Guava-specific Collectors and BiCollectors. |
@@ -232,7 +234,7 @@ public static <K, V> BiCollector<K, V, ImmutableMultiset<K>> toImmutableMultiset |
232 | 234 | * Returns a {@link BiCollector} that collects the key-value pairs into an {@link ImmutableBimap}. |
233 | 235 | * Equivalent to {@code ImmutableBimap::toImmutableBimap}. |
234 | 236 | */ |
235 | | - public static <K, V> BiCollector<K, V, ImmutableBiMap<K, V>> toImmutableBimap() { |
| 237 | + public static <K, V> BiCollector<K, V, ImmutableBiMap<K, V>> toImmutableBiMap() { |
236 | 238 | return ImmutableBiMap::toImmutableBiMap; |
237 | 239 | } |
238 | 240 |
|
@@ -283,6 +285,49 @@ public static <K, V> BiCollector<K, V, ImmutableBiMap<K, V>> toImmutableBimap() |
283 | 285 | (builder, that) -> builder.putAll(that.build()), |
284 | 286 | ImmutableTable.Builder::build)); |
285 | 287 | } |
| 288 | + /** |
| 289 | + * Returns a collector that first maps each input into a key-value pair, and then collects them |
| 290 | + * into a {@link ImmutableMap}. |
| 291 | + * |
| 292 | + * @since 5.1 |
| 293 | + */ |
| 294 | + public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap( |
| 295 | + Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) { |
| 296 | + return mapping(mapper, toImmutableMap()); |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Returns a collector that first maps each input into a key-value pair, and then collects them |
| 301 | + * into a {@link ImmutableListMultimap}. |
| 302 | + * |
| 303 | + * @since 5.1 |
| 304 | + */ |
| 305 | + public static <T, K, V> Collector<T, ?, ImmutableListMultimap<K, V>> toImmutableListMultimap( |
| 306 | + Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) { |
| 307 | + return mapping(mapper, toImmutableListMultimap()); |
| 308 | + } |
| 309 | + |
| 310 | + /** |
| 311 | + * Returns a collector that first maps each input into a key-value pair, and then collects them |
| 312 | + * into a {@link ImmutableSetMultimap}. |
| 313 | + * |
| 314 | + * @since 5.1 |
| 315 | + */ |
| 316 | + public static <T, K, V> Collector<T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimap( |
| 317 | + Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) { |
| 318 | + return mapping(mapper, toImmutableSetMultimap()); |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * Returns a collector that first maps each input into a key-value pair, and then collects them |
| 323 | + * into a {@link ImmutableBiMap}. |
| 324 | + * |
| 325 | + * @since 5.1 |
| 326 | + */ |
| 327 | + public static <T, K, V> Collector<T, ?, ImmutableBiMap<K, V>> toImmutableBiMap( |
| 328 | + Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) { |
| 329 | + return mapping(mapper, toImmutableBiMap()); |
| 330 | + } |
286 | 331 |
|
287 | 332 | private static <K, V, T, R> BiCollector<K, V, R> mappingValues( |
288 | 333 | Function<? super V, ? extends T> mapper, BiCollector<K, T, R> downstream) { |
|
0 commit comments