RFR: 8266846: Add java.time.InstantSource

Michael Hixson github.com+297150+michaelhixson at openjdk.java.net
Sat May 15 08:27:10 UTC 2021


On Thu, 13 May 2021 20:52:33 GMT, Stephen Colebourne <scolebourne at openjdk.org> wrote:

> 8266846: Add java.time.InstantSource

src/java.base/share/classes/java/time/InstantSource.java line 68:

> 66:  * @implSpec
> 67:  * This interface must be implemented with care to ensure other classes operate correctly.
> 68:  * All implementations that can be instantiated must be final, immutable and thread-safe.

(I'm not an openjdk reviewer)

I'm wondering if we can revisit this "immutable" requirement as it is being shuffled from `Clock` to `InstantSource`.

This precludes an implementation that might be useful for testing, where the time provided by a single `InstantSource` instance can be adjusted manually in the test code.  To me that is a notable downside (worth breaking the contract for), and it's not so clear what the upside is.  "Immutable" seems like an odd way to describe the system clock anyway.

Do you know if the people who already use their own `InstantSource` / `TimeSource` / `Supplier<Instant>` have a mutable version that they use in tests?  I would be surprised if they're all satisfied with `InstantSource.fixed`, but perhaps my intuition is wrong here.

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

PR: https://git.openjdk.java.net/jdk/pull/4016


More information about the core-libs-dev mailing list