-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend StreamRules
Refaster rule collection
#605
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import com.google.errorprone.refaster.annotation.Placeholder; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Comparator; | ||
import java.util.DoubleSummaryStatistics; | ||
import java.util.IntSummaryStatistics; | ||
|
@@ -245,14 +246,18 @@ Optional<S> after(Stream<T> stream, Function<? super T, S> function) { | |
} | ||
|
||
/** In order to test whether a stream has any element, simply try to find one. */ | ||
// 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> { | ||
@BeforeTemplate | ||
boolean before(Stream<T> stream) { | ||
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends Collection<?>> collector) { | ||
return Refaster.anyOf( | ||
stream.count() == 0, | ||
stream.count() <= 0, | ||
stream.count() < 1, | ||
stream.findFirst().isEmpty()); | ||
stream.findFirst().isEmpty(), | ||
stream.collect(collector).isEmpty()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. couldn't you theoretically use a filtering collector though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! We should add a comment calling out this caveat. I'll add a commit later this morning 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added two more comments. Since this kind of is rare (and since we rewrite top-level |
||
} | ||
|
||
@AfterTemplate | ||
|
@@ -347,6 +352,14 @@ boolean before(Stream<T> stream, Predicate<? super T> predicate) { | |
stream.filter(predicate).findAny().isEmpty()); | ||
} | ||
|
||
@BeforeTemplate | ||
boolean before2( | ||
Stream<T> stream, | ||
@Matches(IsLambdaExpressionOrMethodReference.class) | ||
Function<? super T, Boolean> predicate) { | ||
return stream.map(predicate).noneMatch(Refaster.anyOf(Boolean::booleanValue, b -> b)); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(Stream<T> stream, Predicate<? super T> predicate) { | ||
return stream.noneMatch(predicate); | ||
|
@@ -377,6 +390,14 @@ boolean before(Stream<T> stream, Predicate<? super T> predicate) { | |
!stream.noneMatch(predicate), stream.filter(predicate).findAny().isPresent()); | ||
} | ||
|
||
@BeforeTemplate | ||
boolean before2( | ||
Stream<T> stream, | ||
@Matches(IsLambdaExpressionOrMethodReference.class) | ||
Function<? super T, Boolean> predicate) { | ||
return stream.map(predicate).anyMatch(Refaster.anyOf(Boolean::booleanValue, b -> b)); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(Stream<T> stream, Predicate<? super T> predicate) { | ||
return stream.anyMatch(predicate); | ||
|
@@ -389,6 +410,14 @@ boolean before(Stream<T> stream, Predicate<? super T> predicate) { | |
return stream.noneMatch(Refaster.anyOf(not(predicate), predicate.negate())); | ||
} | ||
|
||
@BeforeTemplate | ||
boolean before2( | ||
Stream<T> stream, | ||
@Matches(IsLambdaExpressionOrMethodReference.class) | ||
Function<? super T, Boolean> predicate) { | ||
return stream.map(predicate).allMatch(Refaster.anyOf(Boolean::booleanValue, b -> b)); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(Stream<T> stream, Predicate<? super T> predicate) { | ||
return stream.allMatch(predicate); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it nicer to have a separate before template for this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinion. (Something we could enforce one way or another with automation...)