Add samples for coerceIn, coerceAtLeast, coerceAtMost for comparable types

#KT-20357
This commit is contained in:
Ilya Gorbunov
2018-01-10 17:46:52 +03:00
parent 17573a3c21
commit 8fc83e3ff5
5 changed files with 57 additions and 12 deletions
@@ -652,7 +652,7 @@ public infix fun Short.until(to: Short): IntRange {
* Ensures that this value is not less than the specified [minimumValue].
*
* @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise.
* @sample samples.comparisons.ComparableOps.coerceAtLeast
* @sample samples.comparisons.ComparableOps.coerceAtLeastComparable
*/
public fun <T: Comparable<T>> T.coerceAtLeast(minimumValue: T): T {
return if (this < minimumValue) minimumValue else this
@@ -722,7 +722,7 @@ public fun Double.coerceAtLeast(minimumValue: Double): Double {
* Ensures that this value is not greater than the specified [maximumValue].
*
* @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise.
* @sample samples.comparisons.ComparableOps.coerceAtMost
* @sample samples.comparisons.ComparableOps.coerceAtMostComparable
*/
public fun <T: Comparable<T>> T.coerceAtMost(maximumValue: T): T {
return if (this > maximumValue) maximumValue else this
@@ -792,6 +792,7 @@ public fun Double.coerceAtMost(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].
* @sample samples.comparisons.ComparableOps.coerceInComparable
*/
public fun <T: Comparable<T>> T.coerceIn(minimumValue: T?, maximumValue: T?): T {
if (minimumValue !== null && maximumValue !== null) {
@@ -810,6 +811,7 @@ public fun <T: Comparable<T>> 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -822,6 +824,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -834,6 +837,7 @@ public fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short {
* 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -846,6 +850,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -858,6 +863,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -870,6 +876,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -899,7 +906,7 @@ public fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T
* 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`.
* @sample samples.comparisons.ComparableOps.coerceIn
* @sample samples.comparisons.ComparableOps.coerceInComparable
*/
public fun <T: Comparable<T>> T.coerceIn(range: ClosedRange<T>): T {
if (range is ClosedFloatingPointRange) {
@@ -472,7 +472,7 @@ public expect infix fun Short.until(to: Short): IntRange
* Ensures that this value is not less than the specified [minimumValue].
*
* @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise.
* @sample samples.comparisons.ComparableOps.coerceAtLeast
* @sample samples.comparisons.ComparableOps.coerceAtLeastComparable
*/
public expect fun <T: Comparable<T>> T.coerceAtLeast(minimumValue: T): T
@@ -528,7 +528,7 @@ public expect fun Double.coerceAtLeast(minimumValue: Double): Double
* Ensures that this value is not greater than the specified [maximumValue].
*
* @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise.
* @sample samples.comparisons.ComparableOps.coerceAtMost
* @sample samples.comparisons.ComparableOps.coerceAtMostComparable
*/
public expect fun <T: Comparable<T>> T.coerceAtMost(maximumValue: T): T
@@ -584,6 +584,7 @@ public expect fun Double.coerceAtMost(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].
* @sample samples.comparisons.ComparableOps.coerceInComparable
*/
public expect fun <T: Comparable<T>> T.coerceIn(minimumValue: T?, maximumValue: T?): T
@@ -591,6 +592,7 @@ public expect fun <T: Comparable<T>> T.coerceIn(minimumValue: T?, maximumValue:
* 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
public expect fun Byte.coerceIn(minimumValue: Byte, maximumValue: Byte): Byte
@@ -598,6 +600,7 @@ public expect 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
public expect fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short
@@ -605,6 +608,7 @@ public expect fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Shor
* 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
public expect fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int
@@ -612,6 +616,7 @@ public expect 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
public expect fun Long.coerceIn(minimumValue: Long, maximumValue: Long): Long
@@ -619,6 +624,7 @@ public expect 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
public expect fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Float
@@ -626,6 +632,7 @@ public expect fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Floa
* 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
public expect fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double
@@ -641,7 +648,7 @@ public expect fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<
* 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`.
* @sample samples.comparisons.ComparableOps.coerceIn
* @sample samples.comparisons.ComparableOps.coerceInComparable
*/
public expect fun <T: Comparable<T>> T.coerceIn(range: ClosedRange<T>): T
@@ -17,6 +17,7 @@
package samples.comparisons
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
class ComparableOps {
@@ -27,12 +28,25 @@ class ComparableOps {
assertPrints(10.coerceAtLeast(20), "20")
}
@Sample
fun coerceAtLeastComparable() {
assertPrints(DayOfWeek.WEDNESDAY.coerceAtLeast(DayOfWeek.MONDAY), "WEDNESDAY")
assertPrints(DayOfWeek.WEDNESDAY.coerceAtLeast(DayOfWeek.FRIDAY), "FRIDAY")
}
@Sample
fun coerceAtMost() {
assertPrints(10.coerceAtMost(5), "5")
assertPrints(10.coerceAtMost(20), "10")
}
@Sample
fun coerceAtMostComparable() {
assertPrints(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.SATURDAY), "FRIDAY")
assertPrints(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.WEDNESDAY), "WEDNESDAY")
}
@Sample
fun coerceIn() {
assertPrints(10.coerceIn(1, 100), "10")
@@ -43,4 +57,13 @@ class ComparableOps {
10.coerceIn(100, 0)
}
}
@Sample
fun coerceInComparable() {
val workingDays = DayOfWeek.MONDAY..DayOfWeek.FRIDAY
assertPrints(DayOfWeek.WEDNESDAY.coerceIn(workingDays), "WEDNESDAY")
assertPrints(DayOfWeek.SATURDAY.coerceIn(workingDays), "FRIDAY")
assertPrints(DayOfWeek.FRIDAY.coerceIn(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY), "SATURDAY")
}
}
+10 -3
View File
@@ -652,7 +652,7 @@ public infix fun Short.until(to: Short): IntRange {
* Ensures that this value is not less than the specified [minimumValue].
*
* @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise.
* @sample samples.comparisons.ComparableOps.coerceAtLeast
* @sample samples.comparisons.ComparableOps.coerceAtLeastComparable
*/
public fun <T: Comparable<T>> T.coerceAtLeast(minimumValue: T): T {
return if (this < minimumValue) minimumValue else this
@@ -722,7 +722,7 @@ public fun Double.coerceAtLeast(minimumValue: Double): Double {
* Ensures that this value is not greater than the specified [maximumValue].
*
* @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise.
* @sample samples.comparisons.ComparableOps.coerceAtMost
* @sample samples.comparisons.ComparableOps.coerceAtMostComparable
*/
public fun <T: Comparable<T>> T.coerceAtMost(maximumValue: T): T {
return if (this > maximumValue) maximumValue else this
@@ -792,6 +792,7 @@ public fun Double.coerceAtMost(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].
* @sample samples.comparisons.ComparableOps.coerceInComparable
*/
public fun <T: Comparable<T>> T.coerceIn(minimumValue: T?, maximumValue: T?): T {
if (minimumValue !== null && maximumValue !== null) {
@@ -810,6 +811,7 @@ public fun <T: Comparable<T>> 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -822,6 +824,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -834,6 +837,7 @@ public fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short {
* 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -846,6 +850,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -858,6 +863,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -870,6 +876,7 @@ 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].
* @sample samples.comparisons.ComparableOps.coerceIn
*/
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.")
@@ -899,7 +906,7 @@ public fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T
* 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`.
* @sample samples.comparisons.ComparableOps.coerceIn
* @sample samples.comparisons.ComparableOps.coerceInComparable
*/
public fun <T: Comparable<T>> T.coerceIn(range: ClosedRange<T>): T {
if (range is ClosedFloatingPointRange) {
@@ -36,7 +36,7 @@ object ComparableOps : TemplateGroupBase() {
Ensures that this value is not less than the specified [minimumValue].
@return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise.
@sample samples.comparisons.ComparableOps.coerceAtLeast
@sample samples.comparisons.ComparableOps.coerceAtLeast${if (f == Generic) "Comparable" else ""}
"""
}
body {
@@ -58,7 +58,7 @@ object ComparableOps : TemplateGroupBase() {
Ensures that this value is not greater than the specified [maximumValue].
@return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise.
@sample samples.comparisons.ComparableOps.coerceAtMost
@sample samples.comparisons.ComparableOps.coerceAtMost${if (f == Generic) "Comparable" else ""}
"""
}
body {
@@ -80,7 +80,7 @@ object ComparableOps : TemplateGroupBase() {
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`.
@sample samples.comparisons.ComparableOps.coerceIn
@sample samples.comparisons.ComparableOps.coerceIn${if (f == Generic) "Comparable" else ""}
"""
}
body(Generic) {
@@ -344,6 +344,7 @@ object ComparableOps : TemplateGroupBase() {
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].
@sample samples.comparisons.ComparableOps.coerceIn${if (f == Generic) "Comparable" else ""}
"""
}
body(Primitives) {