maximumValue 보다 작거나 같으면 이 값을 반환하고, 그렇지 않으면 maximumValue 를 반환합니다.
public fun <T : Comparable<T>> T.coerceAtMost(maximumValue: T): T {
return if (this > maximumValue) maximumValue else this
}
println(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.SATURDAY)) // FRIDAY
println(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.WEDNESDAY)) // WEDNESDAY
println(10.coerceAtMost(5)) // 5
println(10.coerceAtMost(20)) // 10
'코틀린 > [Filtering] 필터 작업' 카테고리의 다른 글
[Kotlin][Collection] distinct (0) | 2024.09.05 |
---|---|
[Kotlin][Collection] dropWhile / dropLastWhile (0) | 2024.08.28 |
[Kotlin][Collection] takeWhile / takeLastWhile (0) | 2024.08.28 |