Class NutritionSample


public final class NutritionSample extends SessionSample

A logged food or meal, carrying whichever nutrients are known about it.

Sparse by design

A NutritionSample holds only the nutrients actually set. A logged apple sets four fields; a packaged food scanned from a barcode might set thirty. Modelling this as forty nullable fields -- which is roughly what both platforms do -- would make every consumer write forty null checks, so this exposes a map instead and getNutrients() returns only what is present.

NutritionSample lunch = NutritionSample.create(start, end);
lunch.setTitle("Chicken salad");
lunch.setNutrient(Nutrient.ENERGY, 420);
lunch.setNutrient(Nutrient.PROTEIN, 35);
lunch.setNutrient(Nutrient.SODIUM, 610);
Meal type

Health Connect records which meal an entry belongs to; HealthKit does not, and the iOS port carries it in sample metadata. Either way getMealType() round-trips.

  • Field Details

  • Method Details

    • create

      public static NutritionSample create(long startMillis, long endMillis)
      A food or meal consumed over [startMillis, endMillis].
    • create

      public static NutritionSample create(long atMillis)
      A food or meal logged at one moment. Nutrition is interval-only, so this records a one-second span rather than a zero-width one.
    • setNutrient

      public NutritionSample setNutrient(Nutrient nutrient, double amount)
      Sets a nutrient amount, expressed in that nutrient's own unit -- see Nutrient.getUnit().
    • setNutrient

      public NutritionSample setNutrient(Nutrient nutrient, double amount, HealthUnit unit)

      Sets a nutrient amount in an explicit unit, converting into the nutrient's own unit.

      Throws
      • IllegalArgumentException: if unit measures a different dimension than the nutrient -- protein in millilitres, say.
    • getNutrient

      public HealthQuantity getNutrient(Nutrient nutrient)

      The amount of nutrient, or null when this entry does not record it.

      Null rather than zero, for the same reason aggregate buckets return null: "this food's sodium was never measured" and "this food contains no sodium" are different claims, and only one of them is safe to show someone managing their intake.

    • hasNutrient

      public boolean hasNutrient(Nutrient nutrient)
      Whether nutrient is recorded on this entry.
    • getNutrients

      public List<Nutrient> getNutrients()
      The nutrients actually recorded, in no particular order.
    • getNutrientCount

      public int getNutrientCount()
      How many nutrients this entry records.
    • getMealType

      public int getMealType()
      Which meal this belongs to, as a MEAL_ constant.
    • setMealType

      public NutritionSample setMealType(int mealType)
      Attributes this entry to a meal, using a MEAL_ constant.
    • getFoodName

      public String getFoodName()
      The food's name, or null.
    • setFoodName

      public NutritionSample setFoodName(String foodName)
      Names the food.
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class HealthSample