spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.39k stars 38.06k forks source link

Enhance DataSize with comparators and simple algebra #27583

Closed nhmarujo closed 2 years ago

nhmarujo commented 2 years ago

I think it would be useful to add some handy operators to DataSize, like comparators and some algebra:

These comparators are far more readable than using the .compareTo (Similar to Java's approach with Instant - isBefore and isAfter). Also being able to do some simple algebra will be very handy for when you are doing calculations with sizes (dataSize1.plus(dataSize2) is far more readable than doing DataSize.ofBytes(dataSize1.toBytes() + dataSize2.toBytes()).

sbrannen commented 2 years ago

Hi @nhmarujo,

Thanks for the proposal (and your first issue) for the Spring Framework.

At this time, the team does not wish to add to the public API of DataSize since such additions are not needed within the framework itself.

In light of that, I am closing this issue.

nhmarujo commented 2 years ago

Hi @sbrannen . I believe many people using Spring are using DataSize which, for instance, is very handy to use in properties. I get that it is not used internally by the framework, but if it is meant to be used by people using the framework and if this would be handy for them, isn't that a strong enough reason to add it?

If possible, I would even be willing to give it a try producing a PR for this 😄

sbrannen commented 2 years ago

As I mentioned, the team decision was not to implement additional features in DataSize at this time.

However, if the community expresses great interest in such features over time, we may choose to revisit this topic.

sbrannen commented 2 years ago

I think it would be useful to add some handy operators to DataSize

For the sake of clarity, can you please expound on any concrete use case you have where you need such functionality?

nhmarujo commented 2 years ago

Hi @sbrannen . Thank you for your reply.

I can provide a concrete use case we had recently - we were creating a way to automatically calibrate argon2 parameters. For that we had to try to iteratively find the best memory setting, which involved starting at some given value, do some runs and then calculate next step until some criteria was met. This simple example involved:

I can, however, think of more use cases:

Of course you can do any of these without the operators I was suggesting, but I reckon they would make the code far easier to implement and much more readable.

Best regards