-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 add Equatable helper class
- Loading branch information
1 parent
ae03634
commit c2cf3f8
Showing
3 changed files
with
34 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tw.xcc.gumtree.api | ||
|
||
/** | ||
* A base class to facilitate [operator ==] and [hashCode] overrides. | ||
* */ | ||
abstract class Equatable { | ||
/** | ||
* The list of properties that will be used to determine whether two instances are equal. | ||
* */ | ||
protected abstract val equatableProps: List<Any?> | ||
|
||
final override operator fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is Equatable) return false | ||
if (equatableProps.size != other.equatableProps.size) return false | ||
for (i in equatableProps.indices) { | ||
if (equatableProps[i] != other.equatableProps[i]) return false | ||
} | ||
return true | ||
} | ||
|
||
final override fun hashCode(): Int = equatableProps.hashCode() xor javaClass.hashCode() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters