8000 add hashCode and equals to Author and Book DTOs · armdev/java-design-patterns@a8f5029 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8f5029

Browse files
committed
add hashCode and equals to Author and Book DTOs
1 parent 3128d3f commit a8f5029

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

cqrs/src/main/java/com/iluwatar/cqrs/dto/Author.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.iluwatar.cqrs.dto;
22

3+
import java.util.Objects;
4+
35
/**
46
*
57
* This is a DTO (Data Transfer Object) author, contains only useful information to be returned
@@ -48,4 +50,22 @@ public String toString() {
4850
return "AuthorDTO [name=" + name + ", email=" + email + ", username=" + username + "]";
4951
}
5052

53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(username, name, email);
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj) {
60+
if (this == obj) {
61+
return true;
62+
}
63+
if (!(obj instanceof Author)) {
64+
return false;
65+
}
66+
Author other = (Author) obj;
67+
return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name.equals(other.getName());
68+
69+
}
70+
5171
}

cqrs/src/main/java/com/iluwatar/cqrs/dto/Book.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.iluwatar.cqrs.dto;
22

3+
import java.util.Objects;
4+
35
/**
46
*
57
* This is a DTO (Data Transfer Object) book, contains only useful information to be returned
@@ -40,4 +42,21 @@ public String toString() {
4042
return "BookDTO [title=" + title + ", price=" + price + "]";
4143
}
4244

45+
@Override
46+
public int hashCode() {
47+
return Objects.hash(title, price);
48+
}
49+
50+
@Override
51+
public boolean equals(Object obj) {
52+
if (this == obj) {
53+
return true;
54+
}
55+
if (!(obj instanceof Book)) {
56+
return false;
57+
}
58+
Book book = (Book) obj;
59+
return title.equals(book.getTitle()) && price == book.getPrice();
60+
}
61+
4362
}

0 commit comments

Comments
 (0)
0