Skip to content

extract the Dimension class from a given unit or Quantity #438

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

Open
3Fish opened this issue Mar 21, 2025 · 4 comments
Open

extract the Dimension class from a given unit or Quantity #438

3Fish opened this issue Mar 21, 2025 · 4 comments
Labels

Comments

@3Fish
Copy link

3Fish commented Mar 21, 2025

Hi,

I use Idriya in the context of a compiler, where I need to define the actuall Dimension type.
Therefore I need to somehow get the actual Dimension class for a parsed Unit or Quantity.
eg.

SimpleUnitFormat format = SimpleUnitFormat.getInstance();
Unit<?> unit = format.parse("V");
Class<?> clazz = unit.getDimensionClass(); 
assert(clazz.equals(ElectricPotential.class));

Is there currently a way to do this? I have not found anything. If not, I would propose a change that preserves this information and allows the user to retrieve it.

@keilw keilw added the deferred label Mar 30, 2025
@keilw
Copy link
Member

keilw commented Mar 30, 2025

Unfortunately that's still not possible in Java due to the lack of reified generics, see https://maciejwalkowiak.com/blog/java-reified-generics/
Won't close it, but it has to be deferred, until the Java platform allows it. Sorry, we have discussed that several times, also spoke to Brian Goetz about it, but he said the same as in this post about Valhalla: https://openjdk.org/projects/valhalla/design-notes/in-defense-of-erasure

stechio added a commit to pdfclown/pdfclown-common that referenced this issue Jun 12, 2025
- add `_unit_parsing()` subtest demonstrating
<unitsofmeasurement/indriya#438>
- clean up imports
@stechio
Copy link

stechio commented Jun 12, 2025

@3Fish:

I use Indriya in the context of a compiler, where I need to define the actual Dimension type. Therefore I need to somehow get the actual Dimension class for a parsed Unit or Quantity.
eg.

SimpleUnitFormat format = SimpleUnitFormat.getInstance();
Unit<?> unit = format.parse("V");
Class<?> clazz = unit.getDimensionClass(); 
assert(clazz.equals(ElectricPotential.class));

Is there currently a way to do this? I have not found anything. If not, I would propose a change that preserves this information and allows the user to retrieve it.

You seem to confuse the concepts of Dimension type (see javax.measure.Dimension and its modeled types in tech.units.indriya.unit.UnitDimension) and Quantity type (see javax.measure.Quantity and its modeled types under javax.measure.quantity): the ElectricPotential you refer to in your code snippet is a Quantity type (javax.measure.quantity.ElectricPotential), NOT a Dimension type.

Therefore, I assume that what you actually look for is a way to retrieve the Quantity type, rather than the "Dimension" (which is obviously available via javax.measure.Unit.getDimension()) — BTW, the relation between Dimension and Quantity type is NOT bijective: the same Dimension may apply to different Quantity types (e.g., Angle and SolidAngle belong to the same Dimension).

I found myself in the same situation as yours and, after some struggling, I could work out a satisfactory solution based on a single utility method, org.pdfclown.common.util.measure.Units.getQuantityType(Unit), which searches among the registered quantity types a matching with the system unit associated to the unit on topic. The registered quantity types include all the standard ones, dynamically harvested to ensure their synchronization. Since I was craving for a beefed up Unit type, I also defined a wrapper (XtUnit) which features the same method for instance-based access: XtUnit.getQuantityType().

My tests show that this solution is pretty robust (see UnitsTest.getQuantityType__external() test cases against ALL the units available in tech.units.indriya.unit.Units); I also prepared a test dedicated to this very case (see UnitsTest._unit_parsing()) as a proof of concept.

If my solution may be useful to your case, feel free to reuse it, provided that you properly credit its source, thanks!

@keilw
Copy link
Member

keilw commented Jun 12, 2025

Thanks for the pointer. As the "PDFClown" project is under LGPL, I'm afraid we cannot directly use it for Indriya which is under BSD, at least I don't want to get into that whole license thing.

Does your project fully implement JSR 385?
If so, you're more than welcome to test it against the TCK and we could list it under Compatible Implementations in https://unitsofmeasurement.github.io/pages/references.html if it passes the TCK.

@stechio
Copy link

stechio commented Jun 12, 2025

Thanks for the pointer. As the "PDFClown" project is under LGPL, I'm afraid we cannot directly use it for Indriya which is under BSD, at least I don't want to get into that whole license thing.

I'm willing to accommodate your issue, relicensing my code accordingly — I could open a PR to Indriya with the relicensed files, leaving reviewers write permission so you can rearrange them into your code base (provided that my attribution is preserved, of course).

Does your project fully implement JSR 385? If so, you're more than welcome to test it against the TCK and we could list it under Compatible Implementations in https://unitsofmeasurement.github.io/pages/references.html if it passes the TCK.

No, it doesn't, sorry: pdfclown-common-util is just a utility module focused on a specific, typography-oriented subset of the units of measurements 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants