假设我只想在生成的equals和hashCode实现中包含一个或两个字段(或者可能排除一个或多个字段)。对于一个简单的类,例如:
data class Person(val id: String, val name: String)
Groovy有这个:
@EqualsAndHashCode(includes = 'id')
龙目岛有这个:
@EqualsAndHashCode(of = "id")
在Kotlin这样做的惯用方法是什么?
data class Person(val id: String) {
// at least we can guarantee it is present at access time
var name: String by Delegates.notNull()
constructor(id: String, name: String): this(id) {
this.name = name
}
}
虽然感觉不对......我真的不希望name
变得可变,额外的构造函数定义很难看。
答案 0 :(得分:9)
不幸的是,这个解决方案不再有效,而且我还没有(这个)这个问题的另一个好主意。
自动生成的函数仅使用在主构造函数中声明的属性(具有val
或var
的参数)。所以你可以写:
data class Person(val id: String, name: String) {
val name: String = name
}
有关更多信息,请参阅有关Data Classes的参考资料。
答案 1 :(得分:9)
我使用过这种方法。
data class Person(val id: String, val name: String) {
override fun equals(other: Person) = EssentialData(this) == EssentialData(other)
override fun hashCode() = EssentialData(this).hashCode()
override fun toString() = EssentialData(this).toString().replaceFirst("EssentialData", "Person")
}
private data class EssentialData(val id: String) {
constructor(person: Person) : this(id = person.id)
}
答案 2 :(得分:5)
此方法可能适用于财产排斥:
class SkipProperty<T>(val property: T) {
override fun equals(other: Any?) = true
override fun hashCode() = 0
}
SkipProperty.equals
仅返回true,这将导致嵌入的property
被跳过到父对象的equals
中。
data class Person(
val id: String,
val name: SkipProperty<String>
)
答案 3 :(得分:3)
我也不知道Kotlin(1.1)中的“idomatic way”这样做......
我最终覆盖了equals
和hashCode
:
data class Person(val id: String,
val name: String) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as Person
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
}
是不是有“更好”的方式?
答案 4 :(得分:2)
这是一种有点创造性的方法:
data class IncludedArgs(val args: Array<out Any>)
fun includedArgs(vararg args: Any) = IncludedArgs(args)
abstract class Base {
abstract val included : IncludedArgs
override fun equals(other: Any?) = when {
this identityEquals other -> true
other is Base -> included == other.included
else -> false
}
override fun hashCode() = included.hashCode()
override fun toString() = included.toString()
}
class Foo(val a: String, val b : String) : Base() {
override val included = includedArgs(a)
}
fun main(args : Array<String>) {
val foo1 = Foo("a", "b")
val foo2 = Foo("a", "B")
println(foo1 == foo2) //prints "true"
println(foo1) //prints "IncludedArgs(args=[a])"
}
答案 5 :(得分:1)
这是基于@bashor的方法,并使用了私有的主要构造函数和公共的辅助构造函数。遗憾的是,equals不能忽略的属性不能是val,但是可以隐藏setter,所以从外部角度看,结果是等效的。
data class ExampleDataClass private constructor(val important: String) {
var notSoImportant: String = ""
private set
constructor(important: String, notSoImportant: String) : this(important) {
this.notSoImportant = notSoImportant
}
}
答案 6 :(得分:0)
可重用的解决方案::为了方便选择在equals()
和hashCode()
中包含哪些字段,我编写了一个名为“ stem”(基本核心数据)的小助手。 ,与平等相关)。
用法非常简单,并且生成的代码非常小:
class Person(val id: String, val name: String) {
private val stem = Stem(this, { id })
override fun equals(other: Any?) = stem.eq(other)
override fun hashCode() = stem.hc()
}
可以通过即时进行额外的计算来权衡存储在类中的后备字段:
private val stem get() = Stem(this, { id })
由于Stem
具有任何功能,因此您可以自由指定如何计算相等性。要考虑多个字段,只需为每个字段添加一个lambda表达式(变量):
private val stem = Stem(this, { id }, { name })
实施:
class Stem<T : Any>(
private val thisObj: T,
private vararg val properties: T.() -> Any?
) {
fun eq(other: Any?): Boolean {
if (thisObj === other)
return true
if (thisObj.javaClass != other?.javaClass)
return false
// cast is safe, because this is T and other's class was checked for equality with T
@Suppress("UNCHECKED_CAST")
other as T
return properties.all { thisObj.it() == other.it() }
}
fun hc(): Int {
// Fast implementation without collection copies, based on java.util.Arrays.hashCode()
var result = 1
for (element in properties) {
val value = thisObj.element()
result = 31 * result + (value?.hashCode() ?: 0)
}
return result
}
@Deprecated("Not accessible; use eq()", ReplaceWith("this.eq(other)"), DeprecationLevel.ERROR)
override fun equals(other: Any?): Boolean =
throw UnsupportedOperationException("Stem.equals() not supported; call eq() instead")
@Deprecated("Not accessible; use hc()", ReplaceWith("this.hc(other)"), DeprecationLevel.ERROR)
override fun hashCode(): Int =
throw UnsupportedOperationException("Stem.hashCode() not supported; call hc() instead")
}
如果您想知道最后两种方法,它们的存在会使以下错误代码在编译时失败:
override fun equals(other: Any?) = stem.equals(other)
override fun hashCode() = stem.hashCode()
如果这些方法是隐式或通过反射调用的,则该异常仅是一个后备;必要时可以争论。
当然,Stem
类可以进一步扩展为包括toString()
等的自动生成。
答案 7 :(得分:0)
您可以通过定义枚举来创建一个annotation来表示排除属性为@ExcludeToString
或使用@ToString(Type.EXCLUDE)
参数。
然后使用reflection格式化getToString()
的值。
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class ExcludeToString
data class Test(
var a: String = "Test A",
@ExcludeToString var b: String = "Test B"
) {
override fun toString(): String {
return ExcludeToStringUtils.getToString(this)
}
}
object ExcludeToStringUtils {
fun getToString(obj: Any): String {
val toString = LinkedList<String>()
getFieldsNotExludeToString(obj).forEach { prop ->
prop.isAccessible = true
toString += "${prop.name}=" + prop.get(obj)?.toString()?.trim()
}
return "${obj.javaClass.simpleName}=[${toString.joinToString(", ")}]"
}
private fun getFieldsNotExludeToString(obj: Any): List<Field> {
val declaredFields = obj::class.java.declaredFields
return declaredFields.filterNot { field ->
isFieldWithExludeToString(field)
}
}
private fun isFieldWithExludeToString(field: Field): Boolean {
field.annotations.forEach {
if (it.annotationClass == ExcludeToString::class) {
return true
}
}
return false
}
}
GL
答案 8 :(得分:0)
更简单、更快,看看那里,或进入 Kotlin 文档。 https://discuss.kotlinlang.org/t/ignoring-certain-properties-when-generating-equals-hashcode-etc/2715/2 只考虑主构造函数内的字段来构建自动访问方法,如 equals 等。把无意义的东西放在外面。