# Class HealthUnit

java.lang.Object
com.codename1.health.HealthUnit

public final class HealthUnit extends Object

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

  Fields
  Modifier and Type
  Field
  Description
  `static final HealthUnit`
  `CENTIMETER`
  `static final HealthUnit`
  `COUNT`
  A plain tally.
  `static final HealthUnit`
  `COUNT_PER_MINUTE`
  Beats, breaths or steps per minute.
  `static final HealthUnit`
  `COUNT_PER_SECOND`
  Counts per second.
  `static final HealthUnit`
  `CUP_US`
  `static final HealthUnit`
  `DEGREE_CELSIUS`
  Canonical unit of `HealthUnitDimension.TEMPERATURE`.
  `static final HealthUnit`
  `DEGREE_FAHRENHEIT`
  Fahrenheit.
  `static final HealthUnit`
  `FLUID_OUNCE_US`
  `static final HealthUnit`
  `FOOT`
  `static final HealthUnit`
  `GRAM`
  `static final HealthUnit`
  `HOUR`
  `static final HealthUnit`
  `INCH`
  `static final HealthUnit`
  `JOULE`
  `static final HealthUnit`
  `KILOCALORIE`
  Canonical unit of `HealthUnitDimension.ENERGY`.
  `static final HealthUnit`
  `KILOGRAM`
  Canonical unit of `HealthUnitDimension.MASS`.
  `static final HealthUnit`
  `KILOJOULE`
  `static final HealthUnit`
  `KILOMETER`
  `static final HealthUnit`
  `KILOMETER_PER_HOUR`
  `static final HealthUnit`
  `KILOPASCAL`
  `static final HealthUnit`
  `LITER`
  Canonical unit of `HealthUnitDimension.VOLUME`.
  `static final HealthUnit`
  `METER`
  Canonical unit of `HealthUnitDimension.LENGTH`.
  `static final HealthUnit`
  `METER_PER_SECOND`
  Canonical unit of `HealthUnitDimension.VELOCITY`.
  `static final HealthUnit`
  `MICROGRAM`
  `static final HealthUnit`
  `MILE`
  `static final HealthUnit`
  `MILE_PER_HOUR`
  `static final HealthUnit`
  `MILLIGRAM`
  `static final HealthUnit`
  `MILLIGRAM_PER_DECILITER`
  The unit used clinically in the United States.
  `static final HealthUnit`
  `MILLILITER`
  `static final HealthUnit`
  `MILLIMETER_OF_MERCURY`
  Canonical unit of `HealthUnitDimension.PRESSURE`.
  `static final HealthUnit`
  `MILLIMOLE_PER_LITER`
  Canonical unit of `HealthUnitDimension.GLUCOSE_CONCENTRATION`; the SI unit, used across most of the world.
  `static final HealthUnit`
  `MILLISECOND`
  Canonical unit of `HealthUnitDimension.TIME`.
  `static final HealthUnit`
  `MINUTE`
  `static final HealthUnit`
  `ML_PER_KG_PER_MINUTE`
  Canonical unit of `HealthUnitDimension.OXYGEN_UPTAKE`; the standard VO2-max unit.
  `static final HealthUnit`
  `OUNCE`
  `static final HealthUnit`
  `PERCENT`
  A ratio expressed as a percentage.
  `static final HealthUnit`
  `POUND`
  `static final HealthUnit`
  `SECOND`
  `static final HealthUnit`
  `STONE`
  `static final HealthUnit`
  `WATT`
  Canonical unit of `HealthUnitDimension.POWER`.
  `static final HealthUnit`
  `YARD`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static double`
  `convert(double value, HealthUnit from, HealthUnit to)`
  Converts a value between two units of the same dimension.
  `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.
  `double`
  `fromCanonical(double value)`
  Converts `value`, expressed in the canonical unit of this unit's dimension, into this unit.
  `HealthUnitDimension`
  `getDimension()`
  The physical dimension this unit measures.
  `String`
  `getSymbol()`
  The unit symbol, in the exact syntax Apple's `HKUnit(from:)` accepts.
  `boolean`
  `isCompatibleWith(HealthUnit other)`
  `true` when `other` measures the same dimension and a conversion between the two is therefore meaningful.
  `double`
  `toCanonical(double value)`
  Converts `value`, expressed in this unit, into the canonical unit of this unit's dimension.
  `String`
  `toString()`
  Returns `getSymbol()`, so string concatenation in log statements and error messages reads naturally.
  `static List<HealthUnit>`
  `values()`
  Every unit known to this version of the framework.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## 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

  - ### FOOT

    public static final HealthUnit FOOT

  - ### 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
    Canonical unit of `HealthUnitDimension.TEMPERATURE`.

  - ### 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.
    Overrides:
      `toString` in class `Object`