I have a data class TestDataClass that has a custom equals() method, in order to consider the given threshold while comparing the Price property that looks like:
private val THRESHOLD: Double = 1e-7
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as TestDataClass
if (id != other.id) return false
if (kotlin.math.abs(price - other.price) > THRESHOLD) return false
if (curr != other.curr) return false
if (desc != other.desc) return false
if (quantity != other.quantity) return false
return true
}
Then there is an enum class that builds its properties based on the objects of the TestDataClass as:
enum class TestDataClassColumns(private val expr: (TestDataClass) -> Any) {
Price({ it.price }),
Curr({ it.curr }),
Description({ it.desc }),
Quantity({ it.quantity });
}
Furthermore, I have a checkData() method in the enum class TestDataClassColumns that acts is used for the comparison and returns the properties that were not equal:
fun checkData(obj1: TestDataClass?, obj2: TestDataClass?): Boolean = expr(obj1) != expr(obj2)
Null check is there, just removed it from post to have more visibility.
So far, the equals() of TestDataClass works correct, however I have no idea, how to incorporate the THRESHOLD into the checkData() of enum class TestDataClassColumns(). Any ideas how to do it? The overall design should remain the same, meaning that the classes should be there. Here is the link to playground with some examples - Kotlin Playground