From 456f4b0f5a5a6a6ab0f6685c7cb84107728f320b Mon Sep 17 00:00:00 2001 From: cror Date: Mon, 19 Nov 2018 16:41:11 +0100 Subject: [PATCH] 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(); + } + +}