coerce In
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.
Samples
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
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")
//sampleEnd
}
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.
Samples
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
assertPrints(10.coerceIn(1, 100), "10")
assertPrints(10.coerceIn(1..100), "10")
assertPrints(0.coerceIn(1, 100), "1")
assertPrints(500.coerceIn(1, 100), "100")
assertFailsWith<IllegalArgumentException> {
10.coerceIn(100, 0)
}
//sampleEnd
}
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
.
Since Kotlin
Samples
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
assertPrints(10.1.coerceIn(1.0..10.0), "10.0")
assertPrints(9.9.coerceIn(1.0..10.0), "9.9")
assertFailsWith<IllegalArgumentException> { 9.9.coerceIn(1.0..Double.NaN) }
//sampleEnd
}
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
.
Samples
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
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")
//sampleEnd
}
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
.
Samples
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
assertPrints(10.coerceIn(1, 100), "10")
assertPrints(10.coerceIn(1..100), "10")
assertPrints(0.coerceIn(1, 100), "1")
assertPrints(500.coerceIn(1, 100), "100")
assertFailsWith<IllegalArgumentException> {
10.coerceIn(100, 0)
}
//sampleEnd
}
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.
Since Kotlin
Samples
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
assertPrints(10u.coerceIn(1u, 100u), "10")
assertPrints(10u.coerceIn(1u..100u), "10")
assertPrints(0u.coerceIn(1u, 100u), "1")
assertPrints(500u.coerceIn(1u, 100u), "100")
assertFailsWith<IllegalArgumentException> {
10u.coerceIn(100u, 0u)
}
//sampleEnd
}
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
.
Since Kotlin
Samples
import samples.*
import java.time.DayOfWeek
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
assertPrints(10u.coerceIn(1u, 100u), "10")
assertPrints(10u.coerceIn(1u..100u), "10")
assertPrints(0u.coerceIn(1u, 100u), "1")
assertPrints(500u.coerceIn(1u, 100u), "100")
assertFailsWith<IllegalArgumentException> {
10u.coerceIn(100u, 0u)
}
//sampleEnd
}