From 456f4b0f5a5a6a6ab0f6685c7cb84107728f320b Mon Sep 17 00:00:00 2001 From: cror Date: Mon, 19 Nov 2018 16:41:11 +0100 Subject: [PATCH 1/2] BAEL-2388: examples for equals and hashCode --- java-collections-maps/pom.xml | 6 +++ .../java/com/baeldung/map/hashCode/Money.java | 32 +++++++++++++ .../java/com/baeldung/map/hashCode/Team.java | 39 ++++++++++++++++ .../com/baeldung/map/hashCode/Voucher.java | 31 +++++++++++++ .../com/baeldung/map/hashCode/WrongTeam.java | 24 ++++++++++ .../baeldung/map/hashCode/WrongVoucher.java | 33 +++++++++++++ .../map/hashCode/equalsverifier_notes.txt | 1 + .../baeldung/map/hashCode/MoneyUnitTest.java | 27 +++++++++++ .../baeldung/map/hashCode/TeamUnitTest.java | 46 +++++++++++++++++++ 9 files changed, 239 insertions(+) create mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/Money.java create mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/Team.java create mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/Voucher.java create mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongTeam.java create mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongVoucher.java create mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/equalsverifier_notes.txt create mode 100644 java-collections-maps/src/test/java/com/baeldung/map/hashCode/MoneyUnitTest.java create mode 100644 java-collections-maps/src/test/java/com/baeldung/map/hashCode/TeamUnitTest.java diff --git a/java-collections-maps/pom.xml b/java-collections-maps/pom.xml index 0803866c51..6af8f7a5c6 100644 --- a/java-collections-maps/pom.xml +++ b/java-collections-maps/pom.xml @@ -41,6 +41,12 @@ 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/Money.java b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Money.java new file mode 100644 index 0000000000..0ba494d944 --- /dev/null +++ b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Money.java @@ -0,0 +1,32 @@ +package com.baeldung.map.hashcode; + +class Money { + + int amount; + String currencyCode; + + Money(int amount, String currencyCode) { + this.amount = amount; + this.currencyCode = currencyCode; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Money)) + return false; + Money other = (Money)o; + return this.amount == other.amount + && this.currencyCode == other.currencyCode; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + amount; + result = 31 * result + currencyCode.hashCode(); + return result; + } + +} diff --git a/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Team.java b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Team.java new file mode 100644 index 0000000000..6c1473b8e9 --- /dev/null +++ b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Team.java @@ -0,0 +1,39 @@ +package com.baeldung.map.hashcode; + +class Team { + + final String city; + final String department; + + Team(String city, String department) { + this.city = city; + this.department = department; + } + + @Override + public final boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Team)) + return false; + Team otherTeam = (Team)o; + boolean cityEquals = (this.city == null && otherTeam.city == null) + || this.city != null && this.city.equals(otherTeam.city); + boolean departmentEquals = (this.department == null && otherTeam.department == null) + || this.department != null && this.department.equals(otherTeam.department); + return cityEquals && departmentEquals; + } + + @Override + public final int hashCode() { + int result = 17; + if (city != null) { + result = 31 * result + city.hashCode(); + } + if (department != null) { + result = 31 * result + department.hashCode(); + } + return result; + } + +} 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 new file mode 100644 index 0000000000..b4536b38ea --- /dev/null +++ b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/Voucher.java @@ -0,0 +1,31 @@ +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/WrongTeam.java b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongTeam.java new file mode 100644 index 0000000000..57fb218ce0 --- /dev/null +++ b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongTeam.java @@ -0,0 +1,24 @@ +package com.baeldung.map.hashcode; + +class WrongTeam { + + String city; + String department; + + WrongTeam(String city, String department) { + this.city = city; + this.department = department; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof WrongTeam)) + return false; + WrongTeam otherTeam = (WrongTeam)o; + return this.city == otherTeam.city + && this.department == otherTeam.department; + } + +} 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 new file mode 100644 index 0000000000..03122c7cc8 --- /dev/null +++ b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongVoucher.java @@ -0,0 +1,33 @@ +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 new file mode 100644 index 0000000000..adc14f393a --- /dev/null +++ b/java-collections-maps/src/main/java/com/baeldung/map/hashCode/equalsverifier_notes.txt @@ -0,0 +1 @@ + Non-nullity: hashCode throws NullPointerException on field city. diff --git a/java-collections-maps/src/test/java/com/baeldung/map/hashCode/MoneyUnitTest.java b/java-collections-maps/src/test/java/com/baeldung/map/hashCode/MoneyUnitTest.java new file mode 100644 index 0000000000..f4cba4d114 --- /dev/null +++ b/java-collections-maps/src/test/java/com/baeldung/map/hashCode/MoneyUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.map.hashcode; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +public class MoneyUnitTest { + + @Test + public void givenMoneyInstancesWithSameAmountAndCurrency_whenEquals_thenReturnsTrue() { + Money income = new Money(55, "USD"); + Money expenses = new Money(55, "USD"); + + assertTrue(income.equals(expenses)); + } + + @Test + public void givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() { + Money cash = new Money(42, "USD"); + WrongVoucher voucher = new WrongVoucher(42, "USD", "Amazon"); + + assertFalse(voucher.equals(cash)); + assertTrue(cash.equals(voucher)); + } + +} diff --git a/java-collections-maps/src/test/java/com/baeldung/map/hashCode/TeamUnitTest.java b/java-collections-maps/src/test/java/com/baeldung/map/hashCode/TeamUnitTest.java new file mode 100644 index 0000000000..89a7462438 --- /dev/null +++ b/java-collections-maps/src/test/java/com/baeldung/map/hashCode/TeamUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.map.hashcode; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class TeamUnitTest { + + @Test + public void givenMapKeyWithHashCode_whenSearched_thenReturnsCorrectValue() { + Map leaders = new HashMap<>(); + leaders.put(new Team("New York", "development"), "Anne"); + leaders.put(new Team("Boston", "development"), "Brian"); + leaders.put(new Team("Boston", "marketing"), "Charlie"); + + Team myTeam = new Team("New York", "development"); + String myTeamleader = leaders.get(myTeam); + + assertEquals("Anne", myTeamleader); + } + + @Test + public void givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue() { + Map leaders = new HashMap<>(); + leaders.put(new WrongTeam("New York", "development"), "Anne"); + leaders.put(new WrongTeam("Boston", "development"), "Brian"); + leaders.put(new WrongTeam("Boston", "marketing"), "Charlie"); + + WrongTeam myTeam = new WrongTeam("New York", "development"); + String myTeamleader = leaders.get(myTeam); + + assertFalse("Anne".equals(myTeamleader)); + } + + @Test + public void equalsContract() { + EqualsVerifier.forClass(Team.class).verify(); + } + +} From a67c10eec7ff0d733c1ed86f6f590147c4bb7140 Mon Sep 17 00:00:00 2001 From: cror Date: Tue, 20 Nov 2018 13:01:06 +0100 Subject: [PATCH 2/2] BAEL-2388: moved examples java-collections-maps -> core-java-long. Fixing String comparisons --- core-java-lang/pom.xml | 7 +++ .../com/baeldung/equalshashcode}/Money.java | 10 ++-- .../com/baeldung/equalshashcode}/Team.java | 2 +- .../com/baeldung/equalshashcode/Voucher.java | 38 +++++++++++++++ .../baeldung/equalshashcode}/WrongTeam.java | 8 +++- .../baeldung/equalshashcode/WrongVoucher.java | 47 +++++++++++++++++++ .../equalshashcode}/MoneyUnitTest.java | 2 +- .../equalshashcode}/TeamUnitTest.java | 2 +- java-collections-maps/pom.xml | 6 --- .../com/baeldung/map/hashCode/Voucher.java | 31 ------------ .../baeldung/map/hashCode/WrongVoucher.java | 33 ------------- .../map/hashCode/equalsverifier_notes.txt | 1 - 12 files changed, 109 insertions(+), 78 deletions(-) rename {java-collections-maps/src/main/java/com/baeldung/map/hashCode => core-java-lang/src/main/java/com/baeldung/equalshashcode}/Money.java (60%) rename {java-collections-maps/src/main/java/com/baeldung/map/hashCode => core-java-lang/src/main/java/com/baeldung/equalshashcode}/Team.java (96%) create mode 100644 core-java-lang/src/main/java/com/baeldung/equalshashcode/Voucher.java rename {java-collections-maps/src/main/java/com/baeldung/map/hashCode => core-java-lang/src/main/java/com/baeldung/equalshashcode}/WrongTeam.java (67%) create mode 100644 core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java rename {java-collections-maps/src/test/java/com/baeldung/map/hashCode => core-java-lang/src/test/java/com/baeldung/equalshashcode}/MoneyUnitTest.java (94%) rename {java-collections-maps/src/test/java/com/baeldung/map/hashCode => core-java-lang/src/test/java/com/baeldung/equalshashcode}/TeamUnitTest.java (97%) delete mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/Voucher.java delete mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/WrongVoucher.java delete mode 100644 java-collections-maps/src/main/java/com/baeldung/map/hashCode/equalsverifier_notes.txt 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.