chore: etc

This commit is contained in:
Zhongheng Liu 2026-01-16 23:41:47 +01:00
commit 895aecba3d
Signed by: steven
GPG key ID: F69B980899C1C09D
19 changed files with 404 additions and 55 deletions

View file

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

View file

@ -37,7 +37,7 @@ import java.util.Objects;
// TABLE named recipes
@Entity
@Table(name = "recipes")
public class Recipe {
public class Recipe implements Scalable<Recipe> {
// PRIMARY Key, unique id for recipe, generated automatically.
@Id
@ -191,5 +191,16 @@ public class Recipe {
", preparationSteps=" + preparationSteps +
'}';
}
public double kcal() {
return this.ingredients.stream().mapToDouble(RecipeIngredient::kcal).sum();
}
@Override
public Recipe scaleBy(double factor) {
return new Recipe(id, name, locale, ingredients.stream().map(ri -> switch (ri) {
case FormalIngredient f -> f.scaleBy(factor);
case VagueIngredient v -> v;
default -> throw new IllegalArgumentException("Unexpected child class of RecipeIngredient!");
}).toList(), preparationSteps);
}
}

View file

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

View file

@ -1,8 +1,8 @@
package commons;
public interface Scalable<IngredientType extends RecipeIngredient> {
default IngredientType scaleBy(int numPortions) {
public interface Scalable<T> {
default T scaleBy(int numPortions) {
return scaleBy((double) numPortions);
};
IngredientType scaleBy(double factor);
T scaleBy(double factor);
}

View file

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