Reimplement coerceIn to range with new range functions: lessThanOrEquals.

This commit is contained in:
Ilya Gorbunov
2016-11-30 01:51:52 +03:00
parent d60fc7d9a8
commit ea77673dde
4 changed files with 91 additions and 11 deletions
+41 -6
View File
@@ -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
}
}