From 73c97abedd3e27e05177c96955d7d4e78a5f7b94 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Mon, 27 Nov 2023 22:40:00 +0100 Subject: [PATCH] chore: Lint code --- .../kotlin/app/revanced/library/PatchUtils.kt | 61 +++++++++-------- .../app/revanced/library/PatchUtilsTest.kt | 66 ++++++++++--------- 2 files changed, 66 insertions(+), 61 deletions(-) diff --git a/src/main/kotlin/app/revanced/library/PatchUtils.kt b/src/main/kotlin/app/revanced/library/PatchUtils.kt index 4df1bc6..1cc700b 100644 --- a/src/main/kotlin/app/revanced/library/PatchUtils.kt +++ b/src/main/kotlin/app/revanced/library/PatchUtils.kt @@ -26,7 +26,7 @@ object PatchUtils { "Use getMostCommonCompatibleVersions instead.", ReplaceWith( "getMostCommonCompatibleVersions(patches, setOf(packageName))" + - ".entries.firstOrNull()?.value?.keys?.firstOrNull()", + ".entries.firstOrNull()?.value?.keys?.firstOrNull()", ), ) fun getMostCommonCompatibleVersion( @@ -56,35 +56,38 @@ object PatchUtils { patches: PatchSet, packageNames: Set? = null, countUnusedPatches: Boolean = false, - ): PackageNameMap = buildMap { - fun filterWantedPackages(compatiblePackages: Iterable): Iterable { - val wantedPackages = packageNames?.toHashSet() ?: return compatiblePackages - return compatiblePackages.filter { it.name in wantedPackages } - } - - patches - .filter { it.use || countUnusedPatches } - .flatMap { it.compatiblePackages ?: emptyList() } - .let(::filterWantedPackages) - .forEach { compatiblePackage -> - if (compatiblePackage.versions?.isEmpty() == true) - return@forEach - - val versionMap = getOrPut(compatiblePackage.name) { linkedMapOf() } - - compatiblePackage.versions?.let { versions -> - versions.forEach { version -> - versionMap[version] = versionMap.getOrDefault(version, 0) + 1 - } - } + ): PackageNameMap = + buildMap { + fun filterWantedPackages(compatiblePackages: Iterable): Iterable { + val wantedPackages = packageNames?.toHashSet() ?: return compatiblePackages + return compatiblePackages.filter { it.name in wantedPackages } } - // Sort the version maps by the most common version. - forEach { (packageName, versionMap) -> - this[packageName] = versionMap - .asIterable() - .sortedWith(compareByDescending { it.value }) - .associate { it.key to it.value } as VersionMap + patches + .filter { it.use || countUnusedPatches } + .flatMap { it.compatiblePackages ?: emptyList() } + .let(::filterWantedPackages) + .forEach { compatiblePackage -> + if (compatiblePackage.versions?.isEmpty() == true) { + return@forEach + } + + val versionMap = getOrPut(compatiblePackage.name) { linkedMapOf() } + + compatiblePackage.versions?.let { versions -> + versions.forEach { version -> + versionMap[version] = versionMap.getOrDefault(version, 0) + 1 + } + } + } + + // Sort the version maps by the most common version. + forEach { (packageName, versionMap) -> + this[packageName] = + versionMap + .asIterable() + .sortedWith(compareByDescending { it.value }) + .associate { it.key to it.value } as VersionMap + } } - } } diff --git a/src/test/kotlin/app/revanced/library/PatchUtilsTest.kt b/src/test/kotlin/app/revanced/library/PatchUtilsTest.kt index 5c260c2..fc635cf 100644 --- a/src/test/kotlin/app/revanced/library/PatchUtilsTest.kt +++ b/src/test/kotlin/app/revanced/library/PatchUtilsTest.kt @@ -53,30 +53,32 @@ internal object PatchUtilsTest { @Test fun `common versions correctly ordered for each package`() { - fun assertEqualsExpected(compatiblePackageNames: Set?) = assertEqualsVersions( - expected = - mapOf( - "some.package" to linkedMapOf("a" to 3, "b" to 2, "c" to 1), - "some.other.package" to linkedMapOf("b" to 3, "c" to 2, "d" to 1), - "some.other.other.package" to linkedMapOf("a" to 1, "b" to 1), - "some.other.other.other.package" to linkedMapOf(), - ), - patches, - compatiblePackageNames, - countUnusedPatches = true, + fun assertEqualsExpected(compatiblePackageNames: Set?) = + assertEqualsVersions( + expected = + mapOf( + "some.package" to linkedMapOf("a" to 3, "b" to 2, "c" to 1), + "some.other.package" to linkedMapOf("b" to 3, "c" to 2, "d" to 1), + "some.other.other.package" to linkedMapOf("a" to 1, "b" to 1), + "some.other.other.other.package" to linkedMapOf(), + ), + patches, + compatiblePackageNames, + countUnusedPatches = true, + ) + + assertEqualsExpected( + compatiblePackageNames = + setOf( + "some.package", + "some.other.package", + "some.other.other.package", + "some.other.other.other.package", + ), ) assertEqualsExpected( - compatiblePackageNames = setOf( - "some.package", - "some.other.package", - "some.other.other.package", - "some.other.other.other.package", - ), - ) - - assertEqualsExpected( - compatiblePackageNames = null + compatiblePackageNames = null, ) } @@ -84,19 +86,19 @@ internal object PatchUtilsTest { fun `common versions correctly ordered for each package without counting unused patches`() { assertEqualsVersions( expected = - mapOf( - "some.package" to linkedMapOf("a" to 1), - "some.other.package" to linkedMapOf("b" to 2, "c" to 2, "d" to 1), - "some.other.other.package" to linkedMapOf("a" to 1, "b" to 1), - ), + mapOf( + "some.package" to linkedMapOf("a" to 1), + "some.other.package" to linkedMapOf("b" to 2, "c" to 2, "d" to 1), + "some.other.other.package" to linkedMapOf("a" to 1, "b" to 1), + ), patches, compatiblePackageNames = - setOf( - "some.package", - "some.other.package", - "some.other.other.package", - "some.other.other.other.package", - ), + setOf( + "some.package", + "some.other.package", + "some.other.other.package", + "some.other.other.other.package", + ), countUnusedPatches = false, ) }