Skip to content

Commit

Permalink
Extend StreamIsEmpty Refaster rule (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannizotta authored Feb 11, 2024
1 parent 57fa6ae commit 1f71ccc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.filtering;
import static java.util.stream.Collectors.flatMapping;
Expand Down Expand Up @@ -37,6 +38,7 @@
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BinaryOperator;
Expand Down Expand Up @@ -254,15 +256,21 @@ Optional<S> after(Stream<T> stream, Function<? super T, S> function) {
// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
// (Perhaps we could add a `@Matches` guard that validates that the collector expression does not
// contain a `Collectors#filtering` call. That'd still not be 100% accurate, though.)
static final class StreamIsEmpty<T> {
static final class StreamIsEmpty<T, K, V, C extends Collection<K>, M extends Map<K, V>> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends Collection<?>> collector) {
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends C> collector) {
return Refaster.anyOf(
stream.count() == 0,
stream.count() <= 0,
stream.count() < 1,
stream.findFirst().isEmpty(),
stream.collect(collector).isEmpty());
stream.collect(collector).isEmpty(),
stream.collect(collectingAndThen(collector, C::isEmpty)));
}

@BeforeTemplate
boolean before2(Stream<T> stream, Collector<? super T, ?, ? extends M> collector) {
return stream.collect(collectingAndThen(collector, M::isEmpty));
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.filtering;
import static java.util.stream.Collectors.flatMapping;
Expand All @@ -21,11 +24,14 @@
import static java.util.stream.Collectors.summingLong;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -38,8 +44,12 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
ImmutableMap.class,
List.class,
Map.class,
Objects.class,
Streams.class,
collectingAndThen(null, null),
counting(),
filtering(null, null),
flatMapping(null, null),
Expand All @@ -54,7 +64,9 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
summarizingLong(null),
summingDouble(null),
summingInt(null),
summingLong(null));
summingLong(null),
toImmutableList(),
toImmutableMap(null, null));
}

String testJoining() {
Expand Down Expand Up @@ -114,7 +126,12 @@ ImmutableSet<Boolean> testStreamIsEmpty() {
Stream.of(2).count() <= 0,
Stream.of(3).count() < 1,
Stream.of(4).findFirst().isEmpty(),
Stream.of(5).collect(toImmutableSet()).isEmpty());
Stream.of(5).collect(toImmutableSet()).isEmpty(),
Stream.of(6).collect(collectingAndThen(toImmutableList(), List::isEmpty)),
Stream.of(7).collect(collectingAndThen(toImmutableList(), ImmutableList::isEmpty)),
Stream.of(8).collect(collectingAndThen(toImmutableMap(k -> k, v -> v), Map::isEmpty)),
Stream.of(9)
.collect(collectingAndThen(toImmutableMap(k -> k, v -> v), ImmutableMap::isEmpty)));
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.filtering;
import static java.util.stream.Collectors.flatMapping;
Expand All @@ -22,12 +25,15 @@
import static java.util.stream.Collectors.summingLong;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -40,8 +46,12 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
ImmutableMap.class,
List.class,
Map.class,
Objects.class,
Streams.class,
collectingAndThen(null, null),
counting(),
filtering(null, null),
flatMapping(null, null),
Expand All @@ -56,7 +66,9 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
summarizingLong(null),
summingDouble(null),
summingInt(null),
summingLong(null));
summingLong(null),
toImmutableList(),
toImmutableMap(null, null));
}

String testJoining() {
Expand Down Expand Up @@ -115,7 +127,11 @@ ImmutableSet<Boolean> testStreamIsEmpty() {
Stream.of(2).findAny().isEmpty(),
Stream.of(3).findAny().isEmpty(),
Stream.of(4).findAny().isEmpty(),
Stream.of(5).findAny().isEmpty());
Stream.of(5).findAny().isEmpty(),
Stream.of(6).findAny().isEmpty(),
Stream.of(7).findAny().isEmpty(),
Stream.of(8).findAny().isEmpty(),
Stream.of(9).findAny().isEmpty());
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
Expand Down

0 comments on commit 1f71ccc

Please sign in to comment.