From 36b5c8070036870388c86b8695562a43c695a4d8 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 3 Nov 2022 19:08:22 +0100 Subject: [PATCH] Suggestions --- .../errorprone/refasterrules/TimeRules.java | 30 +++++++++++++++---- .../refasterrules/TimeRulesTestInput.java | 19 +++++++----- .../refasterrules/TimeRulesTestOutput.java | 17 +++++++---- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java index e9f07afba55..35a4b36457e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TimeRules.java @@ -66,12 +66,13 @@ ZoneOffset after() { } } + /** Prefer {@link LocalDate#ofInstant(Instant, ZoneId)} over more indirect alternatives. */ static final class LocalDateOfInstant { @BeforeTemplate LocalDate before(Instant instant, ZoneId zoneId) { return Refaster.anyOf( instant.atZone(zoneId).toLocalDate(), - instant.atZone(zoneId).toOffsetDateTime().toLocalDate(), + OffsetDateTime.ofInstant(instant, zoneId).toLocalDate(), LocalDateTime.ofInstant(instant, zoneId).toLocalDate()); } @@ -86,12 +87,13 @@ LocalDate after(Instant instant, ZoneId zoneId) { } } + /** Prefer {@link LocalDateTime#ofInstant(Instant, ZoneId)} over more indirect alternatives. */ static final class LocalDateTimeOfInstant { @BeforeTemplate LocalDateTime before(Instant instant, ZoneId zoneId) { return Refaster.anyOf( instant.atZone(zoneId).toLocalDateTime(), - instant.atZone(zoneId).toOffsetDateTime().toLocalDateTime()); + OffsetDateTime.ofInstant(instant, zoneId).toLocalDateTime()); } @BeforeTemplate @@ -105,12 +107,14 @@ LocalDateTime after(Instant instant, ZoneId zoneId) { } } + /** Prefer {@link LocalTime#ofInstant(Instant, ZoneId)} over more indirect alternatives. */ static final class LocalTimeOfInstant { @BeforeTemplate LocalTime before(Instant instant, ZoneId zoneId) { return Refaster.anyOf( instant.atZone(zoneId).toLocalTime(), - instant.atZone(zoneId).toOffsetDateTime().toLocalTime(), + OffsetDateTime.ofInstant(instant, zoneId).toLocalTime(), + OffsetTime.ofInstant(instant, zoneId).toLocalTime(), LocalDateTime.ofInstant(instant, zoneId).toLocalTime()); } @@ -125,7 +129,20 @@ LocalTime after(Instant instant, ZoneId zoneId) { } } - /** Prefer {@link Instant#atOffset(ZoneOffset)} over the more verbose alternative. */ + /** Prefer {@link OffsetDateTime#ofInstant(Instant, ZoneId)} over more indirect alternatives. */ + static final class OffsetDateTimeOfInstant { + @BeforeTemplate + OffsetDateTime before(Instant instant, ZoneId zoneId) { + return instant.atZone(zoneId).toOffsetDateTime(); + } + + @AfterTemplate + OffsetDateTime after(Instant instant, ZoneId zoneId) { + return OffsetDateTime.ofInstant(instant, zoneId); + } + } + + /** Prefer {@link Instant#atOffset(ZoneOffset)} over more verbose alternatives. */ static final class InstantAtOffset { @BeforeTemplate OffsetDateTime before(Instant instant, ZoneOffset zoneOffset) { @@ -138,10 +155,11 @@ OffsetDateTime after(Instant instant, ZoneOffset zoneOffset) { } } + /** Prefer {@link OffsetTime#ofInstant(Instant, ZoneId)} over more indirect alternatives. */ static final class OffsetTimeOfInstant { @BeforeTemplate OffsetTime before(Instant instant, ZoneId zoneId) { - return instant.atZone(zoneId).toOffsetDateTime().toOffsetTime(); + return OffsetDateTime.ofInstant(instant, zoneId).toOffsetTime(); } @BeforeTemplate @@ -155,7 +173,7 @@ OffsetTime after(Instant instant, ZoneId zoneId) { } } - /** Prefer {@link Instant#atZone(ZoneId)} over the more verbose alternative. */ + /** Prefer {@link Instant#atZone(ZoneId)} over more verbose alternatives. */ static final class InstantAtZone { @BeforeTemplate ZonedDateTime before(Instant instant, ZoneId zoneId) { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java index f5caaeeab5d..9c1b291dfe9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestInput.java @@ -39,24 +39,29 @@ ImmutableSet testUtcConstant() { ImmutableSet testLocalDateOfInstant() { return ImmutableSet.of( Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toLocalDate(), - Instant.EPOCH.atZone(ZoneId.of("Europe/Paris")).toOffsetDateTime().toLocalDate(), Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDate(), - LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")).toLocalDate()); + OffsetDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")).toLocalDate(), + LocalDateTime.ofInstant(Instant.EPOCH, ZoneOffset.MIN).toLocalDate()); } ImmutableSet testLocalDateTimeOfInstant() { return ImmutableSet.of( Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toLocalDateTime(), - Instant.EPOCH.atZone(ZoneId.of("Europe/Berlin")).toOffsetDateTime().toLocalDateTime(), - Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDateTime()); + Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalDateTime(), + OffsetDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")).toLocalDateTime()); } ImmutableSet testLocalTimeOfInstant() { return ImmutableSet.of( Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toLocalTime(), - Instant.EPOCH.atZone(ZoneId.of("Europe/Paris")).toOffsetDateTime().toLocalTime(), Instant.EPOCH.atOffset(ZoneOffset.UTC).toLocalTime(), - LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")).toLocalTime()); + OffsetDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")).toLocalTime(), + OffsetTime.ofInstant(Instant.EPOCH, ZoneOffset.MIN).toLocalTime(), + LocalDateTime.ofInstant(Instant.EPOCH, ZoneOffset.MAX).toLocalTime()); + } + + OffsetDateTime testOffsetDateTimeOfInstant() { + return Instant.EPOCH.atZone(ZoneOffset.UTC).toOffsetDateTime(); } OffsetDateTime testInstantAtOffset() { @@ -65,7 +70,7 @@ OffsetDateTime testInstantAtOffset() { ImmutableSet testOffsetTimeOfInstant() { return ImmutableSet.of( - Instant.EPOCH.atZone(ZoneId.of("Europe/Amsterdam")).toOffsetDateTime().toOffsetTime(), + OffsetDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")).toOffsetTime(), Instant.EPOCH.atOffset(ZoneOffset.UTC).toOffsetTime()); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java index ba0e4908cf6..1f02b4f622d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/TimeRulesTestOutput.java @@ -39,24 +39,29 @@ ImmutableSet testUtcConstant() { ImmutableSet testLocalDateOfInstant() { return ImmutableSet.of( LocalDate.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")), - LocalDate.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Paris")), LocalDate.ofInstant(Instant.EPOCH, ZoneOffset.UTC), - LocalDate.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin"))); + LocalDate.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")), + LocalDate.ofInstant(Instant.EPOCH, ZoneOffset.MIN)); } ImmutableSet testLocalDateTimeOfInstant() { return ImmutableSet.of( LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")), - LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")), - LocalDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC)); + LocalDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC), + LocalDateTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin"))); } ImmutableSet testLocalTimeOfInstant() { return ImmutableSet.of( LocalTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Amsterdam")), - LocalTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Paris")), LocalTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC), - LocalTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin"))); + LocalTime.ofInstant(Instant.EPOCH, ZoneId.of("Europe/Berlin")), + LocalTime.ofInstant(Instant.EPOCH, ZoneOffset.MIN), + LocalTime.ofInstant(Instant.EPOCH, ZoneOffset.MAX)); + } + + OffsetDateTime testOffsetDateTimeOfInstant() { + return OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC); } OffsetDateTime testInstantAtOffset() {