-
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
TimeTemplates
: Introduce Duration.ofX
templates
#41
Changes from 4 commits
af9fb6b
4420491
0b62344
27813a8
9330174
bf8458f
95512ac
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static java.time.temporal.ChronoUnit.DAYS; | ||
Badbond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.time.Clock; | ||
import java.time.Duration; | ||
|
@@ -17,7 +19,7 @@ | |
final class TimeTemplatesTest implements RefasterTemplateTestCase { | ||
@Override | ||
public ImmutableSet<?> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of(ChronoUnit.class); | ||
return ImmutableSet.of(ChronoUnit.class, DAYS); | ||
} | ||
|
||
Instant testClockInstant() { | ||
|
@@ -128,6 +130,34 @@ ImmutableSet<Duration> testZeroDuration() { | |
Duration.of(0, ChronoUnit.MILLIS)); | ||
} | ||
|
||
Duration testDurationOfNanos() { | ||
return Duration.of(1, ChronoUnit.NANOS); | ||
} | ||
|
||
Duration testDurationOfMillis() { | ||
return Duration.of(1, ChronoUnit.MILLIS); | ||
} | ||
|
||
Duration testDurationOfSeconds() { | ||
return Duration.of(1, ChronoUnit.SECONDS); | ||
} | ||
|
||
Duration testDurationOfMinutes() { | ||
return Duration.of(1, ChronoUnit.MINUTES); | ||
} | ||
|
||
Duration testDurationOfHours() { | ||
return Duration.of(1, ChronoUnit.HOURS); | ||
} | ||
|
||
Duration testDurationOfDays() { | ||
return Duration.of(1, ChronoUnit.DAYS); | ||
} | ||
|
||
Duration testDurationOfDaysStaticImport() { | ||
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. As you already pointed out in the PR description, indeed we do not need the static import case. Refaster is smart enough to also match this, so we don't need to have a test for this 😄. 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. Useful to know: if we want to have multiple test cases for one Refaster template, we generally wrap these cases in an Within this class there are other methods that do this. See for example: As a result the tests are more concise and it is easier to see how a Refaster template is tested. 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. Regarding the static import test, this is now removed as suggested. Therefore, also no grouping required. |
||
return Duration.of(1, DAYS); | ||
} | ||
|
||
Duration testDurationBetweenInstants() { | ||
return Duration.ofMillis(Instant.MAX.toEpochMilli() - Instant.MIN.toEpochMilli()); | ||
} | ||
|
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.
Almost every template in this class has a Javadoc, let's add it to these templates as well :).
Perhaps something along the lines of (just a suggestion):
Prefer ... over more contrived alternatives.
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.
The templates are now based on the metric precision, which makes perfect sense. When looking at the implementation(s) of the
Duration.of{...}(amount);
methods, they are ordered lexicographically.Based on that + knowing that @Stephan202 is a fan of lexicographical ordering, we could change the sorting to be lexicographical as well 😄.
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.
Yeah couldn't get a nice description for docs. Also not sure if I'd agree with the term 'contrived'. Based on the
LocalTimeMin
template I went with more basic:/** Prefer {@link Duration#ofNanos(long)} over alternative representations. */
(and similar). Open to suggestions.Regarding ordering, the implementation is not entirely lexicographically ordered 😄
ofSeconds
is not in the correct place andofMillis
should be beforeisMinutes
. AlsoTimeTemplateTestInput#testZeroDuration()
orders in time magnitude.But I don't have a strong preference for order (go Rebel Alliance!) so ordered them lexicographical now instead of in order of time magnitude (insert joke about Yoda time here) as you prefer.
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.
For contrived I think this definition fits the context better:
created or arranged in a way that seems artificial and unrealistic.
.Nevertheless, good suggestion, SGTM!
Other suggestion would be "more verbose examples/representation" as we now don't need
ChronoUnit
😄.Did only check the first four methods, which was not enough apparently 😄.