Skip to content

Commit

Permalink
Compact and replace StreamIs{,Not}Empty Refaster rules
Browse files Browse the repository at this point in the history
The new `StreamFindAnyIs{Empty,Present}` rules are simpler thanks to the
use of `@AlsoNegation`. In some cases an additional application of the
`OptionalIsEmpty` rule will be required.
  • Loading branch information
Stephan202 committed Feb 11, 2024
1 parent 433b8b9 commit 4cbc5a0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.AlsoNegation;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Matches;
import com.google.errorprone.refaster.annotation.MayOptionallyUse;
Expand Down Expand Up @@ -256,7 +257,7 @@ 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, K, V, C extends Collection<K>, M extends Map<K, V>> {
static final class StreamFindAnyIsEmpty<T, K, V, C extends Collection<K>, M extends Map<K, V>> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends C> collector) {
return Refaster.anyOf(
Expand All @@ -274,20 +275,20 @@ boolean before2(Stream<T> stream, Collector<? super T, ?, ? extends M> collector
}

@AfterTemplate
@AlsoNegation
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

/** In order to test whether a stream has any element, simply try to find one. */
static final class StreamIsNotEmpty<T> {
/**
* Prefer {@link Stream#findAny()} over {@link Stream#findFirst()} if one only cares whether the
* stream is nonempty.
*/
static final class StreamFindAnyIsPresent<T> {
@BeforeTemplate
boolean before(Stream<T> stream) {
return Refaster.anyOf(
stream.count() != 0,
stream.count() > 0,
stream.count() >= 1,
stream.findFirst().isPresent());
return stream.findFirst().isPresent();
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ ImmutableSet<Optional<Integer>> testStreamMapFirst() {
Stream.of("bar").map(String::length).findFirst());
}

ImmutableSet<Boolean> testStreamIsEmpty() {
ImmutableSet<Boolean> testStreamFindAnyIsEmpty() {
return ImmutableSet.of(
Stream.of(1).count() == 0,
Stream.of(2).count() <= 0,
Expand All @@ -131,15 +131,14 @@ ImmutableSet<Boolean> testStreamIsEmpty() {
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)));
.collect(collectingAndThen(toImmutableMap(k -> k, v -> v), ImmutableMap::isEmpty)),
Stream.of(10).count() != 0,
Stream.of(11).count() > 0,
Stream.of(12).count() >= 1);
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
return ImmutableSet.of(
Stream.of(1).count() != 0,
Stream.of(2).count() > 0,
Stream.of(3).count() >= 1,
Stream.of(4).findFirst().isPresent());
boolean testStreamFindAnyIsPresent() {
return Stream.of(1).findFirst().isPresent();
}

ImmutableSet<Optional<String>> testStreamMin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ ImmutableSet<Optional<Integer>> testStreamMapFirst() {
Stream.of("bar").findFirst().map(String::length));
}

ImmutableSet<Boolean> testStreamIsEmpty() {
ImmutableSet<Boolean> testStreamFindAnyIsEmpty() {
return ImmutableSet.of(
Stream.of(1).findAny().isEmpty(),
Stream.of(2).findAny().isEmpty(),
Expand All @@ -131,15 +131,14 @@ ImmutableSet<Boolean> testStreamIsEmpty() {
Stream.of(6).findAny().isEmpty(),
Stream.of(7).findAny().isEmpty(),
Stream.of(8).findAny().isEmpty(),
Stream.of(9).findAny().isEmpty());
Stream.of(9).findAny().isEmpty(),
!Stream.of(10).findAny().isEmpty(),
!Stream.of(11).findAny().isEmpty(),
!Stream.of(12).findAny().isEmpty());
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
return ImmutableSet.of(
Stream.of(1).findAny().isPresent(),
Stream.of(2).findAny().isPresent(),
Stream.of(3).findAny().isPresent(),
Stream.of(4).findAny().isPresent());
boolean testStreamFindAnyIsPresent() {
return Stream.of(1).findAny().isPresent();
}

ImmutableSet<Optional<String>> testStreamMin() {
Expand Down

0 comments on commit 4cbc5a0

Please sign in to comment.