public final class NutritionSample

  1. Object
  2. HealthSample
  3. SessionSample
  4. NutritionSample

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 and HealthKit does not, so getMealType() round-trips through the local store and would ride in sample metadata on iOS once that mapping exists.

Local and simulator only in this release

Neither phone carries this shape yet – see the package documentation. A read or write of HealthDataType.NUTRITION on iOS or Android is refused with HealthError.TYPE_NOT_SUPPORTED; everything here works against the local store.

Fields

public static final int MEAL_UNKNOWN = 0The entry is not attributed to a particular meal.
public static final int MEAL_BREAKFAST = 1Breakfast.
public static final int MEAL_LUNCH = 2Lunch.
public static final int MEAL_DINNER = 3Dinner.
public static final int MEAL_SNACK = 4A snack between meals.

Methods

public static NutritionSample create(long startMillis, long endMillis)A food or meal consumed over [startMillis, endMillis].
public static NutritionSample create(long atMillis)A food or meal logged at one moment.
public NutritionSample setNutrient(Nutrient nutrient, double amount)Sets a nutrient amount, expressed in that nutrient’s own unit – see Nutrient.getUnit().
public NutritionSample setNutrient(Nutrient nutrient, double amount, HealthUnit unit)Sets a nutrient amount in an explicit unit, converting into the nutrient’s own unit.
public HealthQuantity getNutrient(Nutrient nutrient)The amount of nutrient, or null when this entry does not record it.
public boolean hasNutrient(Nutrient nutrient)Whether nutrient is recorded on this entry.
public List<Nutrient> getNutrients()The nutrients actually recorded, in no particular order.
public int getNutrientCount()How many nutrients this entry records.
public int getMealType()Which meal this belongs to, as a MEAL_ constant.
public NutritionSample setMealType(int mealType)Attributes this entry to a meal, using a MEAL_ constant.
public String getFoodName()The food’s name, or null.
public NutritionSample setFoodName(String foodName)Names the food.
public String toString()Returns a string representation of the object.

Inherited methods

Field details

MEAL_UNKNOWN

public static final int MEAL_UNKNOWN = 0
The entry is not attributed to a particular meal.

MEAL_BREAKFAST

public static final int MEAL_BREAKFAST = 1
Breakfast.

MEAL_LUNCH

public static final int MEAL_LUNCH = 2
Lunch.

MEAL_DINNER

public static final int MEAL_DINNER = 3
Dinner.

MEAL_SNACK

public static final int MEAL_SNACK = 4
A snack between meals.

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()
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())