fix(common): manually roll equals/hashCode for CommentNode

Excludes the parent field for comparison since it can trigger cycles in some situations

Fixes COMPOSE-LOBSTERS-5G
This commit is contained in:
Harsh Shandilya 2024-08-29 16:41:08 +05:30
parent bf52966772
commit 8224b71465

View file

@ -9,9 +9,9 @@ package dev.msfjarvis.claw.common.comments
import dev.msfjarvis.claw.database.local.PostComments
import dev.msfjarvis.claw.model.Comment
internal data class CommentNode(
internal class CommentNode(
val comment: Comment,
var parent: CommentNode? = null,
private var parent: CommentNode? = null,
val children: MutableList<CommentNode> = mutableListOf(),
val isUnread: Boolean = false,
val indentLevel: Int,
@ -34,6 +34,33 @@ internal data class CommentNode(
)
}
}
/**
* [CommentNode.equals] and [CommentNode.hashCode] are hand-rolled to drop the
* [CommentNode.parent] field from the comparison since it's possible for there to be cycles in
* this comparison check. For our purposes we're fine with foregoing the field.
*/
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as CommentNode
if (comment != other.comment) return false
if (children != other.children) return false
if (isUnread != other.isUnread) return false
if (indentLevel != other.indentLevel) return false
return true
}
override fun hashCode(): Int {
var result = comment.hashCode()
result = 31 * result + children.hashCode()
result = 31 * result + isUnread.hashCode()
result = 31 * result + indentLevel
return result
}
}
internal fun createListNode(