BAEL-2388: examples for equals and hashCode

This commit is contained in:
cror 2018-11-19 16:41:11 +01:00
parent ba98695605
commit 456f4b0f5a
9 changed files with 239 additions and 0 deletions

View File

@ -41,6 +41,12 @@
<artifactId>streamex</artifactId>
<version>0.6.5</version>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1 @@
Non-nullity: hashCode throws NullPointerException on field city.

View File

@ -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));
}
}

View File

@ -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<Team,String> 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<WrongTeam,String> 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();
}
}