Skip to content

Commit a1f944b

Browse files
committed
Both<> collectors
1 parent 4acfe31 commit a1f944b

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

mug-guava/src/main/java/com/google/mu/util/stream/GuavaCollectors.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*****************************************************************************/
1515
package com.google.mu.util.stream;
1616

17+
import static com.google.mu.util.stream.MoreStreams.mapping;
1718
import static java.util.Objects.requireNonNull;
1819
import static java.util.function.Function.identity;
1920

@@ -35,6 +36,7 @@
3536
import com.google.common.collect.Multimap;
3637
import com.google.common.collect.Multimaps;
3738
import com.google.common.collect.Tables;
39+
import com.google.mu.util.Both;
3840

3941
/**
4042
* Guava-specific Collectors and BiCollectors.
@@ -232,7 +234,7 @@ public static <K, V> BiCollector<K, V, ImmutableMultiset<K>> toImmutableMultiset
232234
* Returns a {@link BiCollector} that collects the key-value pairs into an {@link ImmutableBimap}.
233235
* Equivalent to {@code ImmutableBimap::toImmutableBimap}.
234236
*/
235-
public static <K, V> BiCollector<K, V, ImmutableBiMap<K, V>> toImmutableBimap() {
237+
public static <K, V> BiCollector<K, V, ImmutableBiMap<K, V>> toImmutableBiMap() {
236238
return ImmutableBiMap::toImmutableBiMap;
237239
}
238240

@@ -283,6 +285,49 @@ public static <K, V> BiCollector<K, V, ImmutableBiMap<K, V>> toImmutableBimap()
283285
(builder, that) -> builder.putAll(that.build()),
284286
ImmutableTable.Builder::build));
285287
}
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+
}
286331

287332
private static <K, V, T, R> BiCollector<K, V, R> mappingValues(
288333
Function<? super V, ? extends T> mapper, BiCollector<K, T, R> downstream) {

mug-guava/src/test/java/com/google/mu/util/stream/GuavaCollectorsTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
package com.google.mu.util.stream;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.mu.util.Substring.first;
1819
import static com.google.mu.util.stream.BiCollectors.groupingBy;
1920
import static com.google.mu.util.stream.GuavaCollectors.flatteningToImmutableListMultimap;
2021
import static com.google.mu.util.stream.GuavaCollectors.flatteningToImmutableSetMultimap;
2122
import static com.google.mu.util.stream.GuavaCollectors.indexingBy;
23+
import static com.google.mu.util.stream.GuavaCollectors.toImmutableBiMap;
2224
import static com.google.mu.util.stream.GuavaCollectors.toImmutableListMultimap;
2325
import static com.google.mu.util.stream.GuavaCollectors.toImmutableMap;
2426
import static com.google.mu.util.stream.GuavaCollectors.toImmutableMultiset;
@@ -38,6 +40,7 @@
3840
import org.junit.runner.RunWith;
3941
import org.junit.runners.JUnit4;
4042

43+
import com.google.common.collect.ImmutableBiMap;
4144
import com.google.common.collect.ImmutableList;
4245
import com.google.common.collect.ImmutableListMultimap;
4346
import com.google.common.collect.ImmutableMap;
@@ -191,6 +194,40 @@ public class GuavaCollectorsTest {
191194
assertThat(table.rowMap()).containsExactly("r1", ImmutableMap.of("c1", 11, "c2", 12)).inOrder();
192195
}
193196

197+
@Test public void testToImmutableMap_fromPairs() {
198+
String input = "k1=v1,k2=v2";
199+
ImmutableMap<String, String> kvs =
200+
first(',').delimit(input).collect(toImmutableMap(s -> first('=').split(s).orElseThrow()));
201+
assertThat(kvs).containsExactly("k1", "v1", "k2", "v2").inOrder();
202+
}
203+
204+
@Test public void testToImmutableListMultimap_fromPairs() {
205+
String input = "k1=v1,k2=v2,k2=v2";
206+
ImmutableListMultimap<String, String> kvs =
207+
first(',')
208+
.delimit(input)
209+
.collect(toImmutableListMultimap(s -> first('=').split(s).orElseThrow()));
210+
assertThat(kvs).containsExactly("k1", "v1", "k2", "v2", "k2", "v2").inOrder();
211+
}
212+
213+
@Test public void testToImmutableSetMultimap_fromPairs() {
214+
String input = "k1=v1,k2=v2,k2=v3";
215+
ImmutableSetMultimap<String, String> kvs =
216+
first(',')
217+
.delimit(input)
218+
.collect(toImmutableSetMultimap(s -> first('=').split(s).orElseThrow()));
219+
assertThat(kvs).containsExactly("k1", "v1", "k2", "v2", "k2", "v3").inOrder();
220+
}
221+
222+
@Test public void testToImmutableBiMap_fromPairs() {
223+
String input = "k1=v1,k2=v2";
224+
ImmutableBiMap<String, String> kvs =
225+
first(',')
226+
.delimit(input)
227+
.collect(toImmutableBiMap(s -> first('=').split(s).orElseThrow()));
228+
assertThat(kvs).containsExactly("k1", "v1", "k2", "v2").inOrder();
229+
}
230+
194231
@Test public void testIndexingBy() {
195232
assertThat(Stream.of(1, 2).collect(indexingBy(Object::toString)))
196233
.containsExactly("1", 1, "2", 2)

0 commit comments

Comments
 (0)