feat(commons): create new Scalable<T extends RecipeIngredient> interface

Scalable<T extends Recipe> supplies two scaleBy() functions that
implementors should create. This allows for custom handling if more
types of ingredients are handled in the future.
This commit is contained in:
Zhongheng Liu 2025-12-29 12:51:32 +01:00
commit 2d1df8fc11
4 changed files with 48 additions and 1 deletions

View file

@ -10,7 +10,7 @@ import java.util.Optional;
* @see RecipeIngredient * @see RecipeIngredient
*/ */
@Entity @Entity
public class FormalIngredient extends RecipeIngredient { public class FormalIngredient extends RecipeIngredient implements Scalable<FormalIngredient> {
private double amount; private double amount;
private String unitSuffix; private String unitSuffix;
@ -50,4 +50,14 @@ public class FormalIngredient extends RecipeIngredient {
public String toString() { public String toString() {
return amount + unitSuffix + " of " + ingredient.name; return amount + unitSuffix + " of " + ingredient.name;
} }
@Override
public FormalIngredient scaleBy(int numPortions) {
return new FormalIngredient(getIngredient(), amount * numPortions, unitSuffix);
}
@Override
public FormalIngredient scaleBy(double factor) {
return new FormalIngredient(getIngredient(), amount, unitSuffix);
}
} }

View file

@ -57,4 +57,20 @@ public abstract class RecipeIngredient {
//store it in the field //store it in the field
this.ingredient = ingredient; this.ingredient = ingredient;
} }
public void setId(Long id) {
this.id = id;
}
public void setIngredient(Ingredient ingredient) {
this.ingredient = ingredient;
}
public Long getId() {
return id;
}
public Ingredient getIngredient() {
return ingredient;
}
} }

View file

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

View file

@ -16,6 +16,21 @@ public enum Unit {
KILOGRAMME("kg", true, 1_000.0 ), KILOGRAMME("kg", true, 1_000.0 ),
TONNE("t", true, 1_000_000.0), TONNE("t", true, 1_000_000.0),
// TODO Consider more fine-tuned volume unit definitions
// TODO Use density-based calculations?
/*
Volume units are used with the assumption that 1L of ingredient = 1kg.
*/
LITRE("l", true, 1_000.0),
MILLILITRE("ml", true, 1.0),
// We love our American friends!!
// Source:
// https://whatscookingamerica.net/equiv.htm
TABLESPOON("tbsp", true, 14.0),
OUNCE("oz", true, 28.35),
POUND("lb", true, 453.6),
CUP("cup", true, 225),
// A special informal unit // A special informal unit
INFORMAL("<NONE>", false, 0.0); INFORMAL("<NONE>", false, 0.0);