Skip to content

Commit

Permalink
Use BiCollectors.mapping() with Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fluentfuture committed Oct 13, 2024
1 parent c3dbd2b commit 7ccc529
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.mu.util.stream.BiCollectors.mapping;
import static com.google.mu.util.stream.BiStream.groupingBy;
import static com.google.mu.util.stream.MoreCollectors.mapping;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -216,7 +217,7 @@ public static <K, V> BiCollector<K, V, ImmutableListMultimap<K, V>> toImmutableL
public static <K, T, V>
BiCollector<K, T, ImmutableListMultimap<K, V>> flatteningToImmutableListMultimap(
Function<? super T, ? extends Stream<? extends V>> flattener) {
return mappingValues(flattener, ImmutableListMultimap::flatteningToImmutableListMultimap);
return mapping(identity(), flattener, ImmutableListMultimap::flatteningToImmutableListMultimap);
}

/**
Expand All @@ -243,7 +244,7 @@ public static <K, V> BiCollector<K, V, ImmutableSetMultimap<K, V>> toImmutableSe
public static <K, T, V>
BiCollector<K, T, ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap(
Function<? super T, ? extends Stream<? extends V>> flattener) {
return mappingValues(flattener, ImmutableSetMultimap::flatteningToImmutableSetMultimap);
return mapping(identity(), flattener, ImmutableSetMultimap::flatteningToImmutableSetMultimap);
}

/**
Expand Down Expand Up @@ -622,7 +623,7 @@ V getValue() {
*/
public static <K extends Comparable<K>, V> BiCollector<Range<K>, V, BiStream<Range<K>, Chain<V>>>
toDisjointRanges() {
return mappingValues(Chain::of, toDisjointRanges(Chain::concat));
return mapping(identity(), Chain::of, toDisjointRanges(Chain::concat));
}

/**
Expand Down Expand Up @@ -686,11 +687,5 @@ V getValue() {
toDisjointRanges(), m -> m.mapValues(chain -> chain.stream().collect(valueCollector)));
}

private static <K, V, T, R> BiCollector<K, V, R> mappingValues(
Function<? super V, ? extends T> mapper, BiCollector<K, T, R> downstream) {
requireNonNull(mapper);
return BiCollectors.mapping((k, v) -> k, (k, v) -> mapper.apply(v), downstream);
}

private GuavaCollectors() {}
}

0 comments on commit 7ccc529

Please sign in to comment.