public final class HealthUnit

  1. Object
  2. 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);

Fields

public static final HealthUnit COUNTA plain tally.
public static final HealthUnit COUNT_PER_MINUTEBeats, breaths or steps per minute.
public static final HealthUnit COUNT_PER_SECONDCounts per second.
public static final HealthUnit PERCENTA ratio expressed as a percentage.
public static final HealthUnit KILOGRAMCanonical unit of HealthUnitDimension.MASS.
public static final HealthUnit GRAM
public static final HealthUnit MILLIGRAM
public static final HealthUnit MICROGRAM
public static final HealthUnit POUND
public static final HealthUnit OUNCE
public static final HealthUnit STONE
public static final HealthUnit METERCanonical unit of HealthUnitDimension.LENGTH.
public static final HealthUnit KILOMETER
public static final HealthUnit CENTIMETER
public static final HealthUnit MILE
public static final HealthUnit FOOT
public static final HealthUnit INCH
public static final HealthUnit YARD
public static final HealthUnit KILOCALORIECanonical unit of HealthUnitDimension.ENERGY.
public static final HealthUnit KILOJOULE
public static final HealthUnit JOULE
public static final HealthUnit MILLISECONDCanonical unit of HealthUnitDimension.TIME.
public static final HealthUnit SECOND
public static final HealthUnit MINUTE
public static final HealthUnit HOUR
public static final HealthUnit MILLIMETER_OF_MERCURYCanonical unit of HealthUnitDimension.PRESSURE.
public static final HealthUnit KILOPASCAL
public static final HealthUnit DEGREE_CELSIUSCanonical unit of HealthUnitDimension.TEMPERATURE.
public static final HealthUnit DEGREE_FAHRENHEITFahrenheit.
public static final HealthUnit LITERCanonical unit of HealthUnitDimension.VOLUME.
public static final HealthUnit MILLILITER
public static final HealthUnit FLUID_OUNCE_US
public static final HealthUnit CUP_US
public static final HealthUnit WATTCanonical unit of HealthUnitDimension.POWER.
public static final HealthUnit METER_PER_SECONDCanonical unit of HealthUnitDimension.VELOCITY.
public static final HealthUnit KILOMETER_PER_HOUR
public static final HealthUnit MILE_PER_HOUR
public static final HealthUnit ML_PER_KG_PER_MINUTECanonical unit of HealthUnitDimension.OXYGEN_UPTAKE; the standard VO2-max unit.
public static final HealthUnit MILLIMOLE_PER_LITERCanonical unit of HealthUnitDimension.GLUCOSE_CONCENTRATION; the SI unit, used across most of the world.
public static final HealthUnit MILLIGRAM_PER_DECILITERThe unit used clinically in the United States.

Methods

public String getSymbol()The unit symbol, in the exact syntax Apple’s HKUnit(from:) accepts.
public HealthUnitDimension getDimension()The physical dimension this unit measures.
public boolean isCompatibleWith(HealthUnit other)true when other measures the same dimension and a conversion between the two is therefore meaningful.
public double toCanonical(double value)Converts value, expressed in this unit, into the canonical unit of this unit’s dimension.
public double fromCanonical(double value)Converts value, expressed in the canonical unit of this unit’s dimension, into this unit.
public static double convert(double value, HealthUnit from, HealthUnit to)Converts a value between two units of the same dimension.
public static HealthUnit forSymbol(String symbol)Looks a unit up by its symbol, or null when the symbol is unknown to this version of the framework.
public static List<HealthUnit> values()Every unit known to this version of the framework.
public String toString()Returns getSymbol(), so string concatenation in log statements and error messages reads naturally.

Inherited methods

Field details

COUNT

public static final HealthUnit COUNT
A plain tally. Canonical unit of HealthUnitDimension.COUNT.

COUNT_PER_MINUTE

public static final HealthUnit COUNT_PER_MINUTE
Beats, breaths or steps per minute. Canonical unit of HealthUnitDimension.FREQUENCY.

COUNT_PER_SECOND

public static final HealthUnit COUNT_PER_SECOND
Counts per second.

PERCENT

public static final HealthUnit PERCENT
A ratio expressed as a percentage. Canonical unit of HealthUnitDimension.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

public static final HealthUnit KILOGRAM
Canonical unit of HealthUnitDimension.MASS.

