-
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
Introduce OptionalIdentity
Refaster template
#245
Changes from 2 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 |
---|---|---|
|
@@ -332,6 +332,22 @@ Optional<T> after(Optional<T> optional1, Optional<T> optional2) { | |
} | ||
} | ||
|
||
/** | ||
* Don't use the redundant chain of {@link Optional#stream()} followed by either {@link | ||
* Stream#findFirst()} or {@link Stream#findAny()}. | ||
*/ | ||
abstract static class OptionalSkipStreamFindFirst<T> { | ||
svavahb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@BeforeTemplate | ||
Optional<T> before(Optional<T> optional) { | ||
return Refaster.anyOf(optional.stream().findFirst(), optional.stream().findAny()); | ||
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. Perhaps we could also add 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. Oh, good one! Sure; wanna add a proposal? 😄 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, will do! 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. I'm having a small issue: The test keeps failing because the
I can of course try to fix it by using something that doesn't require an import as the comparator, however it feels strange that it would fail on the imports? Shouldn't the import get removed from the input when the refaster template is applied in the error prone compilation? 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. In general it's hard to know whether an import can be removed, because other code may still depend on it (and rules are applied independently). So in practice we instead rely on Google Java Format to drop unused imports after the fact. This does impose a challenge for the tests, as you have noticed. For this purpose we have that funny (This does raise another point: we should have a rule that favours 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. Nice, thanks for the explanation! 😄 |
||
} | ||
|
||
@AfterTemplate | ||
Optional<T> after(Optional<T> optional) { | ||
return optional; | ||
} | ||
} | ||
|
||
// XXX: Add a rule for: | ||
// `optional.flatMap(x -> pred(x) ? Optional.empty() : Optional.of(x))` and variants. | ||
// (Maybe canonicalize the inner expression. Maybe we rewrite already.) | ||
|
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.
@rickie interesting case w.r.t. automated naming: we wouldn't be able to use
Optional
here. I'll go withOptionalIdentity
for now.