File tree Expand file tree Collapse file tree 12 files changed +109
-78
lines changed
main/java/com/baeldung/equalshashcode
test/java/com/baeldung/equalshashcode
src/main/java/com/baeldung/map/hashCode Expand file tree Collapse file tree 12 files changed +109
-78
lines changed Original file line number Diff line number Diff line change 6666 <artifactId >mail</artifactId >
6767 <version >${javax.mail.version} </version >
6868 </dependency >
69+ <dependency >
70+ <groupId >nl.jqno.equalsverifier</groupId >
71+ <artifactId >equalsverifier</artifactId >
72+ <version >${equalsverifier.version} </version >
73+ <scope >test</scope >
74+ </dependency >
6975 </dependencies >
7076
7177 <build >
424430 <maven-shade-plugin .version>3.1.1</maven-shade-plugin .version>
425431 <spring-boot-maven-plugin .version>2.0.3.RELEASE</spring-boot-maven-plugin .version>
426432 <exec-maven-plugin .version>1.6.0</exec-maven-plugin .version>
433+ <equalsverifier .version>3.0.3</equalsverifier .version>
427434 </properties >
428435
429436</project >
Original file line number Diff line number Diff line change 1- package com .baeldung .map . hashcode ;
1+ package com .baeldung .equalshashcode ;
22
33class Money {
44
@@ -17,15 +17,19 @@ public boolean equals(Object o) {
1717 if (!(o instanceof Money ))
1818 return false ;
1919 Money other = (Money )o ;
20+ boolean currencyCodeEquals = (this .currencyCode == null && other .currencyCode == null )
21+ || (this .currencyCode != null && this .currencyCode .equals (other .currencyCode ));
2022 return this .amount == other .amount
21- && this . currencyCode == other . currencyCode ;
23+ && currencyCodeEquals ;
2224 }
2325
2426 @ Override
2527 public int hashCode () {
2628 int result = 17 ;
2729 result = 31 * result + amount ;
28- result = 31 * result + currencyCode .hashCode ();
30+ if (currencyCode != null ) {
31+ result = 31 * result + currencyCode .hashCode ();
32+ }
2933 return result ;
3034 }
3135
Original file line number Diff line number Diff line change 1- package com .baeldung .map . hashcode ;
1+ package com .baeldung .equalshashcode ;
22
33class Team {
44
Original file line number Diff line number Diff line change 1+ package com .baeldung .equalshashcode ;
2+
3+ class Voucher {
4+
5+ private Money value ;
6+ private String store ;
7+
8+ Voucher (int amount , String currencyCode , String store ) {
9+ this .value = new Money (amount , currencyCode );
10+ this .store = store ;
11+ }
12+
13+ @ Override
14+ public boolean equals (Object o ) {
15+ if (o == this )
16+ return true ;
17+ if (!(o instanceof Voucher ))
18+ return false ;
19+ Voucher other = (Voucher )o ;
20+ boolean valueEquals = (this .value == null && other .value == null )
21+ || (this .value != null && this .value .equals (other .value ));
22+ boolean storeEquals = (this .store == null && other .store == null )
23+ || (this .store != null && this .store .equals (other .store ));
24+ return valueEquals && storeEquals ;
25+ }
26+
27+ @ Override
28+ public int hashCode () {
29+ int result = 17 ;
30+ if (this .value != null ) {
31+ result = 31 * result + value .hashCode ();
32+ }
33+ if (this .store != null ) {
34+ result = 31 * result + store .hashCode ();
35+ }
36+ return result ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1- package com .baeldung .map . hashcode ;
1+ package com .baeldung .equalshashcode ;
22
3+ /* (non-Javadoc)
4+ * This class overrides equals, but it doesn't override hashCode.
5+ *
6+ * To see which problems this leads to:
7+ * TeamUnitTest.givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue
8+ */
39class WrongTeam {
410
511 String city ;
Original file line number Diff line number Diff line change 1+ package com .baeldung .equalshashcode ;
2+
3+ /* (non-Javadoc)
4+ * This class extends the Money class that has overridden the equals method and once again overrides the equals method.
5+ *
6+ * To see which problems this leads to:
7+ * MoneyUnitTest.givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric
8+ */
9+ class WrongVoucher extends Money {
10+
11+ private String store ;
12+
13+ WrongVoucher (int amount , String currencyCode , String store ) {
14+ super (amount , currencyCode );
15+
16+ this .store = store ;
17+ }
18+
19+ @ Override
20+ public boolean equals (Object o ) {
21+ if (o == this )
22+ return true ;
23+ if (!(o instanceof WrongVoucher ))
24+ return false ;
25+ WrongVoucher other = (WrongVoucher )o ;
26+ boolean currencyCodeEquals = (this .currencyCode == null && other .currencyCode == null )
27+ || (this .currencyCode != null && this .currencyCode .equals (other .currencyCode ));
28+ boolean storeEquals = (this .store == null && other .store == null )
29+ || (this .store != null && this .store .equals (other .store ));
30+ return this .amount == other .amount
31+ && currencyCodeEquals
32+ && storeEquals ;
33+ }
34+
35+ @ Override
36+ public int hashCode () {
37+ int result = 17 ;
38+ result = 31 * result + amount ;
39+ if (this .currencyCode != null ) {
40+ result = 31 * result + currencyCode .hashCode ();
41+ }
42+ if (this .store != null ) {
43+ result = 31 * result + store .hashCode ();
44+ }
45+ return result ;
46+ }
47+ }
Original file line number Diff line number Diff line change 1- package com .baeldung .map . hashcode ;
1+ package com .baeldung .equalshashcode ;
22
33import static org .junit .Assert .assertTrue ;
44import static org .junit .Assert .assertFalse ;
Original file line number Diff line number Diff line change 1- package com .baeldung .map . hashcode ;
1+ package com .baeldung .equalshashcode ;
22
33import static org .junit .Assert .assertEquals ;
44import static org .junit .Assert .assertFalse ;
Original file line number Diff line number Diff line change 4141 <artifactId >streamex</artifactId >
4242 <version >0.6.5</version >
4343 </dependency >
44- <dependency >
45- <groupId >nl.jqno.equalsverifier</groupId >
46- <artifactId >equalsverifier</artifactId >
47- <version >3.0.3</version >
48- <scope >test</scope >
49- </dependency >
5044 </dependencies >
5145
5246 <properties >
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments