diff --git a/core-java-lang/pom.xml b/core-java-lang/pom.xml index ace39de274..2f307859f1 100644 --- a/core-java-lang/pom.xml +++ b/core-java-lang/pom.xml @@ -66,6 +66,12 @@ mail ${javax.mail.version} + + nl.jqno.equalsverifier + equalsverifier + ${equalsverifier.version} + test + @@ -424,6 +430,7 @@ 3.1.1 2.0.3.RELEASE 1.6.0 + 3.0.3 diff --git a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Money.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Money.java similarity index 60% rename from java-collections-maps/src/main/java/com/baeldung/map/hashCode/Money.java rename to core-java-lang/src/main/java/com/baeldung/equalshashcode/Money.java index 0ba494d944..60c043545d 100644 --- a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Money.java +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Money.java @@ -1,4 +1,4 @@ -package com.baeldung.map.hashcode; +package com.baeldung.equalshashcode; class Money { @@ -17,15 +17,19 @@ class Money { if (!(o instanceof Money)) return false; Money other = (Money)o; + boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null) + || (this.currencyCode != null && this.currencyCode.equals(other.currencyCode)); return this.amount == other.amount - && this.currencyCode == other.currencyCode; + && currencyCodeEquals; } @Override public int hashCode() { int result = 17; result = 31 * result + amount; - result = 31 * result + currencyCode.hashCode(); + if (currencyCode != null) { + result = 31 * result + currencyCode.hashCode(); + } return result; } diff --git a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Team.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Team.java similarity index 96% rename from java-collections-maps/src/main/java/com/baeldung/map/hashCode/Team.java rename to core-java-lang/src/main/java/com/baeldung/equalshashcode/Team.java index 6c1473b8e9..c2dee1de6b 100644 --- a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Team.java +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Team.java @@ -1,4 +1,4 @@ -package com.baeldung.map.hashcode; +package com.baeldung.equalshashcode; class Team { diff --git a/core-java-lang/src/main/java/com/baeldung/equalshashcode/Voucher.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Voucher.java new file mode 100644 index 0000000000..19f46e0358 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Voucher.java @@ -0,0 +1,38 @@ +package com.baeldung.equalshashcode; + +class Voucher { + + private Money value; + private String store; + + Voucher(int amount, String currencyCode, String store) { + this.value = new Money(amount, currencyCode); + this.store = store; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Voucher)) + return false; + Voucher other = (Voucher)o; + boolean valueEquals = (this.value == null && other.value == null) + || (this.value != null && this.value.equals(other.value)); + boolean storeEquals = (this.store == null && other.store == null) + || (this.store != null && this.store.equals(other.store)); + return valueEquals && storeEquals; + } + + @Override + public int hashCode() { + int result = 17; + if (this.value != null) { + result = 31 * result + value.hashCode(); + } + if (this.store != null) { + result = 31 * result + store.hashCode(); + } + return result; + } +} \ No newline at end of file diff --git a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongTeam.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongTeam.java similarity index 67% rename from java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongTeam.java rename to core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongTeam.java index 57fb218ce0..c4477aa790 100644 --- a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongTeam.java +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongTeam.java @@ -1,5 +1,11 @@ -package com.baeldung.map.hashcode; +package com.baeldung.equalshashcode; +/* (non-Javadoc) +* This class overrides equals, but it doesn't override hashCode. +* +* To see which problems this leads to: +* TeamUnitTest.givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue +*/ class WrongTeam { String city; diff --git a/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java new file mode 100644 index 0000000000..97935bf8de --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java @@ -0,0 +1,47 @@ +package com.baeldung.equalshashcode; + +/* (non-Javadoc) +* This class extends the Money class that has overridden the equals method and once again overrides the equals method. +* +* To see which problems this leads to: +* MoneyUnitTest.givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric +*/ +class WrongVoucher extends Money { + + private String store; + + WrongVoucher(int amount, String currencyCode, String store) { + super(amount, currencyCode); + + this.store = store; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof WrongVoucher)) + return false; + WrongVoucher other = (WrongVoucher)o; + boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null) + || (this.currencyCode != null && this.currencyCode.equals(other.currencyCode)); + boolean storeEquals = (this.store == null && other.store == null) + || (this.store != null && this.store.equals(other.store)); + return this.amount == other.amount + && currencyCodeEquals + && storeEquals; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + amount; + if (this.currencyCode != null) { + result = 31 * result + currencyCode.hashCode(); + } + if (this.store != null) { + result = 31 * result + store.hashCode(); + } + return result; + } +} \ No newline at end of file diff --git a/java-collections-maps/src/test/java/com/baeldung/map/hashCode/MoneyUnitTest.java b/core-java-lang/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java similarity index 94% rename from java-collections-maps/src/test/java/com/baeldung/map/hashCode/MoneyUnitTest.java rename to core-java-lang/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java index f4cba4d114..60584fdb53 100644 --- a/java-collections-maps/src/test/java/com/baeldung/map/hashCode/MoneyUnitTest.java +++ b/core-java-lang/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.map.hashcode; +package com.baeldung.equalshashcode; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; diff --git a/java-collections-maps/src/test/java/com/baeldung/map/hashCode/TeamUnitTest.java b/core-java-lang/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java similarity index 97% rename from java-collections-maps/src/test/java/com/baeldung/map/hashCode/TeamUnitTest.java rename to core-java-lang/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java index 89a7462438..a2de408796 100644 --- a/java-collections-maps/src/test/java/com/baeldung/map/hashCode/TeamUnitTest.java +++ b/core-java-lang/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.map.hashcode; +package com.baeldung.equalshashcode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; diff --git a/java-collections-maps/pom.xml b/java-collections-maps/pom.xml index 6af8f7a5c6..0803866c51 100644 --- a/java-collections-maps/pom.xml +++ b/java-collections-maps/pom.xml @@ -41,12 +41,6 @@ streamex 0.6.5 - - nl.jqno.equalsverifier - equalsverifier - 3.0.3 - test - diff --git a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Voucher.java b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Voucher.java deleted file mode 100644 index b4536b38ea..0000000000 --- a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Voucher.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.map.hashcode; - -class Voucher { - - private Money value; - private String store; - - Voucher(int amount, String currencyCode, String store) { - this.value = new Money(amount, currencyCode); - this.store = store; - } - - @Override - public boolean equals(Object o) { - if (o == this) - return true; - if (!(o instanceof Voucher)) - return false; - Voucher other = (Voucher)o; - return this.value == other.value - && this.store == other.store; - } - - @Override - public int hashCode() { - int result = 17; - result = 31 * result + value.hashCode(); - result = 31 * result + store.hashCode(); - return result; - } -} \ No newline at end of file diff --git a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongVoucher.java b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongVoucher.java deleted file mode 100644 index 03122c7cc8..0000000000 --- a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongVoucher.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.map.hashcode; - -class WrongVoucher extends Money { - - private String store; - - WrongVoucher(int amount, String currencyCode, String store) { - super(amount, currencyCode); - - this.store = store; - } - - @Override - public boolean equals(Object o) { - if (o == this) - return true; - if (!(o instanceof WrongVoucher)) - return false; - WrongVoucher other = (WrongVoucher)o; - return this.amount == other.amount - && this.currencyCode == other.currencyCode - && this.store == other.store; - } - - @Override - public int hashCode() { - int result = 17; - result = 31 * result + amount; - result = 31 * result + currencyCode.hashCode(); - result = 31 * result + store.hashCode(); - return result; - } -} \ No newline at end of file diff --git a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/equalsverifier_notes.txt b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/equalsverifier_notes.txt deleted file mode 100644 index adc14f393a..0000000000 --- a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/equalsverifier_notes.txt +++ /dev/null @@ -1 +0,0 @@ - Non-nullity: hashCode throws NullPointerException on field city.