diff --git a/libraries/stdlib/src/generated/_Ranges.kt b/libraries/stdlib/src/generated/_Ranges.kt index 46c031acc3f..d2347e1d112 100644 --- a/libraries/stdlib/src/generated/_Ranges.kt +++ b/libraries/stdlib/src/generated/_Ranges.kt @@ -635,7 +635,7 @@ public fun Short.coerceAtLeast(minimumValue: Short): Short { /** * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. */ public fun > T.coerceAtMost(maximumValue: T): T { return if (this > maximumValue) maximumValue else this @@ -643,7 +643,7 @@ public fun > T.coerceAtMost(maximumValue: T): T { /** * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. */ public fun Byte.coerceAtMost(maximumValue: Byte): Byte { return if (this > maximumValue) maximumValue else this @@ -651,7 +651,7 @@ public fun Byte.coerceAtMost(maximumValue: Byte): Byte { /** * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. */ public fun Double.coerceAtMost(maximumValue: Double): Double { return if (this > maximumValue) maximumValue else this @@ -659,7 +659,7 @@ public fun Double.coerceAtMost(maximumValue: Double): Double { /** * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. */ public fun Float.coerceAtMost(maximumValue: Float): Float { return if (this > maximumValue) maximumValue else this @@ -667,7 +667,7 @@ public fun Float.coerceAtMost(maximumValue: Float): Float { /** * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. */ public fun Int.coerceAtMost(maximumValue: Int): Int { return if (this > maximumValue) maximumValue else this @@ -675,7 +675,7 @@ public fun Int.coerceAtMost(maximumValue: Int): Int { /** * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. */ public fun Long.coerceAtMost(maximumValue: Long): Long { return if (this > maximumValue) maximumValue else this @@ -683,7 +683,7 @@ public fun Long.coerceAtMost(maximumValue: Long): Long { /** * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. */ public fun Short.coerceAtMost(maximumValue: Short): Short { return if (this > maximumValue) maximumValue else this @@ -710,16 +710,10 @@ public fun > T.coerceIn(minimumValue: T?, maximumValue: T?): T * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. */ -public fun Byte.coerceIn(minimumValue: Byte?, maximumValue: Byte?): Byte { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } +public fun Byte.coerceIn(minimumValue: Byte, maximumValue: Byte): Byte { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue return this } @@ -727,16 +721,10 @@ public fun Byte.coerceIn(minimumValue: Byte?, maximumValue: Byte?): Byte { * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. */ -public fun Double.coerceIn(minimumValue: Double?, maximumValue: Double?): Double { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } +public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue return this } @@ -744,16 +732,10 @@ public fun Double.coerceIn(minimumValue: Double?, maximumValue: Double?): Double * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. */ -public fun Float.coerceIn(minimumValue: Float?, maximumValue: Float?): Float { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } +public fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Float { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue return this } @@ -761,16 +743,10 @@ public fun Float.coerceIn(minimumValue: Float?, maximumValue: Float?): Float { * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. */ -public fun Int.coerceIn(minimumValue: Int?, maximumValue: Int?): Int { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } +public fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue return this } @@ -778,16 +754,10 @@ public fun Int.coerceIn(minimumValue: Int?, maximumValue: Int?): Int { * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. */ -public fun Long.coerceIn(minimumValue: Long?, maximumValue: Long?): Long { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } +public fun Long.coerceIn(minimumValue: Long, maximumValue: Long): Long { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue return this } @@ -795,16 +765,10 @@ public fun Long.coerceIn(minimumValue: Long?, maximumValue: Long?): Long { * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. */ -public fun Short.coerceIn(minimumValue: Short?, maximumValue: Short?): Short { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } +public fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue return this } diff --git a/libraries/stdlib/test/numbers/CoercionTest.kt b/libraries/stdlib/test/numbers/CoercionTest.kt index 83c46649cac..7fa9362aee1 100644 --- a/libraries/stdlib/test/numbers/CoercionTest.kt +++ b/libraries/stdlib/test/numbers/CoercionTest.kt @@ -89,7 +89,6 @@ class CoercionTest { } @Test - @Suppress("DEPRECATION_ERROR") fun coercionsComparable() { val v = (0..10).map { ComparableNumber(it) } @@ -118,7 +117,7 @@ class CoercionTest { } } -private class ComparableNumber(public val value: Int) : Comparable { +private class ComparableNumber(val value: Int) : Comparable { override fun compareTo(other: ComparableNumber): Int = this.value - other.value override fun toString(): String = "CV$value" } \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt index f3ae590ca5a..5b123665577 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt @@ -36,7 +36,7 @@ fun comparables(): List { """ Ensures that this value is not greater than the specified [maximumValue]. - @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. """ } body { @@ -67,12 +67,13 @@ fun comparables(): List { } } - templates add f("coerceIn(minimumValue: SELF?, maximumValue: SELF?)") { + templates add f("coerceIn(minimumValue: SELF, maximumValue: SELF)") { sourceFile(SourceFile.Ranges) only(Primitives, Generic) only(numericPrimitives) - returns("SELF") + customSignature(Generic) { "coerceIn(minimumValue: SELF?, maximumValue: SELF?)" } typeParam("T: Comparable") + returns("SELF") doc { """ Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -80,7 +81,15 @@ fun comparables(): List { @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. """ } - body { + body(Primitives) { + """ + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum ${'$'}maximumValue is less than minimum ${'$'}minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + return this + """ + } + body(Generic) { """ if (minimumValue !== null && maximumValue !== null) { if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum ${'$'}maximumValue is less than minimum ${'$'}minimumValue.") @@ -94,7 +103,6 @@ fun comparables(): List { return this """ } - } return templates