Skip to content

Commit

Permalink
Make toBiStream() eagerly call the functions. It's what other collect…
Browse files Browse the repository at this point in the history
…ors like toMap() do.
  • Loading branch information
fluentfuture committed Oct 20, 2024
1 parent 7ccc529 commit 7cb0b6c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 6 additions & 5 deletions mug/src/main/java/com/google/mu/util/stream/BiStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*****************************************************************************/
package com.google.mu.util.stream;

import static com.google.mu.util.stream.MoreStreams.collectingAndThen;
import static com.google.mu.util.stream.MoreStreams.toStream;
import static java.util.Map.Entry.comparingByKey;
import static java.util.Map.Entry.comparingByValue;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -333,7 +333,7 @@ public static <K, V> Builder<K, V> builder() {
*/
public static <T, K, V> Collector<T, ?, BiStream<K, V>> concatenating(
Function<? super T, ? extends BiStream<? extends K, ? extends V>> toBiStream) {
return collectingAndThen(stream -> concat(stream.map(toBiStream)));
return collectingAndThen(Collectors.mapping(toBiStream, toStream()), BiStream::concat);
}

/**
Expand Down Expand Up @@ -394,7 +394,9 @@ public static <K, V> Builder<K, V> builder() {
Function<? super E, ? extends K> toKey, Function<? super E, ? extends V> toValue) {
requireNonNull(toKey);
requireNonNull(toValue);
return collectingAndThen(stream -> from(stream, toKey, toValue));
return collectingAndThen(
Collectors.mapping(e -> kv(toKey.apply(e), toValue.apply(e)), toStream()),
BiStream::fromEntries);
}

/**
Expand All @@ -409,8 +411,7 @@ public static <K, V> Builder<K, V> builder() {
*/
public static <E, K, V> Collector<E, ?, BiStream<K, V>> toBiStream(
Function<? super E, ? extends Both<? extends K, ? extends V>> toPair) {
requireNonNull(toPair);;
return collectingAndThen(stream -> from(stream.map(toPair)));
return collectingAndThen(Collectors.mapping(toPair, toStream()), BiStream::from);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ static <F, T> Stream<T> mapBySpliterator(
}

/** Copying input elements into another stream. */
private static <T> Collector<T, ?, Stream<T>> toStream() {
static <T> Collector<T, ?, Stream<T>> toStream() {
return Collector.of(
Stream::<T>builder,
Stream.Builder::add,
Expand Down
13 changes: 12 additions & 1 deletion mug/src/test/java/com/google/mu/util/stream/BiStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import static com.google.mu.util.Optionals.optional;
import static com.google.mu.util.stream.BiCollectors.toMap;
import static com.google.mu.util.stream.BiStream.biStream;
import static com.google.mu.util.stream.BiStream.crossJoining;
import static com.google.mu.util.stream.BiStream.concatenating;
import static com.google.mu.util.stream.BiStream.crossJoining;
import static com.google.mu.util.stream.BiStream.groupingByEach;
import static com.google.mu.util.stream.BiStream.toAdjacentPairs;
import static com.google.mu.util.stream.BiStream.toBiStream;
import static com.google.mu.util.stream.MoreStreams.indexesFrom;
import static java.util.Arrays.asList;
import static java.util.function.Function.identity;
Expand Down Expand Up @@ -66,6 +67,7 @@
import com.google.common.truth.IterableSubject;
import com.google.common.truth.MultimapSubject;
import com.google.mu.util.BiOptional;
import com.google.mu.util.Both;
import com.google.mu.util.Substring;

@RunWith(JUnit4.class)
Expand Down Expand Up @@ -744,6 +746,15 @@ public class BiStreamTest {
.inOrder();
}

@Test public void testToBiStream_exceptionThrownEagerly() {
assertThrows(
NumberFormatException.class,
() -> Stream.of("1", "b").collect(toBiStream(identity(), v -> Integer.parseInt(v))));
assertThrows(
NumberFormatException.class,
() -> Stream.of("1", "b").collect(toBiStream(v -> Both.of(v, Integer.parseInt(v)))));
}

@Test public void testGroupingBy() {
Map<Integer, List<Integer>> groups =
Stream.of(0, 1, 2).collect(BiStream.groupingBy(n -> n / 2)).toMap();
Expand Down

0 comments on commit 7cb0b6c

Please sign in to comment.