coerceIn所在位置是kotlin.ranges.coerceIn,其相关用法介绍如下。

用法一

fun <T : Comparable<T>> T.coerceIn(
    minimumValue: T?, 
    maximumValue: T?
): T

确保此值位于指定范围内 minimumValue .. maximumValue

例子:

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main(args: Array<String>) {
//sampleStart
val workingDays = DayOfWeek.MONDAY..DayOfWeek.FRIDAY
println(DayOfWeek.WEDNESDAY.coerceIn(workingDays)) // WEDNESDAY
println(DayOfWeek.SATURDAY.coerceIn(workingDays)) // FRIDAY

println(DayOfWeek.FRIDAY.coerceIn(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)) // SATURDAY
//sampleEnd
}

输出:

WEDNESDAY
FRIDAY
SATURDAY

如果它在范围内,则返回此值,如果此值小于 minimumValue,则返回 minimumValue,如果此值大于 maximumValue,则返回 maximumValue

用法二

fun Byte.coerceIn(
    minimumValue: Byte, 
    maximumValue: Byte
): Byte
fun Short.coerceIn(
    minimumValue: Short, 
    maximumValue: Short
): Short
fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int
fun Long.coerceIn(
    minimumValue: Long, 
    maximumValue: Long
): Long
fun Float.coerceIn(
    minimumValue: Float, 
    maximumValue: Float
): Float
fun Double.coerceIn(
    minimumValue: Double, 
    maximumValue: Double
): Double

确保此值位于指定范围内 minimumValue .. maximumValue

例子:

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main(args: Array<String>) {
//sampleStart
println(10.coerceIn(1, 100)) // 10
println(10.coerceIn(1..100)) // 10
println(0.coerceIn(1, 100)) // 1
println(500.coerceIn(1, 100)) // 100
// 10.coerceIn(100, 0) // will fail with IllegalArgumentException
//sampleEnd
}

输出:

10
10
1
100

如果它在范围内,则返回此值,如果此值小于 minimumValue,则返回 minimumValue,如果此值大于 maximumValue,则返回 maximumValue

用法三

fun <T : Comparable<T>> T.coerceIn(
    range: ClosedFloatingPointRange<T>
): T

确保此值位于指定的 range 中。

例子:

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main(args: Array<String>) {
//sampleStart
println(10.1.coerceIn(1.0..10.0)) // 10.0
println(9.9.coerceIn(1.0..10.0)) // 9.9

// 9.9.coerceIn(1.0..Double.NaN) // will fail with IllegalArgumentException
//sampleEnd
}

输出:

10.0
9.9

如果它在 range 中,则返回此值,如果此值小于 range.start ,则返回 range.start ,如果此值大于 range.endInclusive ,则返回 range.endInclusive

用法四

fun <T : Comparable<T>> T.coerceIn(range: ClosedRange<T>): T

确保此值位于指定的 range 中。

例子:

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main(args: Array<String>) {
//sampleStart
val workingDays = DayOfWeek.MONDAY..DayOfWeek.FRIDAY
println(DayOfWeek.WEDNESDAY.coerceIn(workingDays)) // WEDNESDAY
println(DayOfWeek.SATURDAY.coerceIn(workingDays)) // FRIDAY

println(DayOfWeek.FRIDAY.coerceIn(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)) // SATURDAY
//sampleEnd
}

输出:

WEDNESDAY
FRIDAY
SATURDAY

如果它在 range 中,则返回此值,如果此值小于 range.start ,则返回 range.start ,如果此值大于 range.endInclusive ,则返回 range.endInclusive

用法五

fun Int.coerceIn(range: ClosedRange<Int>): Int
fun Long.coerceIn(range: ClosedRange<Long>): Long

确保此值位于指定的 range 中。

例子:

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main(args: Array<String>) {
//sampleStart
println(10.coerceIn(1, 100)) // 10
println(10.coerceIn(1..100)) // 10
println(0.coerceIn(1, 100)) // 1
println(500.coerceIn(1, 100)) // 100
// 10.coerceIn(100, 0) // will fail with IllegalArgumentException
//sampleEnd
}

输出:

10
10
1
100

如果它在 range 中,则返回此值,如果此值小于 range.start ,则返回 range.start ,如果此值大于 range.endInclusive ,则返回 range.endInclusive

用法六

fun UInt.coerceIn(
    minimumValue: UInt, 
    maximumValue: UInt
): UInt
fun ULong.coerceIn(
    minimumValue: ULong, 
    maximumValue: ULong
): ULong
fun UByte.coerceIn(
    minimumValue: UByte, 
    maximumValue: UByte
): UByte
fun UShort.coerceIn(
    minimumValue: UShort, 
    maximumValue: UShort
): UShort

确保此值位于指定范围内 minimumValue .. maximumValue

例子:

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main(args: Array<String>) {
//sampleStart
println(10u.coerceIn(1u, 100u)) // 10
println(10u.coerceIn(1u..100u)) // 10
println(0u.coerceIn(1u, 100u)) // 1
println(500u.coerceIn(1u, 100u)) // 100
// 10u.coerceIn(100u, 0u) // will fail with IllegalArgumentException
//sampleEnd
}

输出:

10
10
1
100

如果它在范围内,则返回此值,如果此值小于 minimumValue,则返回 minimumValue,如果此值大于 maximumValue,则返回 maximumValue

用法七

fun UInt.coerceIn(range: ClosedRange<UInt>): UInt
fun ULong.coerceIn(range: ClosedRange<ULong>): ULong

确保此值位于指定的 range 中。

例子:

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main(args: Array<String>) {
//sampleStart
println(10u.coerceIn(1u, 100u)) // 10
println(10u.coerceIn(1u..100u)) // 10
println(0u.coerceIn(1u, 100u)) // 1
println(500u.coerceIn(1u, 100u)) // 100
// 10u.coerceIn(100u, 0u) // will fail with IllegalArgumentException
//sampleEnd
}

输出:

10
10
1
100

如果它在 range 中,则返回此值,如果此值小于 range.start ,则返回 range.start ,如果此值大于 range.endInclusive ,则返回 range.endInclusive