+
Skip to content

Value semantics for Entity equals/hashCode #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ subprojects { project ->
name = "Pedro Domingues"
email = "pedro.domingues.pt@gmail.com"
}
developer {
id = "efenderbosch"
name = "Eric Fenderbosch"
email = "eric@fender.net"
}
}
scm {
url = "https://github.com/kotlin-orm/ktorm.git"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ internal class EntityImplementation(

override fun equals(other: Any?): Boolean {
return when (other) {
is EntityImplementation -> this === other
is Entity<*> -> this === other.implementation
is EntityImplementation -> values == other.values && entityClass == other.entityClass
is Entity<*> -> values == other.implementation.values && entityClass == other.implementation.entityClass
else -> false
}
}

override fun hashCode(): Int {
return System.identityHashCode(this)
return values.hashCode() + 13 * entityClass.hashCode()
}

override fun toString(): String {
Expand Down
35 changes: 35 additions & 0 deletions ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,39 @@ class EntityTest : BaseTest() {
employee = database.employees.find { it.id eq employee.id } ?: throw AssertionError()
assert(employee.job == "engineer")
}

@Test
fun testValueEquality() {
val now = LocalDate.now()
val employee1 = Employee {
id = 1
name = "Eric"
job = "contributor"
hireDate = now
salary = 50
}

val employee2 = Employee {
id = 1
name = "Eric"
job = "contributor"
hireDate = now
salary = 50
}

assert(employee1 == employee2)
}

@Test
fun testDifferentClassesSameValuesNotEqual() {
val employee = Employee {
name = "name"
}

val department = Department {
name = "name"
}

assert(employee != department)
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载