In my application I have custom Price
type defined as follows
public final class Price {
private BigDecimal cash;
private Currency currency;
...
}
with default Object#equals
and Object#hashCode
methods, since it's not required (I never check Price
objects logical equality).
Also, there is order receipt that represents the total price of goods in the order. If there are 3 items in order with prices 2 USD
, 3 USD
and 2 GBP
, then receipt should contain the entities 5 USD
and 2 GBP
.
For now I represent receipt as map with java.util.Currency
keys
Map<Currency, BigDecimal> receipt;
My question is if it will be a good design in changing it to
Set<Price> receipt;
and reuse my custom Price
type with overridden Object#equals
and Object#hashCode
methods as
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Price price = (Price) o;
return currency.equals(price.currency);
}
@Override
public int hashCode() {
return currency.hashCode();
}
so that
Price usd_2 = new Price(new BigDecimal(2), Currency.getInstance("USD"));
Price usd_3 = new Price(new BigDecimal(3), Currency.getInstance("USD"));
usd_2.equals(usd_3); // true
Will it be a bad choice to override equality methods in this way and what possible pitfalls it may cause in the future?