Skip to content

Commit aaff04b

Browse files
committed
Generate a more correct equals
1 parent a662629 commit aaff04b

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

processor/src/main/kotlin/com/tobrun/datacompat/DataCompatProcessor.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,18 @@ class DataCompatProcessor(
132132
)
133133

134134
// Function equals
135-
val equalsStatementList = mutableListOf("other is $className")
136-
for (entry in propertyMap) {
137-
equalsStatementList.add("&& ${entry.key} == other.${entry.key}")
138-
}
139135
val equalsBuilder = FunSpec.builder("equals")
140136
.addModifiers(KModifier.OVERRIDE)
141137
.addParameter("other", ANY.copy(nullable = true))
138+
.addStatement("if (this === other) return true")
139+
.addStatement("if (javaClass != other?.javaClass) return false")
140+
.addStatement("other as $className")
142141
.addStatement(
143-
equalsStatementList.joinToString(
142+
propertyMap.keys.joinToString(
144143
prefix = "return ",
145-
separator = "\n\t\t"
144+
separator = " && ",
145+
transform = { "$it == other.$it" },
146+
postfix = ""
146147
)
147148
)
148149
.returns(Boolean::class)

0 commit comments

Comments
 (0)