Generate equals/hashCode(): Support content equality from stdlib

#KT-22361 Fixed
This commit is contained in:
Alexey Sedunov
2018-07-06 21:17:39 +03:00
parent 5fcc6cfa0b
commit b441c76313
5 changed files with 76 additions and 30 deletions
@@ -1,5 +1,3 @@
import java.util.Arrays
class A(val n: IntArray, val s: Array<String>) {
val f: Float = 1.0f
@@ -13,16 +11,16 @@ class A(val n: IntArray, val s: Array<String>) {
other as A
if (!Arrays.equals(n, other.n)) return false
if (!Arrays.equals(s, other.s)) return false
if (!n.contentEquals(other.n)) return false
if (!s.contentEquals(other.s)) return false
if (f != other.f) return false
return true
}
override fun hashCode(): Int {
var result = Arrays.hashCode(n)
result = 31 * result + Arrays.hashCode(s)
var result = n.contentHashCode()
result = 31 * result + s.contentHashCode()
result = 31 * result + f.hashCode()
return result
}
@@ -1,5 +1,3 @@
import java.util.Arrays
data class A(val a: IntArray) {
<caret>override fun equals(other: Any?): Boolean {
if (this === other) return true
@@ -7,12 +5,12 @@ data class A(val a: IntArray) {
other as A
if (!Arrays.equals(a, other.a)) return false
if (!a.contentEquals(other.a)) return false
return true
}
override fun hashCode(): Int {
return Arrays.hashCode(a)
return a.contentHashCode()
}
}
@@ -1,5 +1,3 @@
import java.util.Arrays
class EqKotlin(val a: Array<Array<String>>) {
<caret>override fun equals(other: Any?): Boolean {
if (this === other) return true
@@ -7,12 +5,12 @@ class EqKotlin(val a: Array<Array<String>>) {
other as EqKotlin
if (!Arrays.deepEquals(a, other.a)) return false
if (!a.contentDeepEquals(other.a)) return false
return true
}
override fun hashCode(): Int {
return Arrays.deepHashCode(a)
return a.contentDeepHashCode()
}
}
@@ -1,5 +1,3 @@
import java.util.Arrays
class A(val n: IntArray?, val s: Array<String>?) {
val f: Float = 1.0f
@@ -13,16 +11,22 @@ class A(val n: IntArray?, val s: Array<String>?) {
other as A
if (!Arrays.equals(n, other.n)) return false
if (!Arrays.equals(s, other.s)) return false
if (n != null) {
if (other.n == null) return false
if (!n.contentEquals(other.n)) return false
} else if (other.n != null) return false
if (s != null) {
if (other.s == null) return false
if (!s.contentEquals(other.s)) return false
} else if (other.s != null) return false
if (f != other.f) return false
return true
}
override fun hashCode(): Int {
var result = n?.let { Arrays.hashCode(it) } ?: 0
result = 31 * result + (s?.let { Arrays.hashCode(it) } ?: 0)
var result = n?.contentHashCode() ?: 0
result = 31 * result + (s?.contentHashCode() ?: 0)
result = 31 * result + f.hashCode()
return result
}