diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt index 71a045c3..a9a7ec1d 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt @@ -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 = 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(