Skip to content

Commit 16fd975

Browse files
add comments for equals & hashCode
1 parent 53d8422 commit 16fd975

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

ktorm-core/src/main/kotlin/org/ktorm/entity/Entity.kt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ import kotlin.reflect.jvm.jvmErasure
4444
*
4545
* ### Creating Entity Objects
4646
*
47-
* As everyone knows, interfaces cannot be instantiated, so Ktorm provides [Entity.create] functions for us to
48-
* create entity objects. Those functions generate implementations for entity interfaces via JDK dynamic proxy
47+
* As everyone knows, interfaces cannot be instantiated, so Ktorm provides a [Entity.create] function for us to
48+
* create entity objects. This function generate implementations for entity interfaces via JDK dynamic proxy
4949
* and create their instances.
5050
*
51-
* If you don't like creating objects by [Entity.create] functions, Ktorm also provides an abstract factory class
51+
* In case you don't like creating objects by [Entity.create], Ktorm also provides an abstract factory class
5252
* [Entity.Factory]. This class overloads the `invoke` operator of Kotlin, so we just need to add a companion
5353
* object to our entity class extending from [Entity.Factory], then entity objects can be created just like there
5454
* is a constructor: `val department = Department()`.
@@ -209,6 +209,27 @@ public interface Entity<E : Entity<E>> : Serializable {
209209
*/
210210
public fun copy(): E
211211

212+
/**
213+
* Indicate whether some other object is "equal to" this entity.
214+
* Two entities are equal only if they have the same [entityClass] and [properties].
215+
*
216+
* @since 3.4.0
217+
*/
218+
public override fun equals(other: Any?): Boolean
219+
220+
/**
221+
* Return a hash code value for this entity.
222+
*
223+
* @since 3.4.0
224+
*/
225+
public override fun hashCode(): Int
226+
227+
/**
228+
* Return a string representation of this table.
229+
* The format is like `Employee{id=1, name=Eric, job=contributor, hireDate=2021-05-05, salary=50}`.
230+
*/
231+
public override fun toString(): String
232+
212233
/**
213234
* Companion object provides functions to create entity instances.
214235
*/

ktorm-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ class EntityTest : BaseTest() {
520520
val employee = database.employees.find { it.id eq 2 } ?: return
521521
val copy = employee.copy()
522522

523-
assert(employee.hireDate == copy.hireDate)
523+
assert(employee == copy)
524+
assert(employee !== copy)
524525
assert(employee.hireDate !== copy.hireDate) // should not be the same instance because of deep copy.
525526
assert(copy.manager?.implementation?.parent === copy.implementation) // should keep the parent relationship.
526527
}

0 commit comments

Comments
 (0)