Kuba Petržílka
02/17/2021, 8:14 PMequals()
methods in each and every data class when I want to use one of these? 🤔 If so why such comparable variants of the spec. array types are not part of the STL yet?
Obviously, I cannot use all the exension methods provided by the STL directly then.. but it still feels a bit better than messing up my domain model with all those overrides of equals() and hashCode() typically because of one single array field..
Something like this:
class ComparableByteArray (val array: ByteArray) {
val size: Int
get() = array.size
constructor(size: Int) : this(ByteArray(size))
constructor(other: ComparableByteArray) : this(other.array.copyOf())
override fun equals(other: Any?): Boolean =
(this === other) || (javaClass == other?.javaClass && array.contentEquals((other as ComparableByteArray).array))
override fun hashCode(): Int = array.contentHashCode()
override fun toString(): String = array.toString()
operator fun get(index: Int) = array[index]
operator fun set(index: Int, value: Byte) = array.set(index, value)
operator fun iterator(): ByteIterator = array.iterator()
}
data class User {
//....,
secretKey: ComparableByteArray
//...,
// <-- Would have to override equals() and hashCode() here if I used just ByteArray so the data class loses its purpose
}
randomcat
02/17/2021, 8:33 PMrandomcat
02/17/2021, 8:33 PMKuba Petržílka
02/17/2021, 8:42 PMmkrussel
02/17/2021, 8:43 PMKuba Petržílka
02/17/2021, 8:43 PMrandomcat
02/17/2021, 8:44 PMMarc Knaup
02/17/2021, 8:45 PM