gpt4 book ai didi

java - 有没有一种方法可以有效地比较任意对象的字段

转载 作者:行者123 更新时间:2023-11-30 05:21:37 26 4
gpt4 key购买 nike

目前,如果我需要通过特定字段比较对象,我会实现 DiffEqual 接口(interface)

interface DiffEquals<T> {
fun isItemSame(other: Any?): Boolean
fun isContentSame(other: Any?): Boolean
}

某个对象

data class Book(
val title: String, val amount: String, var isSelected: Boolean = false
) : DiffEquals {

override fun isItemSame(other: Any?): Boolean {
if (other != null && other is Book) {
if (title != other.title) {
return false
}
return true
} else {
return false
}
}

override fun isContentSame(other: Any?): Boolean {
other as Book
if (amount != other.amount) return false
if (isSelected != other.isSelected) return false

return true
}
}

我不喜欢这种方法,因为它需要大量的虚拟代码。我尝试制作Id1、Id2、Id3、Mutable1、Mutable2、Mutable3等注释,但资源消耗很大,因此我不得不回滚到上面的界面。如何实现一种比较指定字段的通用比较机制? (不是标准的 equals 不适合,因为更复杂的对象具有不断变化的字段,但对于相等并不重要,重写 equals 会与虚拟代码产生相同的问题,并且在大多数情况下,我需要完全等于)

最佳答案

您可以创建内联 equalsBy 函数:

@PublishedApi
@Suppress("UNCHECKED_CAST")
internal inline fun <T> T.eqlBy(other: Any?, prop1: T.() -> Any?): Boolean = prop1() == (other as T).prop1()

inline fun <reified T> T.equalsBy(other: Any?, prop1: T.() -> Any?): Boolean =
other is T && eqlBy(other, prop1)

inline fun <reified T> T.equalsBy(other: Any?, prop1: T.() -> Any?, prop2: T.() -> Any?): Boolean =
equalsBy(other, prop1) && eqlBy(other, prop2)

inline fun <reified T> T.equalsBy(other: Any?, prop1: T.() -> Any?, prop2: T.() -> Any?, prop3: T.() -> Any?): Boolean =
equalsBy(other, prop1, prop2) && eqlBy(other, prop3)

像这样使用它们:

data class Book(
val title: String, val amount: String, var isSelected: Boolean = false
) : DiffEquals<Book> {
override fun isItemSame(other: Book) = equalsBy(other) { title }
override fun isContentSame(other: Book) = equalsBy(other, { amount }, { isSelected })
}

关于java - 有没有一种方法可以有效地比较任意对象的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59479947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com