Skip to content

Commit 2378ba3

Browse files
committed
Changed DataSchemaImpl.equals implementation to only return true if the order matches too. Adapted hashCode() (this also changes behavior of ColumnSchema.Group and .Frame, but that's good!)
1 parent 6b153f1 commit 2378ba3

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/schema/DataFrameSchemaImpl.kt

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT
1212
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT_FOR_NESTED_SCHEMAS
1313
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
1414
import org.jetbrains.kotlinx.dataframe.schema.plus
15-
import kotlin.collections.forEach
1615

1716
public class DataFrameSchemaImpl(override val columns: Map<String, ColumnSchema>) : DataFrameSchema {
1817

@@ -54,11 +53,47 @@ public class DataFrameSchemaImpl(override val columns: Map<String, ColumnSchema>
5453
return result
5554
}
5655

57-
override fun equals(other: Any?): Boolean = other is DataFrameSchema && this.compare(other).isEqual()
56+
/**
57+
* Returns `true` if, and only if,
58+
* [this schema][this] has the same columns **in the same order** as the [other schema][other].
59+
* The types must also match exactly.
60+
*
61+
* Use [compare][DataFrameSchema.compare] it the order does not matter and
62+
* for other comparison options.
63+
*
64+
* @see [DataFrameSchema.compare]
65+
* @see [CompareResult.matches]
66+
*/
67+
override fun equals(other: Any?): Boolean {
68+
if (this === other) return true
69+
if (other !is DataFrameSchema) return false
70+
if (this.compare(other) != Matches) return false
71+
if (columns.keys.toList() != other.columns.keys.toList()) return false
72+
73+
for ((name, col) in columns) {
74+
val other = other.columns[name]!!
75+
when (col) {
76+
is ColumnSchema.Group -> {
77+
other as ColumnSchema.Group // safe to cast because of compare
78+
if (col.schema != other.schema) return false
79+
}
80+
81+
is ColumnSchema.Frame -> {
82+
other as ColumnSchema.Frame // safe to cast because of compare
83+
if (col.schema != other.schema) return false
84+
}
85+
86+
// already checked by compare
87+
is ColumnSchema.Value -> Unit
88+
}
89+
}
90+
91+
return true
92+
}
5893

5994
override fun toString(): String = render()
6095

61-
override fun hashCode(): Int = columns.hashCode()
96+
override fun hashCode(): Int = columns.toList().hashCode()
6297
}
6398

6499
internal fun DataFrameSchemaImpl.render(): String {

0 commit comments

Comments
 (0)