Reimplement coerceIn to range with new range functions: lessThanOrEquals.
This commit is contained in:
@@ -866,30 +866,65 @@ public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double {
|
||||
/**
|
||||
* Ensures that this value lies in the specified [range].
|
||||
*
|
||||
* @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end.
|
||||
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||
*/
|
||||
public fun <T: Comparable<T>> T.coerceIn(range: ClosedComparableRange<T>): T {
|
||||
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.")
|
||||
return when {
|
||||
!range.lessThanOrEquals(range.start, this) -> range.start
|
||||
!range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that this value lies in the specified [range].
|
||||
*
|
||||
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||
*/
|
||||
public fun <T: Comparable<T>> T.coerceIn(range: ClosedRange<T>): T {
|
||||
if (range is ClosedComparableRange) {
|
||||
return this.coerceIn<T>(range)
|
||||
}
|
||||
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.")
|
||||
return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this
|
||||
return when {
|
||||
this < range.start -> range.start
|
||||
this > range.endInclusive -> range.endInclusive
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that this value lies in the specified [range].
|
||||
*
|
||||
* @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end.
|
||||
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||
*/
|
||||
public fun Int.coerceIn(range: ClosedRange<Int>): Int {
|
||||
if (range is ClosedComparableRange) {
|
||||
return this.coerceIn<Int>(range)
|
||||
}
|
||||
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.")
|
||||
return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this
|
||||
return when {
|
||||
this < range.start -> range.start
|
||||
this > range.endInclusive -> range.endInclusive
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that this value lies in the specified [range].
|
||||
*
|
||||
* @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end.
|
||||
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||
*/
|
||||
public fun Long.coerceIn(range: ClosedRange<Long>): Long {
|
||||
if (range is ClosedComparableRange) {
|
||||
return this.coerceIn<Long>(range)
|
||||
}
|
||||
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.")
|
||||
return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this
|
||||
return when {
|
||||
this < range.start -> range.start
|
||||
this > range.endInclusive -> range.endInclusive
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,8 +84,8 @@ class CoercionTest {
|
||||
|
||||
assertFails { 1.0.coerceIn(1.0, 0.0) }
|
||||
assertFails { 1.0.coerceIn(1.0..0.0) }
|
||||
assertTrue(0.0 == 0.0.coerceIn(0.0, -0.0))
|
||||
assertTrue(0.0 == 0.0.coerceIn(0.0..-0.0))
|
||||
assertTrue(0.0.equals(0.0.coerceIn(0.0, -0.0)))
|
||||
assertTrue((-0.0).equals((-0.0).coerceIn(0.0..-0.0)))
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1904,6 +1904,7 @@ public final class kotlin/ranges/RangesKt {
|
||||
public static final fun coerceIn (JJJ)J
|
||||
public static final fun coerceIn (JLkotlin/ranges/ClosedRange;)J
|
||||
public static final fun coerceIn (Ljava/lang/Comparable;Ljava/lang/Comparable;Ljava/lang/Comparable;)Ljava/lang/Comparable;
|
||||
public static final fun coerceIn (Ljava/lang/Comparable;Lkotlin/ranges/ClosedComparableRange;)Ljava/lang/Comparable;
|
||||
public static final fun coerceIn (Ljava/lang/Comparable;Lkotlin/ranges/ClosedRange;)Ljava/lang/Comparable;
|
||||
public static final fun coerceIn (SSS)S
|
||||
public static final fun doubleRangeContains (Lkotlin/ranges/ClosedRange;B)Z
|
||||
|
||||
@@ -56,13 +56,57 @@ fun comparables(): List<GenericFunction> {
|
||||
"""
|
||||
Ensures that this value lies in the specified [range].
|
||||
|
||||
@return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end.
|
||||
@return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||
"""
|
||||
}
|
||||
body {
|
||||
body(Generic) {
|
||||
"""
|
||||
if (range is ClosedComparableRange) {
|
||||
return this.coerceIn<T>(range)
|
||||
}
|
||||
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
|
||||
return when {
|
||||
this < range.start -> range.start
|
||||
this > range.endInclusive -> range.endInclusive
|
||||
else -> this
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(Primitives) {
|
||||
"""
|
||||
if (range is ClosedComparableRange) {
|
||||
return this.coerceIn<T>(range)
|
||||
}
|
||||
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
|
||||
return when {
|
||||
this < range.start -> range.start
|
||||
this > range.endInclusive -> range.endInclusive
|
||||
else -> this
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("coerceIn(range: ClosedComparableRange<T>)") {
|
||||
sourceFile(SourceFile.Ranges)
|
||||
only(Generic)
|
||||
returns("SELF")
|
||||
typeParam("T: Comparable<T>")
|
||||
doc {
|
||||
"""
|
||||
Ensures that this value lies in the specified [range].
|
||||
|
||||
@return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||
"""
|
||||
}
|
||||
body(Generic) {
|
||||
"""
|
||||
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
|
||||
return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this
|
||||
return when {
|
||||
!range.lessThanOrEquals(range.start, this) -> range.start
|
||||
!range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive
|
||||
else -> this
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user