diff --git a/mug/src/main/java/com/google/mu/util/stream/BiStream.java b/mug/src/main/java/com/google/mu/util/stream/BiStream.java index 4a86867429..13b7b2da3e 100644 --- a/mug/src/main/java/com/google/mu/util/stream/BiStream.java +++ b/mug/src/main/java/com/google/mu/util/stream/BiStream.java @@ -144,7 +144,7 @@ public static Builder builder() { * @since 3.3 */ public static Collector> groupingBy( - Function classifier, BinaryOperator reducer) { + Function classifier, BinaryOperator reducer) { return groupingBy(classifier, reducingGroupMembers(reducer)); } @@ -163,9 +163,9 @@ public static Builder builder() { * @since 3.3 */ public static Collector> groupingBy( - Function classifier, - Function mapper, - BinaryOperator reducer) { + Function classifier, + Function mapper, + BinaryOperator reducer) { return groupingBy(classifier, Collectors.mapping(mapper, reducingGroupMembers(reducer))); } @@ -191,7 +191,7 @@ public static Builder builder() { * @since 3.0 */ public static Collector>> groupingBy( - Function classifier) { + Function classifier) { return groupingBy(classifier, toList()); } @@ -220,10 +220,10 @@ public static Builder builder() { * @since 3.0 */ public static Collector> groupingBy( - Function classifier, Collector valueCollector) { + Function classifier, Collector valueCollector) { requireNonNull(classifier); Collector> grouping = - Collectors.groupingBy(classifier, LinkedHashMap::new, valueCollector); + Collectors.groupingBy(classifier, LinkedHashMap::new, valueCollector); return collectingAndThen(grouping, BiStream::from); } @@ -245,12 +245,12 @@ public static Builder builder() { * @since 6.5 */ public static Collector> groupingByEach( - Function> keysFunction, - Collector groupCollector) { + Function> keysFunction, + Collector groupCollector) { requireNonNull(keysFunction); return Java9Collectors.flatMapping( - (T e) -> keysFunction.apply(e).map(k -> kv(k, e)), - groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, groupCollector))); + (T e) -> keysFunction.apply(e).map(k -> kv(k, e)), + groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, groupCollector))); } /** @@ -275,18 +275,18 @@ public static Builder builder() { * @since 6.5 */ public static Collector> groupingByEach( - Function> keysFunction, - Function valueFunction, - Collector groupCollector) { + Function> keysFunction, + Function valueFunction, + Collector groupCollector) { requireNonNull(keysFunction); requireNonNull(valueFunction); requireNonNull(groupCollector); return Java9Collectors.flatMapping( - (T e) -> { - V v = valueFunction.apply(e); - return keysFunction.apply(e).map(k -> kv(k, v)); - }, - groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, groupCollector))); + (T e) -> { + V v = valueFunction.apply(e); + return keysFunction.apply(e).map(k -> kv(k, v)); + }, + groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, groupCollector))); } /** @@ -310,9 +310,9 @@ public static Builder builder() { * @since 6.5 */ public static Collector> groupingByEach( - Function> keysFunction, - Function valueFunction, - BinaryOperator groupReducer) { + Function> keysFunction, + Function valueFunction, + BinaryOperator groupReducer) { return groupingByEach(keysFunction, valueFunction, reducingGroupMembers(groupReducer)); } @@ -332,7 +332,7 @@ public static Builder builder() { * @since 3.0 */ public static Collector> concatenating( - Function> toBiStream) { + Function> toBiStream) { return collectingAndThen(stream -> concat(stream.map(toBiStream))); } @@ -360,10 +360,10 @@ public static Builder builder() { public static Collector> crossJoining(Stream right) { requireNonNull(right); return collectingAndThen( - toList(), - (List left) -> - // If `right` is infinite, even limit(1) will result in infinite loop otherwise. - left.isEmpty() ? empty() : concat(right.map(r -> from(left, identity(), l -> r)))); + toList(), + (List left) -> + // If `right` is infinite, even limit(1) will result in infinite loop otherwise. + left.isEmpty() ? empty() : concat(right.map(r -> from(left, identity(), l -> r)))); } /** @@ -391,7 +391,7 @@ public static Builder builder() { * @since 3.2 */ public static Collector> toBiStream( - Function toKey, Function toValue) { + Function toKey, Function toValue) { requireNonNull(toKey); requireNonNull(toValue); return collectingAndThen(stream -> from(stream, toKey, toValue)); @@ -408,7 +408,7 @@ public static Builder builder() { * @since 5.1 */ public static Collector> toBiStream( - Function> toPair) { + Function> toPair) { requireNonNull(toPair);; return collectingAndThen(stream -> from(stream.map(toPair))); } @@ -439,18 +439,18 @@ public static BiStream of(K key, V value) { /** Returns a {@code BiStream} of two pairs, containing the supplied keys and values. */ public static BiStream of( - K key1, V value1, K key2, V value2) { + K key1, V value1, K key2, V value2) { return fromEntries(Stream.of(kv(key1, value1), kv(key2, value2))); } /** Returns a {@code BiStream} of three pairs, containing the supplied keys and values. */ public static BiStream of( - K key1, - V value1, - K key2, - V value2, - K key3, - V value3) { + K key1, + V value1, + K key2, + V value2, + K key3, + V value3) { return fromEntries(Stream.of(kv(key1, value1), kv(key2, value2), kv(key3, value3))); } @@ -460,14 +460,14 @@ public static BiStream of( * @since 5.6 */ public static BiStream of( - K k1, - V v1, - K k2, - V v2, - K k3, - V v3, - K k4, - V v4) { + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4) { return fromEntries(Stream.of(kv(k1, v1), kv(k2, v2), kv(k3, v3), kv(k4, v4))); } @@ -477,16 +477,16 @@ public static BiStream of( * @since 5.6 */ public static BiStream of( - K k1, - V v1, - K k2, - V v2, - K k3, - V v3, - K k4, - V v4, - K k5, - V v5) { + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5) { return fromEntries(Stream.of(kv(k1, v1), kv(k2, v2), kv(k3, v3), kv(k4, v4), kv(k5, v5))); } @@ -496,20 +496,20 @@ public static BiStream of( * @since 5.6 */ public static BiStream of( - K k1, - V v1, - K k2, - V v2, - K k3, - V v3, - K k4, - V v4, - K k5, - V v5, - K k6, - V v6) { + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6) { return fromEntries( - Stream.of(kv(k1, v1), kv(k2, v2), kv(k3, v3), kv(k4, v4), kv(k5, v5), kv(k6, v6))); + Stream.of(kv(k1, v1), kv(k2, v2), kv(k3, v3), kv(k4, v4), kv(k5, v5), kv(k6, v6))); } /** @@ -518,23 +518,23 @@ public static BiStream of( * @since 5.6 */ public static BiStream of( - K k1, - V v1, - K k2, - V v2, - K k3, - V v3, - K k4, - V v4, - K k5, - V v5, - K k6, - V v6, - K k7, - V v7) { + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7) { return fromEntries( - Stream.of( - kv(k1, v1), kv(k2, v2), kv(k3, v3), kv(k4, v4), kv(k5, v5), kv(k6, v6), kv(k7, v7))); + Stream.of( + kv(k1, v1), kv(k2, v2), kv(k3, v3), kv(k4, v4), kv(k5, v5), kv(k6, v6), kv(k7, v7))); } /** @@ -543,32 +543,32 @@ public static BiStream of( * @since 5.6 */ public static BiStream of( - K k1, - V v1, - K k2, - V v2, - K k3, - V v3, - K k4, - V v4, - K k5, - V v5, - K k6, - V v6, - K k7, - V v7, - K k8, - V v8) { + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8) { return fromEntries( - Stream.of( - kv(k1, v1), - kv(k2, v2), - kv(k3, v3), - kv(k4, v4), - kv(k5, v5), - kv(k6, v6), - kv(k7, v7), - kv(k8, v8))); + Stream.of( + kv(k1, v1), + kv(k2, v2), + kv(k3, v3), + kv(k4, v4), + kv(k5, v5), + kv(k6, v6), + kv(k7, v7), + kv(k8, v8))); } /** @@ -577,35 +577,35 @@ public static BiStream of( * @since 5.6 */ public static BiStream of( - K k1, - V v1, - K k2, - V v2, - K k3, - V v3, - K k4, - V v4, - K k5, - V v5, - K k6, - V v6, - K k7, - V v7, - K k8, - V v8, - K k9, - V v9) { + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9) { return fromEntries( - Stream.of( - kv(k1, v1), - kv(k2, v2), - kv(k3, v3), - kv(k4, v4), - kv(k5, v5), - kv(k6, v6), - kv(k7, v7), - kv(k8, v8), - kv(k9, v9))); + Stream.of( + kv(k1, v1), + kv(k2, v2), + kv(k3, v3), + kv(k4, v4), + kv(k5, v5), + kv(k6, v6), + kv(k7, v7), + kv(k8, v8), + kv(k9, v9))); } /** @@ -614,38 +614,38 @@ public static BiStream of( * @since 5.6 */ public static BiStream of( - K k1, - V v1, - K k2, - V v2, - K k3, - V v3, - K k4, - V v4, - K k5, - V v5, - K k6, - V v6, - K k7, - V v7, - K k8, - V v8, - K k9, - V v9, - K k10, - V v10) { + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9, + K k10, + V v10) { return fromEntries( - Stream.of( - kv(k1, v1), - kv(k2, v2), - kv(k3, v3), - kv(k4, v4), - kv(k5, v5), - kv(k6, v6), - kv(k7, v7), - kv(k8, v8), - kv(k9, v9), - kv(k10, v10))); + Stream.of( + kv(k1, v1), + kv(k2, v2), + kv(k3, v3), + kv(k4, v4), + kv(k5, v5), + kv(k6, v6), + kv(k7, v7), + kv(k8, v8), + kv(k9, v9), + kv(k10, v10))); } /** @@ -660,9 +660,9 @@ public static BiStream of( */ @SafeVarargs public static BiStream concat( - Map m1, - Map m2, - Map... rest) { + Map m1, + Map m2, + Map... rest) { Stream.Builder> builder = Stream.builder(); builder.add(requireNonNull(m1)).add(requireNonNull(m2)); for (Map m : rest) { @@ -683,9 +683,9 @@ public static BiStream concat( */ @SafeVarargs public static BiStream concat( - BiStream s1, - BiStream s2, - BiStream... rest) { + BiStream s1, + BiStream s2, + BiStream... rest) { Stream.Builder> builder = Stream.builder(); builder.add(requireNonNull(s1)).add(requireNonNull(s2)); for (BiStream s : rest) { @@ -700,7 +700,7 @@ public static BiStream concat( * @since 3.0 */ public static BiStream concat( - Stream> biStreams) { + Stream> biStreams) { return fromEntries(biStreams.flatMap(BiStream::mapToEntry)); } @@ -785,7 +785,7 @@ public static BiStream biStream(Stream elements) { * @since 5.6 */ public static BiStream biStream( - Collection keys, Function toValue) { + Collection keys, Function toValue) { return new GenericEntryStream<>(keys.stream(), identity(), toValue); } @@ -801,7 +801,7 @@ public static BiStream biStream( * @since 5.6 */ public static BiStream biStream( - Stream keys, Function toValue) { + Stream keys, Function toValue) { return new GenericEntryStream<>(keys, identity(), toValue); } @@ -819,7 +819,7 @@ public static BiStream biStream( * @since 5.6 */ public static BiStream biStream( - Function toKey, Collection values) { + Function toKey, Collection values) { return new GenericEntryStream<>(values.stream(), toKey, identity()); } @@ -837,7 +837,7 @@ public static BiStream biStream( * @since 5.6 */ public static BiStream biStream( - Function toKey, Stream values) { + Function toKey, Stream values) { return new GenericEntryStream<>(values, toKey, identity()); } @@ -866,9 +866,9 @@ public static BiStream from(Collection BiStream from( - Collection elements, - Function toKey, - Function toValue) { + Collection elements, + Function toKey, + Function toValue) { return from(elements.stream(), toKey, toValue); } @@ -882,9 +882,9 @@ public static BiStream from( */ @Deprecated public static BiStream from( - Stream stream, - Function toKey, - Function toValue) { + Stream stream, + Function toKey, + Function toValue) { return new GenericEntryStream<>(stream, toKey, toValue); } @@ -894,7 +894,7 @@ public static BiStream from( * @since 5.1 */ public static BiStream from( - Stream> pairs) { + Stream> pairs) { return from(pairs, BiStream::left, BiStream::right); } @@ -946,25 +946,25 @@ public static BiStream from( * @since 5.5 */ public static BiStream repeat( - Function work, - I initial, - BiFunction> increment) { + Function work, + I initial, + BiFunction> increment) { requireNonNull(work); requireNonNull(increment); return fromEntries( - MoreStreams.whileNotNull( - new Supplier>() { - I nextInput = requireNonNull(initial); - @Override public Map.Entry get() { - I in = nextInput; - if (in == null) { - return null; - } - O out = work.apply(in); - nextInput = increment.apply(in, out).orElse(null); - return kv(in, out); - } - })); + MoreStreams.whileNotNull( + new Supplier>() { + I nextInput = requireNonNull(initial); + @Override public Map.Entry get() { + I in = nextInput; + if (in == null) { + return null; + } + O out = work.apply(in); + nextInput = increment.apply(in, out).orElse(null); + return kv(in, out); + } + })); } /** @@ -987,21 +987,21 @@ public interface Partitioner { /** @since 7.1 */ public static > BiStream fromEntries( - Stream entryStream) { + Stream entryStream) { return new GenericEntryStream(entryStream, Map.Entry::getKey, Map.Entry::getValue) { @Override public BiStream map( - BiFunction keyMapper, - BiFunction valueMapper) { + BiFunction keyMapper, + BiFunction valueMapper) { return from(entryStream, forEntry(keyMapper), forEntry(valueMapper)); } @Override public BiStream mapKeys( - BiFunction keyMapper) { + BiFunction keyMapper) { return from(entryStream, forEntry(keyMapper), Map.Entry::getValue); } @Override public BiStream mapValues( - BiFunction valueMapper) { + BiFunction valueMapper) { return from(entryStream, Map.Entry::getKey, forEntry(valueMapper)); } @@ -1036,7 +1036,7 @@ public interface Partitioner { * @since 4.7 */ public final Stream mapToObjIfPresent( - BiFunction> mapper) { + BiFunction> mapper) { return mapToObj(mapper).map(BiStream::orElseNull).filter(Objects::nonNull); } @@ -1045,8 +1045,8 @@ public final Stream mapToObjIfPresent( * valueMapper} to the pairs in this {@code BiStream}. */ public BiStream map( - BiFunction keyMapper, - BiFunction valueMapper) { + BiFunction keyMapper, + BiFunction valueMapper) { requireNonNull(keyMapper); requireNonNull(valueMapper); return fromEntries(mapToObj((k, v) -> kv(keyMapper.apply(k, v), valueMapper.apply(k, v)))); @@ -1073,7 +1073,7 @@ public BiStream map( * @since 5.2 */ public final BiStream map( - BiFunction> mapper) { + BiFunction> mapper) { return from(mapToObj(mapper)); } @@ -1084,11 +1084,11 @@ public final BiStream map( * @since 5.0 */ public final BiStream mapIfPresent( - BiFunction> mapper) { + BiFunction> mapper) { return fromEntries( - mapToObj(mapper) - .filter(BiOptional::isPresent) - .map(optional -> optional.map(BiStream::kv).get())); + mapToObj(mapper) + .filter(BiOptional::isPresent) + .map(optional -> optional.map(BiStream::kv).get())); } /** @@ -1122,7 +1122,7 @@ public BiStream mapKeys(BiFunction BiStream mapValues( - BiFunction valueMapper) { + BiFunction valueMapper) { return map((k, v) -> k, valueMapper); } @@ -1135,7 +1135,7 @@ public BiStream mapValues( *

If a mapped stream is null, an empty stream is used instead. */ public final Stream flatMapToObj( - BiFunction> mapper) { + BiFunction> mapper) { return mapToObj(mapper).flatMap(identity()); } @@ -1145,7 +1145,7 @@ public final Stream flatMapToObj( *

If a mapped stream is null, an empty stream is used instead. */ public final DoubleStream flatMapToDouble( - BiFunction mapper) { + BiFunction mapper) { return mapToObj(mapper).flatMapToDouble(identity()); } @@ -1155,7 +1155,7 @@ public final DoubleStream flatMapToDouble( *

If a mapped stream is null, an empty stream is used instead. */ public final IntStream flatMapToInt( - BiFunction mapper) { + BiFunction mapper) { return mapToObj(mapper).flatMapToInt(identity()); } @@ -1165,7 +1165,7 @@ public final IntStream flatMapToInt( *

If a mapped stream is null, an empty stream is used instead. */ public final LongStream flatMapToLong( - BiFunction mapper) { + BiFunction mapper) { return mapToObj(mapper).flatMapToLong(identity()); } @@ -1180,7 +1180,7 @@ public final LongStream flatMapToLong( *

If a mapped stream is null, an empty stream is used instead. */ public final BiStream flatMap( - BiFunction> mapper) { + BiFunction> mapper) { return fromEntries(mapToObj(mapper).filter(Objects::nonNull).flatMap(BiStream::mapToEntry)); } @@ -1190,11 +1190,11 @@ public final BiStream flatMap( *

If a mapped stream is null, an empty stream is used instead. */ public final BiStream flatMapKeys( - BiFunction> keyMapper) { + BiFunction> keyMapper) { requireNonNull(keyMapper); return fromEntries( - this.>flatMapToObj( // j2cl compiler needs help with type inference - (k, v) -> nullToEmpty(keyMapper.apply(k, v)).map(k2 -> kv(k2, v)))); + this.>flatMapToObj( // j2cl compiler needs help with type inference + (k, v) -> nullToEmpty(keyMapper.apply(k, v)).map(k2 -> kv(k2, v)))); } /** @@ -1203,7 +1203,7 @@ public final BiStream flatMapKeys( *

If a mapped stream is null, an empty stream is used instead. */ public final BiStream flatMapKeys( - Function> keyMapper) { + Function> keyMapper) { requireNonNull(keyMapper); return flatMapKeys((k, v) -> keyMapper.apply(k)); } @@ -1272,7 +1272,7 @@ public final BiStream mapKeysIfPresent(Map * @since 4.7 */ public final BiStream mapKeysIfPresent( - Function> keyMapper) { + Function> keyMapper) { return mapKeys(keyMapper).mapKeys(BiStream::orElseNull).filterKeys(Objects::nonNull); } @@ -1284,7 +1284,7 @@ public final BiStream mapKeysIfPresent( * @since 4.7 */ public final BiStream mapKeysIfPresent( - BiFunction> keyMapper) { + BiFunction> keyMapper) { return mapKeys(keyMapper).mapKeys(BiStream::orElseNull).filterKeys(Objects::nonNull); } @@ -1294,11 +1294,11 @@ public final BiStream mapKeysIfPresent( *

If a mapped stream is null, an empty stream is used instead. */ public final BiStream flatMapValues( - BiFunction> valueMapper) { + BiFunction> valueMapper) { requireNonNull(valueMapper); return fromEntries( - this.>flatMapToObj( // j2cl compiler needs help with type inference - (k, v) -> nullToEmpty(valueMapper.apply(k, v)).map(v2 -> kv(k, v2)))); + this.>flatMapToObj( // j2cl compiler needs help with type inference + (k, v) -> nullToEmpty(valueMapper.apply(k, v)).map(v2 -> kv(k, v2)))); } /** @@ -1307,7 +1307,7 @@ public final BiStream flatMapValues( *

If a mapped stream is null, an empty stream is used instead. */ public final BiStream flatMapValues( - Function> valueMapper) { + Function> valueMapper) { requireNonNull(valueMapper); return flatMapValues((k, v) -> valueMapper.apply(v)); } @@ -1374,7 +1374,7 @@ public final BiStream mapValuesIfPresent(Map BiStream mapValuesIfPresent( - Function> valueMapper) { + Function> valueMapper) { return mapValues(valueMapper).mapValues(BiStream::orElseNull).filterValues(Objects::nonNull); } @@ -1386,7 +1386,7 @@ public final BiStream mapValuesIfPresent( * @since 4.7 */ public final BiStream mapValuesIfPresent( - BiFunction> valueMapper) { + BiFunction> valueMapper) { return mapValues(valueMapper).mapValues(BiStream::orElseNull).filterValues(Objects::nonNull); } @@ -1403,13 +1403,24 @@ public final BiStream peek(BiConsumer action) { * Filter this stream to only pairs matching {@code predicate}. * *

Note that if you are passing in a lambda with the {@code !} operator, consider using {@link - * #skipIf} instead that might even allow you to use method reference. + * #skipIf(BiPredicate)} instead that might even allow you to use method reference. */ public final BiStream filter(BiPredicate predicate) { requireNonNull(predicate); return fromEntries(mapToEntry().filter(kv -> predicate.test(kv.getKey(), kv.getValue()))); } + /** + * Filter this stream to only entries matching {@code predicate}. + * + *

Note that if you are passing in a lambda with the {@code !} operator, consider using {@link + * #skipIf(Predicate)} instead that might even allow you to use method reference. + */ + public final BiStream filter(Predicate> predicate) { + requireNonNull(predicate); + return fromEntries(mapToEntry().filter(predicate)); + } + /** * Filter this stream to only pairs whose key matches {@code predicate}. * @@ -1450,6 +1461,24 @@ public final BiStream skipIf(BiPredicate predicate) return filter(predicate.negate()); } + /** + * Filter this stream to exclude entries matching {@code predicate}. + * + *

Useful especially when it allows you to use method reference. For example: + * + *

{@code
+   * tasks.stream()
+   *      .collect(crossJoining(Arrays.stream(MachineType.values()))
+   *      .skipIf(Worker::blacklistsMachine)
+   *      ...
+   * }
+ * + * @since 5.1 + */ + public final BiStream skipIf(Predicate> predicate) { + return filter(predicate.negate()); + } + /** * Filter this stream to exclude pairs whose key matches {@code predicate}. * @@ -1604,7 +1633,7 @@ public final BiStream sortedByValues(Comparator comparator) { * @since 6.6 */ public final BiStream sortedBy( - BiFunction sortKeyFunction, Comparator comparator) { + BiFunction sortKeyFunction, Comparator comparator) { requireNonNull(sortKeyFunction); return sorted(Comparator.comparing((Map.Entry e) -> sortKeyFunction.apply(e.getKey(), e.getValue()), comparator)); } @@ -1618,8 +1647,8 @@ public final BiStream sortedBy( */ public final BiStream sorted(Comparator byKey, Comparator byValue) { return sorted( - Comparator., K>comparing(Map.Entry::getKey, byKey) - .thenComparing(Map.Entry::getValue, byValue)); + Comparator., K>comparing(Map.Entry::getKey, byKey) + .thenComparing(Map.Entry::getValue, byValue)); } /** @@ -1698,7 +1727,7 @@ public final Map toMap() { * @since 5.6 */ public final R collect( - BiCollector collector, Function finisher) { + BiCollector collector, Function finisher) { return finisher.apply(collect(collector)); } @@ -1726,7 +1755,7 @@ public final R collect( * @since 5.0 */ public abstract A collect( - A container, BiAccumulator accumulator); + A container, BiAccumulator accumulator); /** * Closes any resources associated with this stream, typically used in a try-with-resources @@ -1768,12 +1797,12 @@ public abstract A collect( * @since 5.4 */ public final BiStream groupConsecutiveBy( - Function classifier, - Collector groupCollector) { + Function classifier, + Collector groupCollector) { return this - .mapKeys(classifier) - .groupConsecutiveByKeys(groupCollector.supplier(), groupCollector.accumulator()) - .mapValues(groupCollector.finisher()); + .mapKeys(classifier) + .groupConsecutiveByKeys(groupCollector.supplier(), groupCollector.accumulator()) + .mapValues(groupCollector.finisher()); } /** @@ -1807,7 +1836,7 @@ public final BiStream groupConsecutiveBy( * @since 5.4 */ public final BiStream groupConsecutiveBy( - Function classifier, BinaryOperator groupReducer) { + Function classifier, BinaryOperator groupReducer) { return groupConsecutiveBy(classifier, reducingGroupMembers(groupReducer)); } @@ -1853,11 +1882,11 @@ public final BiStream groupConsecutiveBy( * @since 5.5 */ public final BiStream groupConsecutiveBy( - BiFunction classifier, - BiCollector groupCollector) { + BiFunction classifier, + BiCollector groupCollector) { return this - .>map(classifier, BiStream::kv) - .groupConsecutiveByKeys(groupCollector.collectorOf(Map.Entry::getKey, Map.Entry::getValue)); + .>map(classifier, BiStream::kv) + .groupConsecutiveByKeys(groupCollector.collectorOf(Map.Entry::getKey, Map.Entry::getValue)); } /** @@ -1891,12 +1920,12 @@ public final BiStream groupConsecutiveBy( * @since 5.6 */ public final Stream groupConsecutiveIf( - Partitioner sameGroup, BiCollector groupCollector) { + Partitioner sameGroup, BiCollector groupCollector) { requireNonNull(sameGroup); return biStream(mapToEntry()) - .groupConsecutiveIf( - (p1, p2) -> sameGroup.belong(p1.getKey(), p1.getValue(), p2.getKey(), p2.getValue()), - groupCollector.collectorOf(Map.Entry::getKey, Map.Entry::getValue)); + .groupConsecutiveIf( + (p1, p2) -> sameGroup.belong(p1.getKey(), p1.getValue(), p2.getKey(), p2.getValue()), + groupCollector.collectorOf(Map.Entry::getKey, Map.Entry::getValue)); } /** @@ -1927,13 +1956,13 @@ public final Stream groupConsecutiveIf( * @since 5.4 */ public final Stream groupConsecutiveIf( - BiPredicate sameGroup, Collector groupCollector) { + BiPredicate sameGroup, Collector groupCollector) { BiConsumer groupAccumulator = groupCollector.accumulator(); return groupConsecutiveIf( - sameGroup, - groupCollector.supplier(), - (a, k, v) -> groupAccumulator.accept(a, v), - groupCollector.finisher()); + sameGroup, + groupCollector.supplier(), + (a, k, v) -> groupAccumulator.accept(a, v), + groupCollector.finisher()); } /** @@ -1955,37 +1984,37 @@ public final Stream groupConsecutiveIf( * @since 5.4 */ public final Stream groupConsecutiveIf( - BiPredicate sameGroup, BinaryOperator groupReducer) { + BiPredicate sameGroup, BinaryOperator groupReducer) { return groupConsecutiveIf(sameGroup, reducingGroupMembers(groupReducer)); } private BiStream groupConsecutiveByKeys( - Collector groupCollector) { + Collector groupCollector) { return groupConsecutiveByKeys(groupCollector.supplier(), groupCollector.accumulator()) - .mapValues(groupCollector.finisher()); + .mapValues(groupCollector.finisher()); } private BiStream groupConsecutiveByKeys( - Supplier newGroup, BiConsumer groupAccumulator) { + Supplier newGroup, BiConsumer groupAccumulator) { requireNonNull(newGroup); requireNonNull(groupAccumulator); Temp currentGroup = new Temp<>(); return fromEntries( - groupConsecutiveIf( - Objects::equals, - newGroup, - (a, k, v) -> { - currentGroup.value = k; - groupAccumulator.accept(a, v); - }, - a -> kv(currentGroup.value, a))); + groupConsecutiveIf( + Objects::equals, + newGroup, + (a, k, v) -> { + currentGroup.value = k; + groupAccumulator.accept(a, v); + }, + a -> kv(currentGroup.value, a))); } private Stream groupConsecutiveIf( - BiPredicate sameGroup, - Supplier newRun, - BiAccumulator groupAccumulator, - Function groupFinisher) { + BiPredicate sameGroup, + Supplier newRun, + BiAccumulator groupAccumulator, + Function groupFinisher) { requireNonNull(sameGroup); requireNonNull(newRun); requireNonNull(groupAccumulator); @@ -2076,7 +2105,7 @@ private static T orElseNull(Optional optional) { * instances by either invoking {@code toKey} and {@code toValue} then using the results directly, * or composing the functions to be invoked later. * - *

Doing so isn't always feasible. For example {@link #filter} and {@link #peek} both need to + *

Doing so isn't always feasible. For example {@link #filter(BiPredicate)} and {@link #peek} both need to * evaluate the entry by invoking {@code toKey} and {@code toValue} and the return values need to * be stored to avoid invoking the functions again. For these cases, the stream will be * degeneralized into {@code Stream>} so as to guarantee the "at-most-once" @@ -2088,9 +2117,9 @@ private static class GenericEntryStream extends BiStream { private final Function toValue; GenericEntryStream( - Stream underlying, - Function toKey, - Function toValue) { + Stream underlying, + Function toKey, + Function toValue) { this.underlying = requireNonNull(underlying); this.toKey = requireNonNull(toKey); this.toValue = requireNonNull(toValue); @@ -2162,8 +2191,8 @@ private static class GenericEntryStream extends BiStream { @Override public final A collect(A container, BiAccumulator accumulator) { requireNonNull(accumulator); underlying - .sequential() - .forEachOrdered(e -> accumulator.accumulate(container, toKey.apply(e), toValue.apply(e))); + .sequential() + .forEachOrdered(e -> accumulator.accumulate(container, toKey.apply(e), toValue.apply(e))); return container; } @@ -2202,29 +2231,29 @@ private static final class ZippingStream extends BiStream { @Override public Stream mapToObj(BiFunction mapper) { requireNonNull(mapper); return stream(() -> new Spliteration().ofObj(mapper), ORDERED, NOT_PARALLEL) - .onClose(left::close) - .onClose(right::close); + .onClose(left::close) + .onClose(right::close); } @Override public DoubleStream mapToDouble(ToDoubleBiFunction mapper) { requireNonNull(mapper); return doubleStream(() -> new Spliteration().ofDouble(mapper), ORDERED, NOT_PARALLEL) - .onClose(left::close) - .onClose(right::close); + .onClose(left::close) + .onClose(right::close); } @Override public IntStream mapToInt(ToIntBiFunction mapper) { requireNonNull(mapper); return intStream(() -> new Spliteration().ofInt(mapper), ORDERED, NOT_PARALLEL) - .onClose(left::close) - .onClose(right::close); + .onClose(left::close) + .onClose(right::close); } @Override public LongStream mapToLong(ToLongBiFunction mapper) { requireNonNull(mapper); return longStream(() -> new Spliteration().ofLong(mapper), ORDERED, NOT_PARALLEL) - .onClose(left::close) - .onClose(right::close); + .onClose(left::close) + .onClose(right::close); } @Override public BiStream mapKeys(Function keyMapper) { @@ -2333,7 +2362,7 @@ Spliterator.OfInt ofInt(ToIntBiFunction mapper) { return new AbstractIntSpliterator(estimateSize(), ORDERED) { @Override public boolean tryAdvance(IntConsumer consumer) { return advance() - && emit(mapper.applyAsInt(currentLeft.value, currentRight.value), consumer); + && emit(mapper.applyAsInt(currentLeft.value, currentRight.value), consumer); } }; } @@ -2342,7 +2371,7 @@ Spliterator.OfLong ofLong(ToLongBiFunction mapper) { return new AbstractLongSpliterator(estimateSize(), ORDERED) { @Override public boolean tryAdvance(LongConsumer consumer) { return advance() - && emit(mapper.applyAsLong(currentLeft.value, currentRight.value), consumer); + && emit(mapper.applyAsLong(currentLeft.value, currentRight.value), consumer); } }; } @@ -2351,7 +2380,7 @@ Spliterator.OfDouble ofDouble(ToDoubleBiFunction mapper) { return new AbstractDoubleSpliterator(estimateSize(), ORDERED) { @Override public boolean tryAdvance(DoubleConsumer consumer) { return advance() - && emit(mapper.applyAsDouble(currentLeft.value, currentRight.value), consumer); + && emit(mapper.applyAsDouble(currentLeft.value, currentRight.value), consumer); } }; } @@ -2417,10 +2446,10 @@ static T right(Both both) { } private static BiOptional fromOptionalEntry( - Optional> optional) { + Optional> optional) { return optional.isPresent() - ? BiOptional.of(optional.get().getKey(), optional.get().getValue()) - : BiOptional.empty(); + ? BiOptional.of(optional.get().getKey(), optional.get().getValue()) + : BiOptional.empty(); } private BiStream() {} diff --git a/mug/src/test/java/com/google/mu/util/stream/BiStreamFunctionEvaluationTest.java b/mug/src/test/java/com/google/mu/util/stream/BiStreamFunctionEvaluationTest.java index a028fdf1a5..fb87e2eefd 100644 --- a/mug/src/test/java/com/google/mu/util/stream/BiStreamFunctionEvaluationTest.java +++ b/mug/src/test/java/com/google/mu/util/stream/BiStreamFunctionEvaluationTest.java @@ -48,23 +48,31 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testKeys_toKeyFunctionCalledOnce() { assertThat(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 2).keys()) - .containsExactly("1", "2", "3") - .inOrder(); + .containsExactly("1", "2", "3") + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); } @Test public void testValues_toValueFunctionCalledOnce() { assertThat(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 2).values()) - .containsExactly(2, 4, 6) - .inOrder(); + .containsExactly(2, 4, 6) + .inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @Test public void testFilter_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).filter((k, v) -> v > 10)) - .containsExactly("2", 20, "3", 30) - .inOrder(); + .containsExactly("2", 20, "3", 30) + .inOrder(); + assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); + assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); + } + + @Test public void testFilterEntries_bothFunctionsCalledOnce() { + assertKeyValues( + biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).filter(entry -> entry.getKey().isEmpty() && entry.getValue() < 10)) + .isEmpty(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -72,7 +80,7 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFilterKeys_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).filterKeys(String::isEmpty)) - .isEmpty(); + .isEmpty(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -80,23 +88,23 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFilterValues_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).filterValues(v -> v < 0)) - .isEmpty(); + .isEmpty(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @Test public void testPeek_bothFunctionsCalledOnce() { assertKeyValues(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).peek((k, v) -> {})) - .containsExactly("1", 10, "2", 20, "3", 30) - .inOrder(); + .containsExactly("1", 10, "2", 20, "3", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @Test public void testDistinct_bothFunctionsCalledOnce() { assertKeyValues(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).distinct()) - .containsExactly("1", 10, "2", 20, "3", 30) - .inOrder(); + .containsExactly("1", 10, "2", 20, "3", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -104,9 +112,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testSortedByKeys_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .sortedByKeys(Comparator.naturalOrder())) - .containsExactly("1", 10, "2", 20, "3", 30) - .inOrder(); + .sortedByKeys(Comparator.naturalOrder())) + .containsExactly("1", 10, "2", 20, "3", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -114,9 +122,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testSortedByValues_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .sortedByValues(Comparator.naturalOrder())) - .containsExactly("1", 10, "2", 20, "3", 30) - .inOrder(); + .sortedByValues(Comparator.naturalOrder())) + .containsExactly("1", 10, "2", 20, "3", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -124,29 +132,29 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testMapKeys_toKeyFunctionCalledOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .mapKeys(k -> k + "." + k) - .keys()) - .containsExactly("1.1", "2.2", "3.3") - .inOrder(); + .mapKeys(k -> k + "." + k) + .keys()) + .containsExactly("1.1", "2.2", "3.3") + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); } @Test public void testMapValues_toValueFunctionCalledOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .mapValues(v -> v + 1) - .values()) - .containsExactly(11, 21, 31) - .inOrder(); + .mapValues(v -> v + 1) + .values()) + .containsExactly(11, 21, 31) + .inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @Test public void testMapKeysWithBiFunction_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .mapKeys((k, v) -> Joiner.on('.').join(k, v))) - .containsExactly("1.10", 10, "2.20", 20, "3.30", 30) - .inOrder(); + .mapKeys((k, v) -> Joiner.on('.').join(k, v))) + .containsExactly("1.10", 10, "2.20", 20, "3.30", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -154,9 +162,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testMapValuesWithBiFunction_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .mapValues((k, v) -> Joiner.on('.').join(k, v))) - .containsExactly("1", "1.10", "2", "2.20", "3", "3.30") - .inOrder(); + .mapValues((k, v) -> Joiner.on('.').join(k, v))) + .containsExactly("1", "1.10", "2", "2.20", "3", "3.30") + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -164,9 +172,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testMapWithBiFunctions_bothFunctionsCalledOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .map(Joiner.on('.')::join, (k, v) -> v + "<-" + k)) - .containsExactly("1.10", "10<-1", "2.20", "20<-2", "3.30", "30<-3") - .inOrder(); + .map(Joiner.on('.')::join, (k, v) -> v + "<-" + k)) + .containsExactly("1.10", "10<-1", "2.20", "20<-2", "3.30", "30<-3") + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -174,17 +182,17 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testMapToObj_bothFunctionsCalledOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .mapToObj(Joiner.on("->")::join)) - .containsExactly("1->10", "2->20", "3->30") - .inOrder(); + .mapToObj(Joiner.on("->")::join)) + .containsExactly("1->10", "2->20", "3->30") + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @Test public void testMapToInt_bothFunctionsCalledOnce() { assertThat(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).mapToInt((k, v) -> v)) - .containsExactly(10, 20, 30) - .inOrder(); + .containsExactly(10, 20, 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -192,9 +200,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testMapToLong_bothFunctionsCalledOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .mapToLong((k, v) -> Long.valueOf(k))) - .containsExactly(1L, 2L, 3L) - .inOrder(); + .mapToLong((k, v) -> Long.valueOf(k))) + .containsExactly(1L, 2L, 3L) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -202,10 +210,10 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testMapToDouble_bothFunctionsCalledOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .mapToDouble((k, v) -> Double.valueOf(k)) - .boxed()) - .containsExactly(1D, 2D, 3D) - .inOrder(); + .mapToDouble((k, v) -> Double.valueOf(k)) + .boxed()) + .containsExactly(1D, 2D, 3D) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -213,9 +221,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapKeys_bothFunctionsCalledOnlyOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapKeys(k -> Stream.of(k, k))) - .containsExactly("1", 10, "1", 10, "2", 20, "2", 20, "3", 30, "3", 30) - .inOrder(); + .flatMapKeys(k -> Stream.of(k, k))) + .containsExactly("1", 10, "1", 10, "2", 20, "2", 20, "3", 30, "3", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -223,9 +231,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapKeysWithBiFunction_bothFunctionsCalledOnlyOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapKeys((k, v) -> Stream.of(k, v))) - .containsExactly("1", 10, 10, 10, "2", 20, 20, 20, "3", 30, 30, 30) - .inOrder(); + .flatMapKeys((k, v) -> Stream.of(k, v))) + .containsExactly("1", 10, 10, 10, "2", 20, 20, 20, "3", 30, 30, 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -233,9 +241,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapValues_bothFunctionsCalledOnlyOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapValues(v -> Stream.of(v, v))) - .containsExactly("1", 10, "1", 10, "2", 20, "2", 20, "3", 30, "3", 30) - .inOrder(); + .flatMapValues(v -> Stream.of(v, v))) + .containsExactly("1", 10, "1", 10, "2", 20, "2", 20, "3", 30, "3", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -243,9 +251,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapValuesWithBiFunction_bothFunctionsCalledOnlyOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapValues((k, v) -> Stream.of(k, v))) - .containsExactly("1", "1", "1", 10, "2", "2", "2", 20, "3", "3", "3", 30) - .inOrder(); + .flatMapValues((k, v) -> Stream.of(k, v))) + .containsExactly("1", "1", "1", 10, "2", "2", "2", 20, "3", "3", "3", 30) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -253,9 +261,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMap_bothFunctionsCalledOnlyOnce() { assertKeyValues( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMap((k, v) -> BiStream.of(k, v, v, k))) - .containsExactly("1", 10, 10, "1", "2", 20, 20, "2", "3", 30, 30, "3") - .inOrder(); + .flatMap((k, v) -> BiStream.of(k, v, v, k))) + .containsExactly("1", 10, 10, "1", "2", 20, 20, "2", "3", 30, 30, "3") + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -263,9 +271,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapToObj_bothFunctionsCalledOnlyOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapToObj((k, v) -> Stream.of(v, k))) - .containsExactly(10, "1", 20, "2", 30, "3") - .inOrder(); + .flatMapToObj((k, v) -> Stream.of(v, k))) + .containsExactly(10, "1", 20, "2", 30, "3") + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -273,9 +281,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapToInt_bothFunctionsCalledOnlyOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapToInt((k, v) -> IntStream.of(v, Integer.parseInt(k)))) - .containsExactly(10, 1, 20, 2, 30, 3) - .inOrder(); + .flatMapToInt((k, v) -> IntStream.of(v, Integer.parseInt(k)))) + .containsExactly(10, 1, 20, 2, 30, 3) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -283,9 +291,9 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapToLong_bothFunctionsCalledOnlyOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapToLong((k, v) -> LongStream.of(Long.valueOf(v), Long.parseLong(k)))) - .containsExactly(10L, 1L, 20L, 2L, 30L, 3L) - .inOrder(); + .flatMapToLong((k, v) -> LongStream.of(Long.valueOf(v), Long.parseLong(k)))) + .containsExactly(10L, 1L, 20L, 2L, 30L, 3L) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -293,36 +301,36 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testFlatMapToDouble_bothFunctionsCalledOnlyOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10) - .flatMapToDouble((k, v) -> DoubleStream.of(v, Double.parseDouble(k))) - .boxed()) - .containsExactly(10D, 1D, 20D, 2D, 30D, 3D) - .inOrder(); + .flatMapToDouble((k, v) -> DoubleStream.of(v, Double.parseDouble(k))) + .boxed()) + .containsExactly(10D, 1D, 20D, 2D, 30D, 3D) + .inOrder(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @Test public void testInverseThenKeys_toValueFunctionCalledOnce() { assertThat(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 2).inverse().keys()) - .containsExactly(2, 4, 6); + .containsExactly(2, 4, 6); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @Test public void testInverseThenValues_toKeyFunctionCalledOnce() { assertThat(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 2).inverse().values()) - .containsExactly("1", "2", "3"); + .containsExactly("1", "2", "3"); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); } @Test public void testSkip_skippedEntriesNotEvaluated() { assertKeyValues(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 2).skip(2)) - .containsExactly("3", 6); + .containsExactly("3", 6); assertThat(evaluatedKeys).containsExactly(3).inOrder(); assertThat(evaluatedValues).containsExactly(3).inOrder(); } @Test public void testLimit_limitedEntriesNotEvaluated() { assertKeyValues(biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).limit(2)) - .containsExactly("1", 10, "2", 20); + .containsExactly("1", 10, "2", 20); assertThat(evaluatedKeys).containsExactly(1, 2).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2).inOrder(); } @@ -330,7 +338,7 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testAnyMatch_functionsCalledOnlyOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).anyMatch((k, v) -> v < 0)) - .isFalse(); + .isFalse(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -338,7 +346,7 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testAllMatch_functionsCalledOnlyOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).allMatch((k, v) -> v > 0)) - .isTrue(); + .isTrue(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } @@ -346,22 +354,22 @@ public final class BiStreamFunctionEvaluationTest extends TestCase { @Test public void testNoneMatch_functionsCalledOnlyOnce() { assertThat( biStream(Stream.of(1, 2, 3), Object::toString, i -> i * 10).noneMatch((k, v) -> v < 0)) - .isTrue(); + .isTrue(); assertThat(evaluatedKeys).containsExactly(1, 2, 3).inOrder(); assertThat(evaluatedValues).containsExactly(1, 2, 3).inOrder(); } private BiStream biStream( - Stream stream, - Function toKey, - Function toValue) { + Stream stream, + Function toKey, + Function toValue) { return BiStream.from( - stream, trackCallHistory(toKey, evaluatedKeys), trackCallHistory(toValue, evaluatedValues)); + stream, trackCallHistory(toKey, evaluatedKeys), trackCallHistory(toValue, evaluatedValues)); } // Poor man's mock private static Function trackCallHistory( - Function function, List history) { + Function function, List history) { return arg -> { history.add(arg); return function.apply(arg); @@ -379,13 +387,13 @@ private static MultimapSubject assertKeyValues(BiStream stream) { } public static Collector> toLinkedListMultimap( - Function toKey, Function toValue) { + Function toKey, Function toValue) { return Collector.of( - LinkedListMultimap::create, - (m, e) -> m.put(toKey.apply(e), toValue.apply(e)), - (m1, m2) -> { - m1.putAll(m2); - return m1; - }); + LinkedListMultimap::create, + (m, e) -> m.put(toKey.apply(e), toValue.apply(e)), + (m1, m2) -> { + m1.putAll(m2); + return m1; + }); } } diff --git a/mug/src/test/java/com/google/mu/util/stream/BiStreamInvariantsTest.java b/mug/src/test/java/com/google/mu/util/stream/BiStreamInvariantsTest.java index 7897fbfed5..fea62a319c 100644 --- a/mug/src/test/java/com/google/mu/util/stream/BiStreamInvariantsTest.java +++ b/mug/src/test/java/com/google/mu/util/stream/BiStreamInvariantsTest.java @@ -78,10 +78,10 @@ public class BiStreamInvariantsTest { @Parameters(name = "{index}: {0}/{1}") public static ImmutableList factories() { return Sets.cartesianProduct( - ImmutableSet.copyOf(BiStreamFactory.values()), ImmutableSet.copyOf(Variant.values())) - .stream() - .map(Collection::toArray) - .collect(toImmutableList()); + ImmutableSet.copyOf(BiStreamFactory.values()), ImmutableSet.copyOf(Variant.values())) + .stream() + .map(Collection::toArray) + .collect(toImmutableList()); } @Test public void empty() { @@ -90,59 +90,59 @@ public static ImmutableList factories() { @Test public void nonEmpty() { assertKeyValues(of("one", 1)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)) + .inOrder(); assertKeyValues(of("one", 1, "two", 2)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2)) + .inOrder(); assertKeyValues(of("one", 1, "two", 2, "three", 3)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2, "three", 3)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2, "three", 3)) + .inOrder(); } @Test public void mapKeys() { assertKeyValues(of("one", 1).mapKeys((k, v) -> k + v)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one1", 1)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one1", 1)) + .inOrder(); assertKeyValues(of("one", 1).mapKeys(k -> k + k)) - .containsExactlyEntriesIn(ImmutableMultimap.of("oneone", 1)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("oneone", 1)) + .inOrder(); assertKeyValues(of("one", 1).mapKeys(k -> null)) - .containsExactlyEntriesIn(keyValues(null, 1)) - .inOrder(); + .containsExactlyEntriesIn(keyValues(null, 1)) + .inOrder(); } @Test public void mapValues() { assertKeyValues(of("one", 1).mapValues((k, v) -> k + v)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", "one1")) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", "one1")) + .inOrder(); assertKeyValues(of("one", 1).mapValues(v -> v * 10)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 10)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 10)) + .inOrder(); assertKeyValues(of("one", 1).mapValues(v -> null)) - .containsExactlyEntriesIn(keyValues("one", null)) - .inOrder(); + .containsExactlyEntriesIn(keyValues("one", null)) + .inOrder(); } @Test public void distinct_byKey() { BiStream distinct = - biStream(Stream.of(1, 1, 2, 2, 3), x -> null).distinct(); + biStream(Stream.of(1, 1, 2, 2, 3), x -> null).distinct(); assertKeyValues(distinct).containsExactlyEntriesIn(keyValues(1, null, 2, null, 3, null)); } @Test public void distinct_byValue() { BiStream distinct = - biStream(k -> null, Stream.of(1, 1, 2, 2, 3)).distinct(); + biStream(k -> null, Stream.of(1, 1, 2, 2, 3)).distinct(); assertKeyValues(distinct).containsExactlyEntriesIn(keyValues(null, 1, null, 2, null, 3)); } @Test public void flatMap() { assertKeyValues(of("one", 1).flatMap((k, v) -> of(k, v * 10, k, v * 11))) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 10, "one", 11)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 10, "one", 11)) + .inOrder(); assertKeyValues(of("one", 1).flatMap((k, v) -> of(null, null))) - .containsExactlyEntriesIn(keyValues(null, null)) - .inOrder(); + .containsExactlyEntriesIn(keyValues(null, null)) + .inOrder(); } @Test public void flatMap_mapperReturnsNull() { @@ -151,14 +151,14 @@ public static ImmutableList factories() { @Test public void flatMapKeys() { assertKeyValues(of("one", 1).flatMapKeys((k, v) -> Stream.of(k, v))) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, 1, 1)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, 1, 1)) + .inOrder(); assertKeyValues(of("one", 1).flatMapKeys(k -> Stream.of(k, k + k))) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "oneone", 1)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "oneone", 1)) + .inOrder(); assertKeyValues(of("one", 1).flatMapKeys(k -> Stream.of(null, null))) - .containsExactlyEntriesIn(keyValues(null, 1, null, 1)) - .inOrder(); + .containsExactlyEntriesIn(keyValues(null, 1, null, 1)) + .inOrder(); } @Test public void flatMapKeys_mapperReturnsNull() { @@ -167,8 +167,8 @@ public static ImmutableList factories() { @Test public void mapKeysIfPresent_found() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapKeysIfPresent(ImmutableMap.of("uno", "one"))) - .containsExactly("one", 1) - .inOrder(); + .containsExactly("one", 1) + .inOrder(); } @Test public void mapKeysIfPresent_notFound() { @@ -177,36 +177,36 @@ public static ImmutableList factories() { @Test public void mapKeysIfPresent_present() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapKeysIfPresent(k -> Optional.of("found:" + k))) - .containsExactly("found:uno", 1, "found:dos", 2) - .inOrder(); + .containsExactly("found:uno", 1, "found:dos", 2) + .inOrder(); } @Test public void mapKeysIfPresent_absent() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapKeysIfPresent(k -> Optional.empty())) - .isEmpty(); + .isEmpty(); } @Test public void mapKeysIfPresent_biFunction_present() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapKeysIfPresent((k, v) -> Optional.of(k + "->" + v))) - .containsExactly("uno->1", 1, "dos->2", 2) - .inOrder(); + .containsExactly("uno->1", 1, "dos->2", 2) + .inOrder(); } @Test public void mapKeysIfPresent_biFunction_absent() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapKeysIfPresent((k, v) -> Optional.empty())) - .isEmpty(); + .isEmpty(); } @Test public void flatMapValues() { assertKeyValues(of("one", 1).flatMapValues((k, v) -> Stream.of(k, v))) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", "one", "one", 1)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", "one", "one", 1)) + .inOrder(); assertKeyValues(of("one", 1).flatMapValues(v -> Stream.of(v, v * 10))) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "one", 10)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "one", 10)) + .inOrder(); assertKeyValues(of("one", 1).flatMapValues(v -> Stream.of(null, null))) - .containsExactlyEntriesIn(keyValues("one", null, "one", null)) - .inOrder(); + .containsExactlyEntriesIn(keyValues("one", null, "one", null)) + .inOrder(); } @Test public void flatMapValues_mapperReturnsNull() { @@ -215,8 +215,8 @@ public static ImmutableList factories() { @Test public void mapValuesIfPresent_found() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapValuesIfPresent(ImmutableMap.of(1, "one"))) - .containsExactly("uno", "one") - .inOrder(); + .containsExactly("uno", "one") + .inOrder(); } @Test public void mapValuesIfPresent_notFound() { @@ -225,73 +225,83 @@ public static ImmutableList factories() { @Test public void mapValuesIfPresent_present() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapValuesIfPresent(v -> Optional.of("found:" + v))) - .containsExactly("uno", "found:1", "dos", "found:2") - .inOrder(); + .containsExactly("uno", "found:1", "dos", "found:2") + .inOrder(); } @Test public void mapValuesIfPresent_absent() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapValuesIfPresent(v -> Optional.empty())) - .isEmpty(); + .isEmpty(); } @Test public void mapValuesIfPresent_biFunction_present() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapValuesIfPresent((k, v) -> Optional.of(k + "->" + v))) - .containsExactly("uno", "uno->1", "dos", "dos->2") - .inOrder(); + .containsExactly("uno", "uno->1", "dos", "dos->2") + .inOrder(); } @Test public void mapValuesIfPresent_biFunction_absent() { assertKeyValues(BiStream.of("uno", 1, "dos", 2).mapValuesIfPresent((k, v) -> Optional.empty())) - .isEmpty(); + .isEmpty(); } @Test public void filter() { assertKeyValues(of("one", 1, "two", "two").filter((k, v) -> k.equals(v))) - .containsExactlyEntriesIn(ImmutableMultimap.of("two", "two")); + .containsExactlyEntriesIn(ImmutableMultimap.of("two", "two")); + } + + @Test public void filterEntries() { + assertKeyValues(of("one", 1, "two", "two").filter(entry -> entry.getKey().equals(entry.getValue()))) + .containsExactlyEntriesIn(ImmutableMultimap.of("two", "two")); } @Test public void filterKeys() { assertKeyValues(of("one", 1, "two", 2).filterKeys("one"::equals)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)); } @Test public void filterValues() { assertKeyValues(of("one", 1, "two", 2).filterValues(v -> v == 2)) - .containsExactlyEntriesIn(ImmutableMultimap.of("two", 2)); + .containsExactlyEntriesIn(ImmutableMultimap.of("two", 2)); + } + + @Test public void skipEntriesIf() { + assertKeyValues(of("one", 1, "two", "two").skipIf(entry -> entry.getKey().equals(entry.getValue()))) + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)); } @Test public void skipIf() { assertKeyValues(of("one", 1, "two", "two").skipIf((k, v) -> k.equals(v))) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)); } @Test public void skipKeysIf() { assertKeyValues(of("one", 1, "two", 2).skipKeysIf("one"::equals)) - .containsExactlyEntriesIn(ImmutableMultimap.of("two", 2)); + .containsExactlyEntriesIn(ImmutableMultimap.of("two", 2)); } @Test public void skipValuesIf() { assertKeyValues(of("one", 1, "two", 2).skipValuesIf(v -> v == 2)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1)); } @Test public void append() { assertKeyValues(of("one", 1).append(of("two", 2, "three", 3))) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2, "three", 3)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2, "three", 3)) + .inOrder(); assertKeyValues(of("one", 1).append("two", 2).append("three", 3)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2, "three", 3)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2, "three", 3)) + .inOrder(); assertKeyValues(of("one", 1).append("two", null).append(null, 3)) - .containsExactlyEntriesIn(keyValues("one", 1, "two", null, null, 3)) - .inOrder(); + .containsExactlyEntriesIn(keyValues("one", 1, "two", null, null, 3)) + .inOrder(); } @Test public void peek() { AtomicInteger sum = new AtomicInteger(); assertKeyValues(of(1, 2, 3, 4).peek((k, v) -> sum.addAndGet(k + v))) - .containsExactlyEntriesIn(ImmutableMultimap.of(1, 2, 3, 4)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of(1, 2, 3, 4)) + .inOrder(); assertThat(sum.get()).isEqualTo(10); } @@ -323,17 +333,17 @@ public static ImmutableList factories() { @Test public void findFirst_nullValueThrowx() { assertThrows(NullPointerException.class, of("one", null, "two", 2)::findFirst); assertThrows( - NullPointerException.class, of("one", 1, "two", 2).mapValues(v -> null)::findFirst); + NullPointerException.class, of("one", 1, "two", 2).mapValues(v -> null)::findFirst); } @Test public void findFirst_nullKeyMapsToNonNull() { assertThat(of(null, 1, "two", 2).mapKeys(k -> Objects.toString(k)).findFirst()) - .isEqualTo(BiOptional.of("null", 1)); + .isEqualTo(BiOptional.of("null", 1)); } @Test public void findFirst_nullValueMapsToNonNull() { assertThat(of("one", null, "two", 2).mapValues(v -> Objects.toString(v)).findFirst()) - .isEqualTo(BiOptional.of("one", "null")); + .isEqualTo(BiOptional.of("one", "null")); } @Test public void findAny() { @@ -353,12 +363,12 @@ public static ImmutableList factories() { @Test public void findAny_nullKeyMapsToNonNull() { assertThat(of(null, 1).mapKeys(k -> Objects.toString(k)).findAny()) - .isEqualTo(BiOptional.of("null", 1)); + .isEqualTo(BiOptional.of("null", 1)); } @Test public void findAny_nullValueMapsToNonNull() { assertThat(of("one", null).mapValues(v -> Objects.toString(v)).findAny()) - .isEqualTo(BiOptional.of("one", "null")); + .isEqualTo(BiOptional.of("one", "null")); } @Test public void keys() { @@ -369,14 +379,14 @@ public static ImmutableList factories() { List mappedValues = new ArrayList<>(); assertThat( of("one", 1, "two", 2) - .mapValues( - v -> { - mappedValues.add(v); - return v; - }) - .keys()) - .containsExactly("one", "two") - .inOrder(); + .mapValues( + v -> { + mappedValues.add(v); + return v; + }) + .keys()) + .containsExactly("one", "two") + .inOrder(); assertThat(mappedValues).containsExactly(1, 2).inOrder(); } @@ -388,30 +398,30 @@ public static ImmutableList factories() { List mappedKeys = new ArrayList<>(); assertThat( of("one", 1, "two", 2) - .mapKeys( - k -> { - mappedKeys.add(k); - return k; - }) - .values()) - .containsExactly(1, 2) - .inOrder(); + .mapKeys( + k -> { + mappedKeys.add(k); + return k; + }) + .values()) + .containsExactly(1, 2) + .inOrder(); assertThat(mappedKeys).containsExactly("one", "two").inOrder(); } @Test public void testMapToObj() { assertThat(of("one", 1, "two", 2).mapToObj(Joiner.on(':')::join)) - .containsExactly("one:1", "two:2"); + .containsExactly("one:1", "two:2"); } @Test public void testMapToObjIfPresent_present() { assertThat(of("one", 1, "two", 2).mapToObjIfPresent((k, v) -> Optional.of(k + ":" + v))) - .containsExactly("one:1", "two:2"); + .containsExactly("one:1", "two:2"); } @Test public void testMapToObjIfPresent_absent() { assertThat(of("one", 1, "two", 2).mapToObjIfPresent((k, v) -> Optional.empty())) - .isEmpty(); + .isEmpty(); } @Test public void collectToMap() { @@ -420,7 +430,7 @@ public static ImmutableList factories() { @Test public void toCollect_toMapMergingValues() { assertThat(of("k", 1, "k", 2).collect(toMap((v1, v2) -> v1 * 10 + v2))) - .containsExactly("k", 12); + .containsExactly("k", 12); } @Test public void collect() { @@ -430,14 +440,14 @@ public static ImmutableList factories() { @Test public void collect_toImmutableListMultimapWithInflexibleMapperTypes() { ImmutableListMultimap multimap = - of("one", 1, "one", 10, "two", 2).collect(new BiCollector>() { - @Override - public Collector> collectorOf(Function toKey, Function toValue) { - return BiStreamInvariantsTest.toImmutableMultimap(toKey,toValue); - } - }); + of("one", 1, "one", 10, "two", 2).collect(new BiCollector>() { + @Override + public Collector> collectorOf(Function toKey, Function toValue) { + return BiStreamInvariantsTest.toImmutableMultimap(toKey,toValue); + } + }); assertThat(multimap) - .containsExactlyEntriesIn(ImmutableListMultimap.of("one", 1, "one", 10, "two", 2)); + .containsExactlyEntriesIn(ImmutableListMultimap.of("one", 1, "one", 10, "two", 2)); } @Test public void forEach() { @@ -449,11 +459,11 @@ public static ImmutableList factories() { @Test public void forEachOrdered() { List list = new ArrayList<>(); of(1, 2, 3, 4) - .forEachOrdered( - (k, v) -> { - list.add(k); - list.add(v); - }); + .forEachOrdered( + (k, v) -> { + list.add(k); + list.add(v); + }); assertThat(list).containsExactly(1, 2, 3, 4).inOrder(); } @@ -463,20 +473,20 @@ public static ImmutableList factories() { @Test public void map() { assertKeyValues(of(1, "one", 2, "two").map(Joiner.on(':')::join, (k, v) -> v + ":" + k)) - .containsExactly("1:one", "one:1", "2:two", "two:2") - .inOrder(); + .containsExactly("1:one", "one:1", "2:two", "two:2") + .inOrder(); } @Test public void map_toPair() { assertKeyValues(of(1, "a:foo", 2, "b:bar").map((l, t) -> first(':').split(t).orElseThrow())) - .containsExactly("a", "foo", "b", "bar") - .inOrder(); + .containsExactly("a", "foo", "b", "bar") + .inOrder(); } @Test public void mapIfPresent_mapToPresent() { assertKeyValues(of(1, "one", 2, "two").mapIfPresent((k, v) -> BiOptional.of(k + ":" + v, v + ":" + k))) - .containsExactly("1:one", "one:1", "2:two", "two:2") - .inOrder(); + .containsExactly("1:one", "one:1", "2:two", "two:2") + .inOrder(); } @Test public void mapIfPresent_mapToEnty() { @@ -489,26 +499,26 @@ public static ImmutableList factories() { @Test public void mapToInt() { assertThat(of(1, 2, 3, 4).mapToInt((k, v) -> k * 10 + v).boxed()) - .containsExactly(12, 34) - .inOrder(); + .containsExactly(12, 34) + .inOrder(); } @Test public void mapToLong() { assertThat(of(1, 2, 3, 4).mapToLong((k, v) -> k * 10 + v).boxed()) - .containsExactly(12L, 34L) - .inOrder(); + .containsExactly(12L, 34L) + .inOrder(); } @Test public void mapToDouble() { assertThat(of(1, 2, 3, 4).mapToDouble((k, v) -> k * 10 + v).boxed()) - .containsExactly(12D, 34D) - .inOrder(); + .containsExactly(12D, 34D) + .inOrder(); } @Test public void flatMapToObj() { assertThat(of(1, 2, 3, 4).flatMapToObj((k, n) -> Collections.nCopies(n, k).stream())) - .containsExactly(1, 1, 3, 3, 3, 3) - .inOrder(); + .containsExactly(1, 1, 3, 3, 3, 3) + .inOrder(); } @Test public void flatMapToObj_mapperReturnsNull() { @@ -518,10 +528,10 @@ public static ImmutableList factories() { @Test public void flatMapToInt() { assertThat( of(1, 2, 3, 4) - .flatMapToInt((k, n) -> Collections.nCopies(n, k).stream().mapToInt(i -> i)) - .boxed()) - .containsExactly(1, 1, 3, 3, 3, 3) - .inOrder(); + .flatMapToInt((k, n) -> Collections.nCopies(n, k).stream().mapToInt(i -> i)) + .boxed()) + .containsExactly(1, 1, 3, 3, 3, 3) + .inOrder(); } @Test public void flatMapToInt_mapperReturnsNull() { @@ -531,10 +541,10 @@ public static ImmutableList factories() { @Test public void flatMapToLong() { assertThat( of(1, 2, 3, 4) - .flatMapToLong((k, n) -> Collections.nCopies(n, k).stream().mapToLong(i -> i)) - .boxed()) - .containsExactly(1L, 1L, 3L, 3L, 3L, 3L) - .inOrder(); + .flatMapToLong((k, n) -> Collections.nCopies(n, k).stream().mapToLong(i -> i)) + .boxed()) + .containsExactly(1L, 1L, 3L, 3L, 3L, 3L) + .inOrder(); } @Test public void flatMapToLong_mapperReturnsNull() { @@ -544,10 +554,10 @@ public static ImmutableList factories() { @Test public void flatMapToDouble() { assertThat( of(1, 2, 3, 4) - .flatMapToDouble((k, n) -> Collections.nCopies(n, k).stream().mapToDouble(i -> i)) - .boxed()) - .containsExactly(1D, 1D, 3D, 3D, 3D, 3D) - .inOrder(); + .flatMapToDouble((k, n) -> Collections.nCopies(n, k).stream().mapToDouble(i -> i)) + .boxed()) + .containsExactly(1D, 1D, 3D, 3D, 3D, 3D) + .inOrder(); } @Test public void flatMapToDouble_mapperReturnsNull() { @@ -556,152 +566,152 @@ public static ImmutableList factories() { @Test public void limit() { assertKeyValues(of("one", 1, "two", 2, "three", 3).limit(2)) - .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("one", 1, "two", 2)) + .inOrder(); } @Test public void skip() { assertKeyValues(of("one", 1, "two", 2, "three", 3).skip(1)) - .containsExactlyEntriesIn(ImmutableMultimap.of("two", 2, "three", 3)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("two", 2, "three", 3)) + .inOrder(); } @Test public void sortedByKeys() { assertKeyValues(of("a", 1, "c", 2, "b", 3).sortedByKeys(naturalOrder())) - .containsExactlyEntriesIn(ImmutableMultimap.of("a", 1, "b", 3, "c", 2)); + .containsExactlyEntriesIn(ImmutableMultimap.of("a", 1, "b", 3, "c", 2)); } @Test public void sortedByValues() { assertKeyValues(of("a", 3, "b", 1, "c", 2).sortedByValues(naturalOrder())) - .containsExactlyEntriesIn(ImmutableMultimap.of("b", 1, "c", 2, "a", 3)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("b", 1, "c", 2, "a", 3)) + .inOrder(); } @Test public void testMaxByKey_found() { assertThat(of(1, "y", 2, "x").collect(maxByKey(naturalOrder()))) - .isEqualTo(BiOptional.of(2, "x")); + .isEqualTo(BiOptional.of(2, "x")); } - @Test public void testMaxByKey_multipleMax_firstWins() { + @Test public void testMaxByKey_multipleMax_firstWins() { assertThat(of(1, "y", 2, "x", 2, "a").collect(maxByKey(naturalOrder()))) - .isEqualTo(BiOptional.of(2, "x")); + .isEqualTo(BiOptional.of(2, "x")); } - @Test public void testMaxByKey_notFound() { + @Test public void testMaxByKey_notFound() { assertThat(this.of().collect(maxByKey(naturalOrder()))) - .isEqualTo(BiOptional.empty()); + .isEqualTo(BiOptional.empty()); } - @Test public void testMinByKey_found() { + @Test public void testMinByKey_found() { assertThat(of(1, "y", 2, "x").collect(minByKey(naturalOrder()))) - .isEqualTo(BiOptional.of(1, "y")); + .isEqualTo(BiOptional.of(1, "y")); } - @Test public void testMinByKey_multipleMin_firstWins() { + @Test public void testMinByKey_multipleMin_firstWins() { assertThat(of(1, "y", 2, "x", 1, "a").collect(minByKey(naturalOrder()))) - .isEqualTo(BiOptional.of(1, "y")); + .isEqualTo(BiOptional.of(1, "y")); } - @Test public void testMinByKey_notFound() { + @Test public void testMinByKey_notFound() { assertThat(this.of().collect(minByKey(naturalOrder()))) - .isEqualTo(BiOptional.empty()); - } - - @Test public void testMaxByValue_found() { - assertThat(of(1, "y", 2, "x").collect(maxByValue(naturalOrder()))) - .isEqualTo(BiOptional.of(1, "y")); - } - -@Test public void testMaxByValue_multipleMax_firstWins() { - assertThat(of(1, "x", 2, "y", 3, "y").collect(maxByValue(naturalOrder()))) - .isEqualTo(BiOptional.of(2, "y")); - } - -@Test public void testMaxByValue_notFound() { - assertThat(this.of().collect(maxByValue(naturalOrder()))) - .isEqualTo(BiOptional.empty()); - } - -@Test public void testMinByValue_found() { - assertThat(of(1, "y", 2, "x").collect(minByValue(naturalOrder()))) - .isEqualTo(BiOptional.of(2, "x")); - } - -@Test public void testMinByValue_multipleMin_firstWins() { - assertThat(of(1, "y", 3, "x", 2, "x").collect(minByValue(naturalOrder()))) - .isEqualTo(BiOptional.of(3, "x")); - } - -@Test public void testMinByValue_notFound() { - assertThat(this.of().collect(minByValue(naturalOrder()))) - .isEqualTo(BiOptional.empty()); - } - -@Test -public void testMinBy_found() { - assertThat(of(1, "y", 2, "x").collect(minBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.of(1, "y")); - assertThat(of(1, "y", 1, "x").collect(minBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.of(1, "x")); -} + .isEqualTo(BiOptional.empty()); + } -@Test -public void testMinBy_breakTieByValue() { - assertThat(of(1, "y", 2, "x", 1, "a").collect(minBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.of(1, "a")); -} + @Test public void testMaxByValue_found() { + assertThat(of(1, "y", 2, "x").collect(maxByValue(naturalOrder()))) + .isEqualTo(BiOptional.of(1, "y")); + } -@Test -public void testMinBy_notFound() { - assertThat(this.of().collect(minBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.empty()); -} + @Test public void testMaxByValue_multipleMax_firstWins() { + assertThat(of(1, "x", 2, "y", 3, "y").collect(maxByValue(naturalOrder()))) + .isEqualTo(BiOptional.of(2, "y")); + } -@Test -public void testMaxBy_found() { - assertThat(of(1, "y", 2, "x").collect(maxBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.of(2, "x")); - assertThat(of(1, "y", 1, "x").collect(maxBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.of(1, "y")); -} + @Test public void testMaxByValue_notFound() { + assertThat(this.of().collect(maxByValue(naturalOrder()))) + .isEqualTo(BiOptional.empty()); + } -@Test -public void testMaxBy_breakTieByValue() { - assertThat(of(3, "x", 2, "y", 3, "y").collect(maxBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.of(3, "y")); -} + @Test public void testMinByValue_found() { + assertThat(of(1, "y", 2, "x").collect(minByValue(naturalOrder()))) + .isEqualTo(BiOptional.of(2, "x")); + } -@Test -public void testMaxBy_notFound() { - assertThat(this.of().collect(maxBy(naturalOrder(), naturalOrder()))) - .isEqualTo(BiOptional.empty()); -} + @Test public void testMinByValue_multipleMin_firstWins() { + assertThat(of(1, "y", 3, "x", 2, "x").collect(minByValue(naturalOrder()))) + .isEqualTo(BiOptional.of(3, "x")); + } + + @Test public void testMinByValue_notFound() { + assertThat(this.of().collect(minByValue(naturalOrder()))) + .isEqualTo(BiOptional.empty()); + } + + @Test + public void testMinBy_found() { + assertThat(of(1, "y", 2, "x").collect(minBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.of(1, "y")); + assertThat(of(1, "y", 1, "x").collect(minBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.of(1, "x")); + } + + @Test + public void testMinBy_breakTieByValue() { + assertThat(of(1, "y", 2, "x", 1, "a").collect(minBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.of(1, "a")); + } + + @Test + public void testMinBy_notFound() { + assertThat(this.of().collect(minBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.empty()); + } + + @Test + public void testMaxBy_found() { + assertThat(of(1, "y", 2, "x").collect(maxBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.of(2, "x")); + assertThat(of(1, "y", 1, "x").collect(maxBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.of(1, "y")); + } + + @Test + public void testMaxBy_breakTieByValue() { + assertThat(of(3, "x", 2, "y", 3, "y").collect(maxBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.of(3, "y")); + } + + @Test + public void testMaxBy_notFound() { + assertThat(this.of().collect(maxBy(naturalOrder(), naturalOrder()))) + .isEqualTo(BiOptional.empty()); + } @Test public void distinct() { assertKeyValues(of("a", 1, "b", 2, "a", 1).distinct()) - .containsExactlyEntriesIn(ImmutableMultimap.of("a", 1, "b", 2)) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of("a", 1, "b", 2)) + .inOrder(); assertKeyValues(of("a", null, "a", null).distinct()) - .containsExactlyEntriesIn(keyValues("a", null)) - .inOrder(); + .containsExactlyEntriesIn(keyValues("a", null)) + .inOrder(); assertKeyValues(of("a", null, "b", null).distinct()) - .containsExactlyEntriesIn(keyValues("a", null, "b", null)) - .inOrder(); + .containsExactlyEntriesIn(keyValues("a", null, "b", null)) + .inOrder(); assertKeyValues(of(null, 1, null, 1).distinct()) - .containsExactlyEntriesIn(keyValues(null, 1)) - .inOrder(); + .containsExactlyEntriesIn(keyValues(null, 1)) + .inOrder(); assertKeyValues(of(null, 1, null, 2).distinct()) - .containsExactlyEntriesIn(keyValues(null, 1, null, 2)) - .inOrder(); + .containsExactlyEntriesIn(keyValues(null, 1, null, 2)) + .inOrder(); assertKeyValues(of(null, null, null, null).distinct()) - .containsExactlyEntriesIn(keyValues(null, null)) - .inOrder(); + .containsExactlyEntriesIn(keyValues(null, null)) + .inOrder(); } @Test public void inverse() { assertKeyValues(of("a", 1).inverse()) - .containsExactlyEntriesIn(ImmutableMultimap.of(1, "a")) - .inOrder(); + .containsExactlyEntriesIn(ImmutableMultimap.of(1, "a")) + .inOrder(); } @Test public void testGroupConsecutiveBy_emptyStream() { @@ -710,18 +720,18 @@ public void testMaxBy_notFound() { @Test public void testGroupConsecutiveBy_singleElement() { assertKeyValues(of("1", 1).groupConsecutiveBy(identity(), toList())) - .containsExactly("1", asList(1)); + .containsExactly("1", asList(1)); } @Test public void testGroupConsecutiveBy_twoElementsSameRun() { assertKeyValues(of("k", "a", "k", "b").groupConsecutiveBy(identity(), toList())) - .containsExactly("k", asList("a", "b")); + .containsExactly("k", asList("a", "b")); } @Test public void testGroupConsecutiveBy_twoElementsDifferentRuns() { assertKeyValues(of("k1", 1, "k2", 2).groupConsecutiveBy(identity(), toList())) - .containsExactly("k1", asList(1), "k2", asList(2)) - .inOrder(); + .containsExactly("k1", asList(1), "k2", asList(2)) + .inOrder(); } @Test public void testGroupConsecutiveIf_emptyStream() { @@ -730,18 +740,18 @@ public void testMaxBy_notFound() { @Test public void testGroupConsecutiveIf_singleElement() { assertThat(of("1", 1).groupConsecutiveIf(Object::equals, toList())) - .containsExactly(asList(1)); + .containsExactly(asList(1)); } @Test public void testGroupConsecutiveIf_twoElementsSameRun() { assertThat(of("k", "a", "k", "b").groupConsecutiveIf(Object::equals, toList())) - .containsExactly(asList("a", "b")); + .containsExactly(asList("a", "b")); } @Test public void testGroupConsecutiveIf_twoElementsDifferentRuns() { assertThat(of("k1", 1, "k2", 2).groupConsecutiveIf(Object::equals, Integer::sum)) - .containsExactly(1, 2) - .inOrder(); + .containsExactly(1, 2) + .inOrder(); } @Test public void testGroupConsecutiveBy_withGroupReducer_emptyStream() { @@ -750,18 +760,18 @@ public void testMaxBy_notFound() { @Test public void testGroupConsecutiveBy_withGroupReducer_singleElement() { assertKeyValues(of("1", 1).groupConsecutiveBy(identity(), Integer::sum)) - .containsExactly("1", 1); + .containsExactly("1", 1); } @Test public void testGroupConsecutiveBy_withGroupReducer_twoElementsSameRun() { assertKeyValues(of("k", "a", "k", "b").groupConsecutiveBy(identity(), String::concat)) - .containsExactly("k", "ab"); + .containsExactly("k", "ab"); } @Test public void testGroupConsecutiveBy_withGroupReducer_twoElementsDifferentRuns() { assertKeyValues(of("k1", 1, "k2", 2).groupConsecutiveBy(identity(), Integer::sum)) - .containsExactly("k1", 1, "k2", 2) - .inOrder(); + .containsExactly("k1", 1, "k2", 2) + .inOrder(); } @Test public void testGroupConsecutiveBy_groupWithBiCollector_emptyStream() { @@ -771,19 +781,19 @@ public void testMaxBy_notFound() { @Test public void testGroupConsecutiveBy_groupWithBiCollector_singleElement() { assertKeyValues( of("1", 1).groupConsecutiveBy((k, v) -> Integer.parseInt(k), toMap())) - .containsExactly(1, ImmutableMap.of("1", 1)); + .containsExactly(1, ImmutableMap.of("1", 1)); } @Test public void testGroupConsecutiveBy_groupWithBiCollector_twoElementsSameRun() { assertKeyValues( of("k", "a", "k", "b").groupConsecutiveBy((k, v) -> k, ImmutableListMultimap::toImmutableListMultimap)) - .containsExactly("k", ImmutableListMultimap.of("k", "a", "k", "b")); + .containsExactly("k", ImmutableListMultimap.of("k", "a", "k", "b")); } @Test public void testGroupConsecutiveBy_groupWithBiCollector_twoElementsDifferentRuns() { assertKeyValues(of("k1", 1, "k2", 2).groupConsecutiveBy((k, v) -> k, toMap())) - .containsExactly("k1", ImmutableMap.of("k1", 1), "k2", ImmutableMap.of("k2", 2)) - .inOrder(); + .containsExactly("k1", ImmutableMap.of("k1", 1), "k2", ImmutableMap.of("k2", 2)) + .inOrder(); } private BiStream of() { @@ -815,19 +825,19 @@ static MultimapSubject assertKeyValues(BiStream stream) { // Intentionally declare the parameter types without wildcards, to make sure // BiCollector can still work with such naive method references. public static Collector> toImmutableMultimap( - Function keyMapper, Function valueMapper) { + Function keyMapper, Function valueMapper) { return ImmutableListMultimap.toImmutableListMultimap(keyMapper, valueMapper); } private static Collector> toLinkedListMultimap( - Function toKey, Function toValue) { + Function toKey, Function toValue) { return Collector.of( - LinkedListMultimap::create, - (m, e) -> m.put(toKey.apply(e), toValue.apply(e)), - (m1, m2) -> { - m1.putAll(m2); - return m1; - }); + LinkedListMultimap::create, + (m, e) -> m.put(toKey.apply(e), toValue.apply(e)), + (m1, m2) -> { + m1.putAll(m2); + return m1; + }); } private static ListMultimap keyValues(K key, V value) { @@ -899,54 +909,54 @@ BiStream newBiStream(K k1, V v1, K k2, V v2, K k3, V v3) { @Override BiStream newBiStream() { return BiStream.from( - ImmutableListMultimap.of().entries().stream(), - Map.Entry::getKey, - Map.Entry::getValue); + ImmutableListMultimap.of().entries().stream(), + Map.Entry::getKey, + Map.Entry::getValue); } @Override BiStream newBiStream(K key, V value) { return BiStream.from( - keyValues(value, key).entries().stream(), Map.Entry::getValue, Map.Entry::getKey); + keyValues(value, key).entries().stream(), Map.Entry::getValue, Map.Entry::getKey); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2) { return BiStream.from( - keyValues(k1, v1, k2, v2).entries().stream(), Map.Entry::getKey, Map.Entry::getValue); + keyValues(k1, v1, k2, v2).entries().stream(), Map.Entry::getKey, Map.Entry::getValue); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2, K k3, V v3) { return BiStream.from( - keyValues(k1, v1, k2, v2, k3, v3).entries().stream(), - Map.Entry::getKey, - Map.Entry::getValue); + keyValues(k1, v1, k2, v2, k3, v3).entries().stream(), + Map.Entry::getKey, + Map.Entry::getValue); } }, COLLECTED_FROM_GENERIC_ENTRY_STREAM { @Override BiStream newBiStream() { return ImmutableListMultimap.of().entries().stream() - .collect(toBiStream(Map.Entry::getKey, Map.Entry::getValue)); + .collect(toBiStream(Map.Entry::getKey, Map.Entry::getValue)); } @Override BiStream newBiStream(K key, V value) { return keyValues(value, key).entries().stream() - .collect(toBiStream(Map.Entry::getValue, Map.Entry::getKey)); + .collect(toBiStream(Map.Entry::getValue, Map.Entry::getKey)); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2) { return keyValues(k1, v1, k2, v2).entries().stream() - .collect(toBiStream(Map.Entry::getKey, Map.Entry::getValue)); + .collect(toBiStream(Map.Entry::getKey, Map.Entry::getValue)); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2, K k3, V v3) { return keyValues(k1, v1, k2, v2, k3, v3).entries().stream() - .collect(toBiStream(Map.Entry::getKey, Map.Entry::getValue)); + .collect(toBiStream(Map.Entry::getKey, Map.Entry::getValue)); } }, FROM_PAIRS { @@ -974,25 +984,25 @@ BiStream newBiStream(K k1, V v1, K k2, V v2, K k3, V v3) { @Override BiStream newBiStream() { return ImmutableListMultimap.of().entries().stream() - .collect(toBiStream(this::fromEntry)); + .collect(toBiStream(this::fromEntry)); } @Override BiStream newBiStream(K key, V value) { return keyValues(key, value).entries().stream() - .collect(toBiStream(this::fromEntry)); + .collect(toBiStream(this::fromEntry)); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2) { return keyValues(k1, v1, k2, v2).entries().stream() - .collect(toBiStream(this::fromEntry)); + .collect(toBiStream(this::fromEntry)); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2, K k3, V v3) { return keyValues(k1, v1, k2, v2, k3, v3).entries().stream() - .collect(toBiStream(this::fromEntry)); + .collect(toBiStream(this::fromEntry)); } private Both fromEntry(Map.Entry entry) { @@ -1003,25 +1013,25 @@ private Both fromEntry(Map.Entry entry) { @Override BiStream newBiStream() { return BiStream.from( - ImmutableListMultimap.of().entries(), Map.Entry::getKey, Map.Entry::getValue); + ImmutableListMultimap.of().entries(), Map.Entry::getKey, Map.Entry::getValue); } @Override BiStream newBiStream(K key, V value) { return BiStream.from( - keyValues(value, key).entries(), Map.Entry::getValue, Map.Entry::getKey); + keyValues(value, key).entries(), Map.Entry::getValue, Map.Entry::getKey); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2) { return BiStream.from( - keyValues(k1, v1, k2, v2).entries(), Map.Entry::getKey, Map.Entry::getValue); + keyValues(k1, v1, k2, v2).entries(), Map.Entry::getKey, Map.Entry::getValue); } @Override BiStream newBiStream(K k1, V v1, K k2, V v2, K k3, V v3) { return BiStream.from( - keyValues(k1, v1, k2, v2, k3, v3).entries(), Map.Entry::getKey, Map.Entry::getValue); + keyValues(k1, v1, k2, v2, k3, v3).entries(), Map.Entry::getKey, Map.Entry::getValue); } }, FROM_ZIP { @@ -1112,6 +1122,12 @@ BiStream wrap(BiStream stream) { return stream.filter((k, v) -> true); } }, + TRIVIAL_FILTER_ENTRY { + @Override + BiStream wrap(BiStream stream) { + return stream.filter(entry -> true); + } + }, TRIVIAL_FILTER_KEY { @Override BiStream wrap(BiStream stream) { @@ -1130,6 +1146,12 @@ BiStream wrap(BiStream stream) { return stream.skipIf((k, v) -> false); } }, + TRIVIAL_SKIP_ENTRIES_IF { + @Override + BiStream wrap(BiStream stream) { + return stream.skipIf(entry -> false); + } + }, TRIVIAL_SKIP_KEYS_IF { @Override BiStream wrap(BiStream stream) {