chore(commons): implement missing equals/hashcode

This commit is contained in:
Zhongheng Liu 2025-12-29 14:01:44 +01:00
commit 554d769305
4 changed files with 56 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package commons;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
/** /**
@ -60,4 +61,17 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
public FormalIngredient scaleBy(double factor) { public FormalIngredient scaleBy(double factor) {
return new FormalIngredient(getIngredient(), amount, unitSuffix); return new FormalIngredient(getIngredient(), amount, unitSuffix);
} }
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
FormalIngredient that = (FormalIngredient) o;
return Double.compare(amount, that.amount) == 0 && Objects.equals(unitSuffix, that.unitSuffix);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), amount, unitSuffix);
}
} }

View file

@ -6,6 +6,8 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import java.util.Objects;
@Entity @Entity
public class Ingredient { public class Ingredient {
@ -69,6 +71,18 @@ public class Ingredient {
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Ingredient that = (Ingredient) o;
return id == that.id && Double.compare(proteinPer100g, that.proteinPer100g) == 0 && Double.compare(fatPer100g, that.fatPer100g) == 0 && Double.compare(carbsPer100g, that.carbsPer100g) == 0 && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name, proteinPer100g, fatPer100g, carbsPer100g);
}
} }

View file

@ -11,6 +11,8 @@ import jakarta.persistence.InheritanceType;
import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import java.util.Objects;
/** /**
* The base RecipeIngredient class, holding a reference to the * The base RecipeIngredient class, holding a reference to the
* {@link Ingredient Ingredient} it has an (in)formal amount * {@link Ingredient Ingredient} it has an (in)formal amount
@ -73,4 +75,16 @@ public abstract class RecipeIngredient {
public Ingredient getIngredient() { public Ingredient getIngredient() {
return ingredient; return ingredient;
} }
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
RecipeIngredient that = (RecipeIngredient) o;
return Objects.equals(id, that.id) && Objects.equals(ingredient, that.ingredient);
}
@Override
public int hashCode() {
return Objects.hash(id, ingredient);
}
} }

View file

@ -2,6 +2,8 @@ package commons;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import java.util.Objects;
/** /**
* A vague ingredient inheriting from {@link RecipeIngredient RecipeIngredient}. * A vague ingredient inheriting from {@link RecipeIngredient RecipeIngredient}.
* It has a {@link String String} description that describes an informal quantity. * It has a {@link String String} description that describes an informal quantity.
@ -31,4 +33,16 @@ public class VagueIngredient extends RecipeIngredient {
public String toString() { public String toString() {
return description + " " + ingredient.name; return description + " " + ingredient.name;
} }
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
VagueIngredient that = (VagueIngredient) o;
return Objects.equals(description, that.description);
}
@Override
public int hashCode() {
return Objects.hashCode(description);
}
} }