Skip to content
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

Merged
merged 7 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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.)
*
* <p><strong>Warning:</strong> this rewrite rule increases precision!
Expand All @@ -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.)
*
* <p><strong>Warning:</strong> this rewrite rule increases precision!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ ImmutableSet<Duration> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ ImmutableSet<Duration> 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);
}

Comment on lines +120 to +143
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, I'd have expected this to be sorted by time units (days, hours, minutes, seconds, millis, nanos), but this works for me as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See discussion here. I'm open to both 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this works for me as well.

Will keep true to my words here. 🙂

Duration testDurationBetweenInstants() {
return Duration.between(Instant.MIN, Instant.MAX);
}
Expand Down