On Fri, 24 Oct 2025 17:38:58 GMT, Volkan Yazici <[email protected]> wrote:
> > We might soon have saturating addition functionality in
> > `java.time.Instant`; see: #27549
>
> Great tip! 💯 I will hold this PR until #27549 gets merged, and use
> `Instant::plusSaturated` in `Deadline::plus*` and `::minus` methods.
One problem for this PR is that the proposed `Instant` functionality in that PR
will only work with `Duration` not `TemporalAmount`. Another problem is that
you cannot implement saturating subtraction based on saturating addition here.
If you are thinking along these lines, then it will fail if `amountToSubtract`
is the minimum value for `Duration`:
deadline.plus(amountToSubtract.negated())
Now, I understand that in your case you will never have negative duration, let
alone such extremely negative one. But it would still be good to be robust,
especially if it also involves less code.
`Deadline.minus` seems to be used twice. Both times it is used for a comparison
like this:
t1 - dt < t0
To avoid subtraction, rearrange the terms. Different rearrangements enable
different options, but either option is fine:
* t1 - t0 < dt (compare durations using Instant.until/Duration.between)
* t1 < t0 + dt (compare instants using future Instant.plusSaturating)
> > I note that `jdk.internal.net.http.common.Deadline` also wants to have
> > saturating subtraction, and I wonder if that's really needed. It seems that
> > the two usages of the `minus` method in the codebase can be reimplemented
> > alternatively. In which case `Deadline` could delete `minus`.
>
> I also have my reservations regarding the rich, yet seldom used API surface
> of `Deadline`. But revamping it is out of the scope of this work.
>
> > Furthermore, if there's no need for saturating subtraction, do we need the
> > `Deadline` class? What does it provide, that `Instant` does not?
>
> In short, `Instant` is not necessarily generated using a
> monotonically-increasing `InstantSource`. `Deadline` is introduced to avoid
> that ambiguity and guaranteed to be always monotonically-increasing. See
> [this conversation for
> details](https://github.com/openjdk/jdk/pull/14450#pullrequestreview-1479500686).
Okay, so you want your source of ticks to be exclusive and monotonic, neither
of which could be guaranteed without introducing a few specialised types. Got
it.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/27973#issuecomment-3444417312