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 Details

  • 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