Class HealthUnit
A unit of measure for health data. Instances are interned constants, so
== is a valid identity test and is used throughout the API.
The symbol is the wire format
getSymbol() is public API rather than an internal detail because the
string genuinely has to cross the platform boundary: Apple's
HKUnit(from:) parses exactly this syntax ("count/min", "kg",
"kcal", "mmHg", "degC", "mg/dL", "mL/(kg*min)"), so the iOS
port passes it straight through with no mapping table. The Android port
maps it to the matching androidx.health.connect.client.units type.
Making that contract visible and documented is better than smuggling it
through an implementation class.
Conversion
Conversion is affine (canonical = value * scale + offset) rather than
a simple ratio, because temperature needs the offset. Converting between
units of different dimensions throws IllegalArgumentException: that is
a bug in the calling code, not a condition to be reported through an
AsyncResource.
double lb = HealthUnit.convert(80, HealthUnit.KILOGRAM, HealthUnit.POUND);
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final HealthUnitstatic final HealthUnitA plain tally.static final HealthUnitBeats, breaths or steps per minute.static final HealthUnitCounts per second.static final HealthUnitstatic final HealthUnitCanonical unit ofHealthUnitDimension.TEMPERATURE.static final HealthUnitFahrenheit.static final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitCanonical unit ofHealthUnitDimension.ENERGY.static final HealthUnitCanonical unit ofHealthUnitDimension.MASS.static final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitCanonical unit ofHealthUnitDimension.VOLUME.static final HealthUnitCanonical unit ofHealthUnitDimension.LENGTH.static final HealthUnitCanonical unit ofHealthUnitDimension.VELOCITY.static final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitThe unit used clinically in the United States.static final HealthUnitstatic final HealthUnitCanonical unit ofHealthUnitDimension.PRESSURE.static final HealthUnitCanonical unit ofHealthUnitDimension.GLUCOSE_CONCENTRATION; the SI unit, used across most of the world.static final HealthUnitCanonical unit ofHealthUnitDimension.TIME.static final HealthUnitstatic final HealthUnitCanonical unit ofHealthUnitDimension.OXYGEN_UPTAKE; the standard VO2-max unit.static final HealthUnitstatic final HealthUnitA ratio expressed as a percentage.static final HealthUnitstatic final HealthUnitstatic final HealthUnitstatic final HealthUnitCanonical unit ofHealthUnitDimension.POWER.static final HealthUnit -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleconvert(double value, HealthUnit from, HealthUnit to) Converts a value between two units of the same dimension.static HealthUnitLooks a unit up by its symbol, ornullwhen the symbol is unknown to this version of the framework.doublefromCanonical(double value) Convertsvalue, expressed in the canonical unit of this unit's dimension, into this unit.The physical dimension this unit measures.The unit symbol, in the exact syntax Apple'sHKUnit(from:)accepts.booleanisCompatibleWith(HealthUnit other) truewhenothermeasures the same dimension and a conversion between the two is therefore meaningful.doubletoCanonical(double value) Convertsvalue, expressed in this unit, into the canonical unit of this unit's dimension.toString()ReturnsgetSymbol(), so string concatenation in log statements and error messages reads naturally.static List<HealthUnit> values()Every unit known to this version of the framework.
-
Field Details
-
COUNT
A plain tally. Canonical unit ofHealthUnitDimension.COUNT. -
COUNT_PER_MINUTE
Beats, breaths or steps per minute. Canonical unit ofHealthUnitDimension.FREQUENCY. -
COUNT_PER_SECOND
Counts per second. -
PERCENT
A ratio expressed as a percentage. Canonical unit ofHealthUnitDimension.PERCENT. Note that HealthKit represents oxygen saturation and body-fat percentage as 0..1 fractions natively; the iOS port scales them into this unit. -
KILOGRAM
Canonical unit ofHealthUnitDimension.MASS. -
GRAM
-
MILLIGRAM
-
MICROGRAM
-
POUND
-
OUNCE
-
STONE
-
METER
Canonical unit ofHealthUnitDimension.LENGTH. -
KILOMETER
-
CENTIMETER
-
MILE
-
FOOT
-
INCH
-
YARD
-
KILOCALORIE
Canonical unit ofHealthUnitDimension.ENERGY. Kilocalories rather than joules because it is the idiomatic unit on both platforms and in every consumer health UI. -
KILOJOULE
-
JOULE
-
MILLISECOND
Canonical unit ofHealthUnitDimension.TIME. Milliseconds because heart-rate variability is reported in them and the rest of the API speaks epoch millis. -
SECOND
-
MINUTE
-
HOUR
-
MILLIMETER_OF_MERCURY
Canonical unit ofHealthUnitDimension.PRESSURE. -
KILOPASCAL
-
DEGREE_CELSIUS
Canonical unit ofHealthUnitDimension.TEMPERATURE. -
DEGREE_FAHRENHEIT
Fahrenheit.C = F * 5/9 - 160/9, which is whyHealthUnitconversion carries an offset rather than being a plain ratio. -
LITER
Canonical unit ofHealthUnitDimension.VOLUME. -
MILLILITER
-
FLUID_OUNCE_US
-
CUP_US
-
WATT
Canonical unit ofHealthUnitDimension.POWER. -
METER_PER_SECOND
Canonical unit ofHealthUnitDimension.VELOCITY. -
KILOMETER_PER_HOUR
-
MILE_PER_HOUR
-
ML_PER_KG_PER_MINUTE
Canonical unit ofHealthUnitDimension.OXYGEN_UPTAKE; the standard VO2-max unit. The symbol is the exactHKUnitspelling. -
MILLIMOLE_PER_LITER
Canonical unit ofHealthUnitDimension.GLUCOSE_CONCENTRATION; the SI unit, used across most of the world. -
MILLIGRAM_PER_DECILITER
The unit used clinically in the United States. The 18.0182 factor is the molar mass of glucose -- it is not a general mg/dL to mmol/L conversion and must not be reused for another analyte.
-
-
Method Details
-
getSymbol
The unit symbol, in the exact syntax Apple'sHKUnit(from:)accepts. See the class documentation for why this is public API. -
getDimension
The physical dimension this unit measures. -
isCompatibleWith
truewhenothermeasures the same dimension and a conversion between the two is therefore meaningful. -
toCanonical
public double toCanonical(double value) Convertsvalue, expressed in this unit, into the canonical unit of this unit's dimension. -
fromCanonical
public double fromCanonical(double value) Convertsvalue, expressed in the canonical unit of this unit's dimension, into this unit. -
convert
Converts a value between two units of the same dimension.
Throws
IllegalArgumentException: if either unit is null or the two measure different dimensions. Crossing dimensions is a coding error and is surfaced as one.
-
forSymbol
Looks a unit up by its symbol, ornullwhen the symbol is unknown to this version of the framework. Total by design: a persisted symbol read back by an older runtime yieldsnullrather than an exception. -
values
Every unit known to this version of the framework. -
toString
ReturnsgetSymbol(), so string concatenation in log statements and error messages reads naturally.
-