All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.serenitybdd.screenplay.ensure.collections.CollectionsComparison.kt Maven / Gradle / Ivy

package net.serenitybdd.screenplay.ensure.collections

/*

 */
class CollectionsComparison(val comparator: Comparator? = null) {
    fun areEqual(actual: Collection?, expected: Collection?): Boolean {
        if (actual == null && expected == null) return true
        if (actual == null || expected == null) return false

        return compareElementsOf(actual, expected)
    }

    private fun compareElementsOf(actual: Collection, expected: Collection): Boolean {
        return if (comparator == null) actual == expected else elementsAreIdenticalUsingComparatorIn(actual, expected)
    }

    private fun elementsAreIdenticalUsingComparatorIn(actual: Collection, expected: Collection): Boolean {
        if (actual.size != expected.size) return false

        actual.forEachIndexed { i, value ->
            if (!(isSame(value, expected.elementAt(i)))) return false
        }
        return true
    }

    fun isSame(actual: A, expected: A): Boolean {
        if (actual == null && expected == null) return true
        if (actual == null || expected == null) return false

        return if (comparator == null) actual == expected else (comparator.compare(actual, expected) == 0)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy