coerceAtLeast

fun <T : Comparable<T>> T.coerceAtLeast(minimumValue: T): T

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.

Samples

import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() { 
   //sampleStart 
   assertPrints(DayOfWeek.WEDNESDAY.coerceAtLeast(DayOfWeek.MONDAY), "WEDNESDAY")
assertPrints(DayOfWeek.WEDNESDAY.coerceAtLeast(DayOfWeek.FRIDAY), "FRIDAY") 
   //sampleEnd
}

fun Byte.coerceAtLeast(minimumValue: Byte): Byte
fun Short.coerceAtLeast(minimumValue: Short): Short
fun Int.coerceAtLeast(minimumValue: Int): Int
fun Long.coerceAtLeast(minimumValue: Long): Long
fun Float.coerceAtLeast(minimumValue: Float): Float
fun Double.coerceAtLeast(minimumValue: Double): Double

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.

Samples

import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() { 
   //sampleStart 
   assertPrints(10.coerceAtLeast(5), "10")
assertPrints(10.coerceAtLeast(20), "20") 
   //sampleEnd
}

fun UInt.coerceAtLeast(minimumValue: UInt): UInt
fun ULong.coerceAtLeast(minimumValue: ULong): ULong
fun UByte.coerceAtLeast(minimumValue: UByte): UByte
fun UShort.coerceAtLeast(minimumValue: UShort): UShort

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.

Since Kotlin

1.5

Samples

import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() { 
   //sampleStart 
   assertPrints(10u.coerceAtLeast(5u), "10")
assertPrints(10u.coerceAtLeast(20u), "20") 
   //sampleEnd
}