You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We need to display chart axis labels like Time (min), Distance (km), Voltage (mV). This requires two pieces of information per axis: the quantity name (e.g. "Distance") and the unit symbol (e.g. "km").
The API has no runtime-accessible concept of a "quantity name" attached to a Unit. The generic type parameter Quantity<Q> (e.g. Unit<Length>) is erased at runtime, and Unit.getName() returns the unit name (e.g. "Metre"), not the quantity name (e.g. "Length") and sometimes even null:
Unit Expression
Class
getSymbol()
getName()
toString()
Units.METRE
BaseUnit
m
Metre
m
Units.SECOND
BaseUnit
s
Second
s
Units.KILOGRAM
BaseUnit
kg
Kilogram
kg
Units.HERTZ
AlternateUnit
Hz
Hertz
Hz
Units.VOLT
AlternateUnit
V
Volt
V
Units.BECQUEREL
AlternateUnit
Bq
Becquerel
Bq
Units.MINUTE
TransformedUnit
min
Minute
min
Units.HOUR
TransformedUnit
h
Hour
h
Units.CELSIUS
TransformedUnit
℃
Celsius
℃
Units.GRAM
TransformedUnit
null
Gram
g
NANO(METRE)
TransformedUnit
null
null
nm
MILLI(SECOND)
TransformedUnit
null
null
ms
KILO(METRE)
TransformedUnit
null
null
km
KILO(HERTZ)
TransformedUnit
null
null
kHz
MILLI(VOLT)
TransformedUnit
null
null
mV
AbstractUnit.ONE
ProductUnit
``
One
one
getName() returns null for all metric-prefixed units — the most common ones in scientific applications
getSymbol() also returns null for prefixed units (only toString() works)
getName() always returns a unit name ("Metre", "Hertz"), never a quantity name ("Length", "Frequency")
There's no way to distinguish unit names from quantity names programmatically
The closest options:
Method
Returns
Problem
unit.getName()
Unit name ("Metre", "Hertz")
Not a quantity name; null for prefixed units
unit.getDimension()
Physical dimension [L]
Lossy: Wavelength, Distance, Height all map to [L]
Quantity<Q> type param
Compile-time only
Erased at runtime; can't inspect
unit.isCompatible(X)
Boolean
Requires manual mapping table from dimension→name
While even the last has some shortcomings: HERTZ and BECQUEREL share the same dimension [1/T] but are semantically different quantities (Frequency vs. Radioactivity):
This means dimension-based inference cannot reliably determine the quantity name. An isCompatible() chain checking HERTZ before BECQUEREL (or vice versa) will always pick one and miss the other. The API provides no way to ask "which quantity type is this unit for?"
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
The Problem
We need to display chart axis labels like
Time (min),Distance (km),Voltage (mV). This requires two pieces of information per axis: the quantity name (e.g. "Distance") and the unit symbol (e.g. "km").The API has no runtime-accessible concept of a "quantity name" attached to a
Unit. The generic type parameterQuantity<Q>(e.g.Unit<Length>) is erased at runtime, andUnit.getName()returns the unit name (e.g. "Metre"), not the quantity name (e.g. "Length") and sometimes evennull:getSymbol()getName()toString()Units.METREmMetremUnits.SECONDsSecondsUnits.KILOGRAMkgKilogramkgUnits.HERTZHzHertzHzUnits.VOLTVVoltVUnits.BECQUERELBqBecquerelBqUnits.MINUTEminMinuteminUnits.HOURhHourhUnits.CELSIUS℃Celsius℃Units.GRAMnullGramgNANO(METRE)nullnullnmMILLI(SECOND)nullnullmsKILO(METRE)nullnullkmKILO(HERTZ)nullnullkHzMILLI(VOLT)nullnullmVAbstractUnit.ONEOneonegetName()returnsnullfor all metric-prefixed units — the most common ones in scientific applicationsgetSymbol()also returnsnullfor prefixed units (onlytoString()works)getName()always returns a unit name ("Metre", "Hertz"), never a quantity name ("Length", "Frequency")The closest options:
unit.getName()nullfor prefixed unitsunit.getDimension()[L][L]Quantity<Q>type paramunit.isCompatible(X)While even the last has some shortcomings: HERTZ and BECQUEREL share the same dimension
[1/T]but are semantically different quantities (Frequency vs. Radioactivity):This means dimension-based inference cannot reliably determine the quantity name. An
isCompatible()chain checkingHERTZbeforeBECQUEREL(or vice versa) will always pick one and miss the other. The API provides no way to ask "which quantity type is this unit for?"Beta Was this translation helpful? Give feedback.
All reactions