Skip to content

Commit 184800e

Browse files
authored
KTLN-798 - Equals and hashcode generator in Kotlin (#766)
1 parent e60dd8c commit 184800e

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.equalsandhashcode
2+
3+
4+
data class Person(val name: String, val age: Int, val address: String) {
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.equalsandhashcode
2+
3+
import java.util.Objects
4+
data class PersonWithUniqueName(val name: String, val age: Int, val address: String) {
5+
6+
override fun equals(other: Any?): Boolean {
7+
if (this === other) return true
8+
if (other == null || javaClass != other.javaClass) return false
9+
val otherPerson: PersonWithUniqueName = other as PersonWithUniqueName
10+
return otherPerson.name == name
11+
}
12+
13+
override fun hashCode(): Int {
14+
return Objects.hash(name)
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.equalsandhashcode
2+
3+
data class PersonWithUniqueNameAndAge(val name: String, val age: Int) {
4+
lateinit var address: String
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.equalsandhashcode
2+
3+
import org.junit.jupiter.api.Test
4+
5+
import org.junit.jupiter.api.Assertions.*
6+
7+
class PersonUnitTest {
8+
9+
@Test
10+
fun `equal when fields are equal`() {
11+
val person1 = Person("John", 18, "Address")
12+
val person2 = Person("John", 18, "Address")
13+
assertEquals(person1, person2)
14+
}
15+
16+
@Test
17+
fun `not equal when fields are different`() {
18+
val person1 = Person("John", 18, "Address")
19+
val person2 = Person("John", 18, "Another Address")
20+
assertNotEquals(person1, person2)
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.equalsandhashcode
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
5+
6+
class PersonWithUniqueNameAndAgeUnitTest {
7+
@Test
8+
fun `equal when name and age fields are equal`() {
9+
val person1 = PersonWithUniqueNameAndAge("John", 18)
10+
person1.address = "Address"
11+
val person2 = PersonWithUniqueNameAndAge("John", 18)
12+
person2.address = "Another Address"
13+
14+
assertEquals(person1, person2)
15+
}
16+
17+
@Test
18+
fun `not equal when name and age fields are not equal`() {
19+
val person1 = PersonWithUniqueNameAndAge("John", 18)
20+
person1.address = "Address"
21+
val person2 = PersonWithUniqueNameAndAge("John", 19)
22+
person2.address = "Address"
23+
24+
assertNotEquals(person1, person2)
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.equalsandhashcode
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
5+
6+
class PersonWithUniqueNameUnitTest {
7+
@Test
8+
fun `equal when name field is equal`() {
9+
val person1 = PersonWithUniqueName("John", 18, "Address")
10+
val person2 = PersonWithUniqueName("John", 19, "Another Address")
11+
assertEquals(person1, person2)
12+
}
13+
}

0 commit comments

Comments
 (0)