# Class NutritionSample

java.lang.Object
com.codename1.health.HealthSample
com.codename1.health.SessionSample
com.codename1.health.nutrition.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 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

invalid reference

    HealthError#TYPE_NOT_SUPPORTED

; everything here works against the

local store.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `MEAL_BREAKFAST`
  Breakfast.
  `static final int`
  `MEAL_DINNER`
  Dinner.
  `static final int`
  `MEAL_LUNCH`
  Lunch.
  `static final int`
  `MEAL_SNACK`
  A snack between meals.
  `static final int`
  `MEAL_UNKNOWN`
  The entry is not attributed to a particular meal.

- ## Method Summary

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

  ### Methods inherited from class SessionSample

  `getNotes, getTitle, setNotes, setTitle`

  ### Methods inherited from class HealthSample

  `equals, getDuration, getDurationMillis, getEnd, getEndMillis, getId, getMetadata, getRecordingMethod, getSource, getStart, getStartMillis, getType, hashCode, isInstantaneous, putMetadata, setId, setRecordingMethod, setSource`

  ### Methods inherited from class Object

  `clone, getClass, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### MEAL_UNKNOWN

    public static final int MEAL_UNKNOWN
    The entry is not attributed to a particular meal.
    See Also:
      - Constant Field Values

  - ### MEAL_BREAKFAST

    public static final int MEAL_BREAKFAST
    Breakfast.
    See Also:
      - Constant Field Values

  - ### MEAL_LUNCH

    public static final int MEAL_LUNCH
    Lunch.
    See Also:
      - Constant Field Values

  - ### MEAL_DINNER

    public static final int MEAL_DINNER
    Dinner.
    See Also:
      - Constant Field Values

  - ### MEAL_SNACK

    public static final int MEAL_SNACK
    A snack between meals.
    See Also:
      - Constant Field Values

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