GRAM

public static final HealthUnit GRAM

MILLIGRAM

public static final HealthUnit MILLIGRAM

MICROGRAM

public static final HealthUnit MICROGRAM

POUND

public static final HealthUnit POUND

OUNCE

public static final HealthUnit OUNCE

STONE

public static final HealthUnit STONE

METER

public static final HealthUnit METER
Canonical unit of HealthUnitDimension.LENGTH.

KILOMETER

public static final HealthUnit KILOMETER

CENTIMETER

public static final HealthUnit CENTIMETER

MILE

public static final HealthUnit MILE

INCH

public static final HealthUnit INCH

YARD

public static final HealthUnit YARD

KILOCALORIE

public static final HealthUnit KILOCALORIE
Canonical unit of HealthUnitDimension.ENERGY. Kilocalories rather than joules because it is the idiomatic unit on both platforms and in every consumer health UI.

KILOJOULE

public static final HealthUnit KILOJOULE

JOULE

public static final HealthUnit JOULE

MILLISECOND

public static final HealthUnit MILLISECOND
Canonical unit of HealthUnitDimension.TIME. Milliseconds because heart-rate variability is reported in them and the rest of the API speaks epoch millis.

SECOND

public static final HealthUnit SECOND

MINUTE

public static final HealthUnit MINUTE

HOUR

public static final HealthUnit HOUR

MILLIMETER_OF_MERCURY

public static final HealthUnit MILLIMETER_OF_MERCURY
Canonical unit of HealthUnitDimension.PRESSURE.

KILOPASCAL

public static final HealthUnit KILOPASCAL

DEGREE_CELSIUS

public static final HealthUnit DEGREE_CELSIUS

DEGREE_FAHRENHEIT

public static final HealthUnit DEGREE_FAHRENHEIT
Fahrenheit. C = F * 5/9 - 160/9, which is why HealthUnit conversion carries an offset rather than being a plain ratio.

LITER

public static final HealthUnit LITER
Canonical unit of HealthUnitDimension.VOLUME.

MILLILITER

public static final HealthUnit MILLILITER

FLUID_OUNCE_US

public static final HealthUnit FLUID_OUNCE_US

CUP_US

public static final HealthUnit CUP_US

WATT

public static final HealthUnit WATT
Canonical unit of HealthUnitDimension.POWER.

METER_PER_SECOND

public static final HealthUnit METER_PER_SECOND
Canonical unit of HealthUnitDimension.VELOCITY.

KILOMETER_PER_HOUR

public static final HealthUnit KILOMETER_PER_HOUR

MILE_PER_HOUR

public static final HealthUnit MILE_PER_HOUR

ML_PER_KG_PER_MINUTE

public static final HealthUnit ML_PER_KG_PER_MINUTE
Canonical unit of HealthUnitDimension.OXYGEN_UPTAKE; the standard VO2-max unit. The symbol is the exact HKUnit spelling.

MILLIMOLE_PER_LITER

public static final HealthUnit MILLIMOLE_PER_LITER
Canonical unit of HealthUnitDimension.GLUCOSE_CONCENTRATION; the SI unit, used across most of the world.

MILLIGRAM_PER_DECILITER

public static final HealthUnit 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

public String getSymbol()
The unit symbol, in the exact syntax Apple’s HKUnit(from:) accepts. See the class documentation for why this is public API.

getDimension

public HealthUnitDimension getDimension()
The physical dimension this unit measures.

isCompatibleWith

public boolean isCompatibleWith(HealthUnit other)
true when other measures the same dimension and a conversion between the two is therefore meaningful.

toCanonical

public double toCanonical(double value)
Converts value, expressed in this unit, into the canonical unit of this unit’s dimension.

fromCanonical

public double fromCanonical(double value)
Converts value, expressed in the canonical unit of this unit’s dimension, into this unit.

convert

public static double convert(double value, HealthUnit from, HealthUnit to)
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

public static HealthUnit forSymbol(String symbol)
Looks a unit up by its symbol, or null when the symbol is unknown to this version of the framework. Total by design: a persisted symbol read back by an older runtime yields null rather than an exception.

values

public static List<HealthUnit> values()
Every unit known to this version of the framework.

toString

public String toString()
Returns getSymbol(), so string concatenation in log statements and error messages reads naturally.