diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java index 31693ef8ff..f16d2d7f5f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java @@ -17,6 +17,7 @@ import java.time.chrono.ChronoLocalDate; import java.time.chrono.ChronoLocalDateTime; import java.time.chrono.ChronoZonedDateTime; +import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; /** Refaster templates related to expressions dealing with time. */ @@ -310,8 +311,86 @@ Duration after() { } } + /** Prefer {@link Duration#ofDays(long)} over alternative representations. */ + static final class DurationOfDays { + @BeforeTemplate + Duration before(long amount) { + return Duration.of(amount, ChronoUnit.DAYS); + } + + @AfterTemplate + Duration after(long amount) { + return Duration.ofDays(amount); + } + } + + /** Prefer {@link Duration#ofHours(long)} over alternative representations. */ + static final class DurationOfHours { + @BeforeTemplate + Duration before(long amount) { + return Duration.of(amount, ChronoUnit.HOURS); + } + + @AfterTemplate + Duration after(long amount) { + return Duration.ofHours(amount); + } + } + + /** Prefer {@link Duration#ofMillis(long)} over alternative representations. */ + static final class DurationOfMillis { + @BeforeTemplate + Duration before(long amount) { + return Duration.of(amount, ChronoUnit.MILLIS); + } + + @AfterTemplate + Duration after(long amount) { + return Duration.ofMillis(amount); + } + } + + /** Prefer {@link Duration#ofMinutes(long)} over alternative representations. */ + static final class DurationOfMinutes { + @BeforeTemplate + Duration before(long amount) { + return Duration.of(amount, ChronoUnit.MINUTES); + } + + @AfterTemplate + Duration after(long amount) { + return Duration.ofMinutes(amount); + } + } + + /** Prefer {@link Duration#ofNanos(long)} over alternative representations. */ + static final class DurationOfNanos { + @BeforeTemplate + Duration before(long amount) { + return Duration.of(amount, ChronoUnit.NANOS); + } + + @AfterTemplate + Duration after(long amount) { + return Duration.ofNanos(amount); + } + } + + /** Prefer {@link Duration#ofSeconds(long)} over alternative representations. */ + static final class DurationOfSeconds { + @BeforeTemplate + Duration before(long amount) { + return Duration.of(amount, ChronoUnit.SECONDS); + } + + @AfterTemplate + Duration after(long amount) { + return Duration.ofSeconds(amount); + } + } + /** - * Don't unnecessarily convert two and from milliseconds. (This way nanosecond precision is + * Don't unnecessarily convert to and from milliseconds. (This way nanosecond precision is * retained.) * *

Warning: this rewrite rule increases precision! @@ -329,7 +408,7 @@ Duration after(Instant a, Instant b) { } /** - * Don't unnecessarily convert two and from milliseconds. (This way nanosecond precision is + * Don't unnecessarily convert to and from milliseconds. (This way nanosecond precision is * retained.) * *

Warning: this rewrite rule increases precision! diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java index 84394c9d44..2d3910de2a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java @@ -128,6 +128,30 @@ ImmutableSet testZeroDuration() { Duration.of(0, ChronoUnit.MILLIS)); } + Duration testDurationOfDays() { + return Duration.of(1, ChronoUnit.DAYS); + } + + Duration testDurationOfHours() { + return Duration.of(1, ChronoUnit.HOURS); + } + + Duration testDurationOfMillis() { + return Duration.of(1, ChronoUnit.MILLIS); + } + + Duration testDurationOfMinutes() { + return Duration.of(1, ChronoUnit.MINUTES); + } + + Duration testDurationOfNanos() { + return Duration.of(1, ChronoUnit.NANOS); + } + + Duration testDurationOfSeconds() { + return Duration.of(1, ChronoUnit.SECONDS); + } + Duration testDurationBetweenInstants() { return Duration.ofMillis(Instant.MAX.toEpochMilli() - Instant.MIN.toEpochMilli()); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java index b514b0f358..681971a176 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java @@ -117,6 +117,30 @@ ImmutableSet testZeroDuration() { Duration.ZERO); } + Duration testDurationOfDays() { + return Duration.ofDays(1); + } + + Duration testDurationOfHours() { + return Duration.ofHours(1); + } + + Duration testDurationOfMillis() { + return Duration.ofMillis(1); + } + + Duration testDurationOfMinutes() { + return Duration.ofMinutes(1); + } + + Duration testDurationOfNanos() { + return Duration.ofNanos(1); + } + + Duration testDurationOfSeconds() { + return Duration.ofSeconds(1); + } + Duration testDurationBetweenInstants() { return Duration.between(Instant.MIN, Instant.MAX); }