feat(commons/kcal): kcal eval infrastructure

This commit is contained in:
Zhongheng Liu 2026-01-18 18:18:45 +01:00
commit 8422734a3f
Signed by: steven
GPG key ID: F69B980899C1C09D
4 changed files with 28 additions and 1 deletions

View file

@ -76,4 +76,15 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
public int hashCode() { public int hashCode() {
return Objects.hash(super.hashCode(), amount, unitSuffix); return Objects.hash(super.hashCode(), amount, unitSuffix);
} }
@Override
public double kcal() {
final double PER_GRAMS = 100;
return ingredient.kcalPer100g() * amountInBaseUnit() / PER_GRAMS;
}
@Override
public double amount() {
return amountInBaseUnit();
}
} }

View file

@ -198,7 +198,12 @@ public class Recipe {
default -> throw new IllegalStateException("Unexpected value: " + ri); default -> throw new IllegalStateException("Unexpected value: " + ri);
}).toList(); }).toList();
return new Recipe(recipe.getId(), recipe.getName(), recipe.getLocale(), i, recipe.getPreparationSteps()); return new Recipe(recipe.getId(), recipe.getName(), recipe.getLocale(), i, recipe.getPreparationSteps());
}
public double kcal() {
final double PER = 100; // Gram
return
this.ingredients.stream().mapToDouble(RecipeIngredient::kcal).sum() /
this.ingredients.stream().mapToDouble(RecipeIngredient::amount).sum() * PER;
} }
} }

View file

@ -92,4 +92,6 @@ public abstract class RecipeIngredient {
public int hashCode() { public int hashCode() {
return Objects.hash(id, ingredient); return Objects.hash(id, ingredient);
} }
public abstract double kcal();
public abstract double amount();
} }

View file

@ -50,4 +50,13 @@ public class VagueIngredient extends RecipeIngredient {
public int hashCode() { public int hashCode() {
return Objects.hashCode(description); return Objects.hashCode(description);
} }
@Override
public double kcal() {
return 0;
}
@Override
public double amount() {
return 0;
}
} }