I would like to propose a new TimestampGenerator
functional interface be added as a core utility. I feel this would be a great parallel to the existing IdGenerator
functional interface that already exists. The contract would look as follows:
@FunctionalInterface
public interface TimestampGenerator {
long generateTimestamp();
}
For components that need to generate timestamps, a bean could easily be declared as follows:
@Configuration
class TimestampConfiguration {
@Bean
TimestampGenerator timestampGenerator() {
return System::currentTimeMillis;
}
}
This comes in handy when mocking dependencies in tests as relying on static methods for generating timestamps makes assertions difficult. In many of my projects I end up defining a similar interface so I would love to see it become part of the core framework.
A simple proof of concept can be seen here.
Comment From: erichaagdev
I have never contributed to the Spring project before. If the feature is deemed worthy I would love to pick this up.
Comment From: jnizet
Isn't this what the standard java.time.Clock
class has been designed for?
Comment From: sbrannen
@gorlah, thanks for the proposal.
Isn't this what the standard
java.time.Clock
class has been designed for?
Indeed, we recommend the use of java.time.Clock
for such use cases.
See https://github.com/spring-projects/spring-framework/issues/24884#issuecomment-612887209 and https://github.com/spring-projects/spring-framework/issues/25782#issuecomment-694722314 for related discussions.
In light of that, I am closing this issue.