Implement hashCode/equals on ID for Entities

This also drops an angry comment that was the result of not being able
to fathom the default behaviour.

Fixes #87.
This commit is contained in:
Simon Bruder 2024-01-03 23:46:34 +01:00
parent baafd768ec
commit 90d368a95b
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
6 changed files with 66 additions and 10 deletions

View file

@ -2,6 +2,7 @@
// SPDX-FileCopyrightText: 2023 swt23w23
package catering.catalog;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -60,4 +61,16 @@ public class Consumable extends Product {
wholesalePrice = price;
}
@Override
public int hashCode() {
return Objects.hash(Consumable.class, getId());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Consumable other) {
return getId().equals(other.getId());
}
return false;
}
}

View file

@ -2,6 +2,7 @@
// SPDX-FileCopyrightText: 2023 swt23w23
package catering.catalog;
import java.util.Objects;
import java.util.Set;
import javax.money.MonetaryAmount;
@ -48,4 +49,17 @@ public class Rentable extends Product {
public MonetaryAmount getPriceForTime(int hours) {
return getPrice().multiply(hours).add(getPrice());
}
@Override
public int hashCode() {
return Objects.hash(Rentable.class, getId());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Rentable other) {
return getId().equals(other.getId());
}
return false;
}
}

View file

@ -16,6 +16,7 @@ import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Entity
@ -127,4 +128,17 @@ public class CustomOrder extends Order {
public void setInvoiceAvailable(boolean available) {
this.invoiceAvailable = available;
}
@Override
public int hashCode() {
return Objects.hash(CustomOrder.class, getId());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof CustomOrder other) {
return getId().equals(other.getId());
}
return false;
}
}

View file

@ -8,6 +8,7 @@ import org.salespointframework.quantity.Quantity;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
@Entity
public class CustomCatalogEntry {
@ -77,4 +78,17 @@ public class CustomCatalogEntry {
public void setTotalCost(int totalCost) {
this.totalCost = totalCost;
}
@Override
public int hashCode() {
return Objects.hash(CustomCatalogEntry.class, getId());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof CustomCatalogEntry other) {
return getId() == other.getId();
}
return false;
}
}

View file

@ -4,6 +4,8 @@ package catering.staff;
import static org.salespointframework.core.Currencies.EURO;
import java.util.Objects;
import javax.money.MonetaryAmount;
import org.javamoney.moneta.Money;
@ -58,6 +60,11 @@ public class Employee {
this.wage = Money.of(wage, EURO);
}
@Override
public int hashCode() {
return Objects.hash(Employee.class, getId());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Employee other) {

View file

@ -7,6 +7,8 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import java.util.Objects;
import org.salespointframework.core.AbstractAggregateRoot;
import org.salespointframework.useraccount.Role;
import org.salespointframework.useraccount.UserAccount;
@ -74,23 +76,15 @@ public class User {
return userAccount.getRoles().stream().toList().contains(Role.of(role));
}
// Der Ficker muss so. -- sbruder
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((fullName == null) ? 0 : fullName.hashCode());
result = prime * result + ((userAccount == null) ? 0 : userAccount.getId().hashCode());
return result;
return Objects.hash(User.class, getId());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof User other) {
return address.equals(other.address) && fullName.equals(other.fullName)
&& userAccount.getPassword().equals(other.userAccount.getPassword());
return getId().equals(other.getId());
}
return false;
}