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

Add utilities related to java.time.Duration #1221

Closed
sleberknight opened this issue Dec 21, 2024 Discussed in #1217 · 0 comments · Fixed by #1223
Closed

Add utilities related to java.time.Duration #1221

sleberknight opened this issue Dec 21, 2024 Discussed in #1217 · 0 comments · Fixed by #1223
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

Because I've recently been dealing with Duration a lot, and having to check whether positive or positive or zero, I created a discussion ( #1217 ) on whether to create our own KiwiDurations utility. Admittedly, it would contain some pretty trivial methods. But after writing:

if (!duration.isNegative() && !duration.isZero()) { ... }

and even:

if (!duration.isNegative()) { ... }

several times, I wanted this instead:

if (duration.isPositive()) { ... }

and

if (duration.isPositiveOrZero()) { ... }

Why? First, having to write a compound boolean expression that negates each individual expression is annoying, especially when you do it many times (or even twice). Second, compound expressions are more difficult to read when skimming code, especially when each one has a ! in front of it (it's easy to miss them).

Since this library isn't Kotlin or a language supporting extension methods, I clearly can't modify the JDK's Duration class. So, I guess we'll just have to do with:

if (KiwiDurations.isPositive(duration)) { ... }

and

if (KiwiDurations.isPositiveOrZero(duration)) { ... }

With static imports it's nicer - just isPositive(duration).

While we're at it, might as well support the other missing option:

if (KiwiDurations.isNegativeOrZero(duration)) { ... }

So, that's three simple, but useful and easier to read in my opinion, methods:

  • isPositive(Duration)
  • isPositiveOrZero(Duration)
  • isNegativeOrZero(Duration)
@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Dec 21, 2024
@sleberknight sleberknight added this to the 4.6.0 milestone Dec 21, 2024
@sleberknight sleberknight self-assigned this Dec 21, 2024
sleberknight added a commit that referenced this issue Dec 24, 2024
Java's Duration contains (instance) methods isNegative() and
isZero(), but nothing else. Add convenience methods that allow
for one-liner checks of Duration instances whether they are
positive, positive or zero, or negative or zero.

The initial KiwiDurations contains the following methods:

* isPositive
* isPositiveOrZero
* isNegativeOrZero

Closes #1221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant