unitsofmeasurement / indriya

JSR 385 - Reference Implementation
Other
115 stars 40 forks source link

Can't create a RationalConverter #374

Closed SebastienDorgan closed 1 year ago

SebastienDorgan commented 2 years ago

Hi, I use version 2.1.3 for indriya. I would like to create a DECIBEL unit as documented in the transform method of the Unit class. Unit DECIBEL = Unit.ONE.transform( new LogConverter(10).inverse().concatenate( new RationalConverter(1, 10))); Unfortunately I do not found a public constructor or a static 'of' method that would allow me to to create a RationalConverter. Can you help me?

SebastienDorgan commented 2 years ago

I found that I can use the MultiplyConverter class reading https://schneide.blog/tag/indriya/. Maybe the documentation of the Unit transform method should be updated

andi-huber commented 2 years ago

Thanks for the blog link!

I'm sure you figured this out already, what does work with Indriya:

import tech.units.indriya.AbstractUnit;
import tech.units.indriya.function.LogConverter;
import tech.units.indriya.function.MultiplyConverter;
//...
Unit<Dimensionless> DECIBEL = AbstractUnit.ONE.transform(
        new LogConverter(10)
        .inverse()
        .concatenate(
            MultiplyConverter.ofRational(1, 10)));

All 3 imported classes are specific to Indriya (the reference implementation). In other words the example above is specific to Indriya. Perhaps updating the javadoc in unit-api for javax.measure.Unit.transform(UnitConverter) with a hint that the provided example there is just to make a point but depends on the actual implementation one is using would help. Though not sure whether worth the effort.

SebastienDorgan commented 2 years ago

Thank you for your answer

keilw commented 1 year ago

@SebastienDorgan did the answer satisfy you and we may close this question?

keilw commented 1 year ago

Closing this based on the remark of May 30.