Class 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; HealthKit does
not, and the iOS port carries it in sample metadata. Either way
getMealType() round-trips.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intBreakfast.static final intDinner.static final intLunch.static final intA snack between meals.static final intThe entry is not attributed to a particular meal. -
Method Summary
Modifier and TypeMethodDescriptionstatic NutritionSamplecreate(long atMillis) A food or meal logged at one moment.static NutritionSamplecreate(long startMillis, long endMillis) A food or meal consumed over[startMillis, endMillis].The food's name, or null.intWhich meal this belongs to, as aMEAL_constant.getNutrient(Nutrient nutrient) The amount ofnutrient, ornullwhen this entry does not record it.intHow many nutrients this entry records.The nutrients actually recorded, in no particular order.booleanhasNutrient(Nutrient nutrient) Whethernutrientis recorded on this entry.setFoodName(String foodName) Names the food.setMealType(int mealType) Attributes this entry to a meal, using aMEAL_constant.setNutrient(Nutrient nutrient, double amount) Sets a nutrient amount, expressed in that nutrient's own unit -- seeNutrient.getUnit().setNutrient(Nutrient nutrient, double amount, HealthUnit unit) Sets a nutrient amount in an explicit unit, converting into the nutrient's own unit.toString()Returns a string representation of the object.Methods inherited from class SessionSample
getNotes, getTitle, setNotes, setTitleMethods inherited from class HealthSample
equals, getDurationMillis, getEndMillis, getId, getMetadata, getRecordingMethod, getSource, getStartMillis, getType, hashCode, isInstantaneous, putMetadata, setId, setRecordingMethod, setSource
-
Field Details
-
MEAL_UNKNOWN
public static final int MEAL_UNKNOWNThe entry is not attributed to a particular meal.- See Also:
-
MEAL_BREAKFAST
public static final int MEAL_BREAKFASTBreakfast.- See Also:
-
MEAL_LUNCH
public static final int MEAL_LUNCHLunch.- See Also:
-
MEAL_DINNER
public static final int MEAL_DINNERDinner.- See Also:
-
MEAL_SNACK
public static final int MEAL_SNACKA snack between meals.- See Also:
-
-
Method Details
-
create
A food or meal consumed over[startMillis, endMillis]. -
create
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
Sets a nutrient amount, expressed in that nutrient's own unit -- seeNutrient.getUnit(). -
setNutrient
Sets a nutrient amount in an explicit unit, converting into the nutrient's own unit.
Throws
IllegalArgumentException: ifunitmeasures a different dimension than the nutrient -- protein in millilitres, say.
-
getNutrient
The amount of
nutrient, ornullwhen 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
Whethernutrientis recorded on this entry. -
getNutrients
-
getNutrientCount
public int getNutrientCount()How many nutrients this entry records. -
getMealType
public int getMealType()Which meal this belongs to, as aMEAL_constant. -
setMealType
Attributes this entry to a meal, using aMEAL_constant. -
getFoodName
The food's name, or null. -
setFoodName
Names the food. -
toString
Description copied from class:ObjectReturns 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:
toStringin classHealthSample
-