RFR: 8368856: Add methods for saturating Duration arithmetic to Instant [v7]

Pavel Rappo prappo at openjdk.org
Thu Oct 9 19:37:21 UTC 2025


On Thu, 9 Oct 2025 14:15:23 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

> As specified and implemented, `Duration.plus(long amount, TemporalUnit)` does not make an exception for amount == 0. For any other value of month or years, the value is an estimate and the exception is thrown. It might be useful to consider a change (as a separate enhancement)

I'm not sure that I completely understand what you are saying. Are you saying that a zero of any unit is still zero and, if added, could result in the initial instant rather than throw an exception if the unit is "incompatible" with `Instant`?

If so, then it reminds me of the following problem: should an unmodifiable set allow to delete an element which it does not contain? Different APIs decide differently. For example, this throws an exception:

    Set.of().remove(1)

Whereas this doesn't:

    Collections.emptySet().remove(1)

I don't know java.time deeply enough to lean one way or the other. But my gut feeling tells me that unconditional exception is easier to reason about and is more reliable.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/27549#discussion_r2417776606


More information about the core-libs-dev mailing list