Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plus/minus methods as aliases for add/subtract transformations #10

Closed
jamesdh opened this issue Nov 16, 2023 · 4 comments
Closed

Add plus/minus methods as aliases for add/subtract transformations #10

jamesdh opened this issue Nov 16, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@jamesdh
Copy link

jamesdh commented Nov 16, 2023

When using this library with Groovy, including plus() and minus() methods would allow one to more succinctly perform transformations, e.g.

Distance d1 = Distance.ofMeters(10)
Distance d2 = Distance.ofMeters(100)
assert d1 + d2 == Distance.ofMeters(110)
// or even the following, which would default to using the unit of measurement that d1 was created with
assert d1 + 10 == Distance.ofMeters(20)

Here's a blog post with a bit more info and also the official docs

@pjazdzyk
Copy link
Owner

Hi James, thanks. I will check this article, and I will see to what extend this can be incorporated into the code.

@pjazdzyk pjazdzyk added the enhancement New feature or request label Nov 19, 2023
@pjazdzyk pjazdzyk self-assigned this Nov 19, 2023
@pjazdzyk
Copy link
Owner

pjazdzyk commented Nov 19, 2023

@jamesdh Feature is implemented and it will be included in the next planned release. You can also test it now, using code from the branch:
https://github.com/pjazdzyk/unitility/tree/feature/SNSUNI-46_add_frameworks_support
The next release will be a major change, project will be now multimodule and will include framework support for easier use in web applications.
In addition to plus/minus I have also refactored divide method, to allow similar functionality. I have done some simple tests with Groovy and it seems to work fine:

        // Temperature examples
        def t1 = Temperature.ofCelsius(20)
        def t2 = Temperature.ofCelsius(10)
        def t3 = Temperature.ofKelvins(303.15) // =30 oC

        // Adding & subtracting: The same unit
        def t4 = t1 + t2
        def t5 = t1 - t2
        def t6 = t1 + 15.5;
        println t4 // Temperature{30.0°C}
        println t5 // Temperature{10.0°C}
        println t6 // Temperature{35.5°C}

        // Adding & subtracting: Different units of the same quantity, resolving to unit type of the first addend.
        def t7 = t1 + t3
        def t8 = t1 - t3
        println t7 // Temperature{50.0°C}
        println t8 // Temperature{-10.0°C}

        // Multiply
        def t9 = t1 * t2
        def t10 = t1 * 2
        println t9 // will resolve to double = 200.0
        println t10 // Temperature{40.0°C}

        // Divide
        def t11 = t1 / t2
        def t12 = t1 / 2
        println t11 // will resolve to double = 2.0
        println t12 // Temperature{10.0°C}

For multiply and divide - on attempt to divide or multiply by other quantity it resolves to double. This is because that kind of operations changes the Unit type and at this point there is no resolver implemented to determine, that for i.e: division of Newton over Area in m2 should be resolved to Pressure in Pascals.

@jamesdh
Copy link
Author

jamesdh commented Nov 21, 2023

You are awesome. Thank you for this! Love the project!

@pjazdzyk
Copy link
Owner

pjazdzyk commented Dec 10, 2023

I am very happy you like my project!
New version - 2.0.0 has been released (it is not backward compatible, unfortunately). Changes from this issue are included in the new version - I hope it will work fine.
Spatial types I have plan to implement bit later, but I will do that.
Thanks for your input! :)

Feature added in release: v2.0.0 - Frameworks support: Spring and Quarkus

@pjazdzyk pjazdzyk pinned this issue Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants