Add samples for coerceAtLeast

#KT-20357
This commit is contained in:
shiraji
2018-01-05 11:39:29 +09:00
committed by Ilya Gorbunov
parent 1db0e5c23e
commit db607e231c
5 changed files with 51 additions and 0 deletions
@@ -652,6 +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
*/
public fun <T: Comparable<T>> T.coerceAtLeast(minimumValue: T): T {
return if (this < minimumValue) minimumValue else this
@@ -661,6 +662,7 @@ public 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.
* @sample samples.comparisons.ComparableOps.coerceAtLeast
*/
public fun Byte.coerceAtLeast(minimumValue: Byte): Byte {
return if (this < minimumValue) minimumValue else this
@@ -670,6 +672,7 @@ public fun Byte.coerceAtLeast(minimumValue: Byte): Byte {
* 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
*/
public fun Short.coerceAtLeast(minimumValue: Short): Short {
return if (this < minimumValue) minimumValue else this
@@ -679,6 +682,7 @@ public fun Short.coerceAtLeast(minimumValue: Short): Short {
* 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
*/
public fun Int.coerceAtLeast(minimumValue: Int): Int {
return if (this < minimumValue) minimumValue else this
@@ -688,6 +692,7 @@ public fun Int.coerceAtLeast(minimumValue: Int): Int {
* 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
*/
public fun Long.coerceAtLeast(minimumValue: Long): Long {
return if (this < minimumValue) minimumValue else this
@@ -697,6 +702,7 @@ public fun Long.coerceAtLeast(minimumValue: Long): Long {
* 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
*/
public fun Float.coerceAtLeast(minimumValue: Float): Float {
return if (this < minimumValue) minimumValue else this
@@ -706,6 +712,7 @@ public fun Float.coerceAtLeast(minimumValue: Float): Float {
* 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
*/
public fun Double.coerceAtLeast(minimumValue: Double): Double {
return if (this < minimumValue) minimumValue else this
@@ -472,6 +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
*/
public expect fun <T: Comparable<T>> T.coerceAtLeast(minimumValue: T): T
@@ -479,6 +480,7 @@ public expect 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.
* @sample samples.comparisons.ComparableOps.coerceAtLeast
*/
public expect fun Byte.coerceAtLeast(minimumValue: Byte): Byte
@@ -486,6 +488,7 @@ public expect fun Byte.coerceAtLeast(minimumValue: Byte): Byte
* 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
*/
public expect fun Short.coerceAtLeast(minimumValue: Short): Short
@@ -493,6 +496,7 @@ public expect fun Short.coerceAtLeast(minimumValue: Short): Short
* 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
*/
public expect fun Int.coerceAtLeast(minimumValue: Int): Int
@@ -500,6 +504,7 @@ public expect fun Int.coerceAtLeast(minimumValue: Int): Int
* 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
*/
public expect fun Long.coerceAtLeast(minimumValue: Long): Long
@@ -507,6 +512,7 @@ public expect fun Long.coerceAtLeast(minimumValue: Long): Long
* 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
*/
public expect fun Float.coerceAtLeast(minimumValue: Float): Float
@@ -514,6 +520,7 @@ public expect fun Float.coerceAtLeast(minimumValue: Float): Float
* 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
*/
public expect fun Double.coerceAtLeast(minimumValue: Double): Double
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package samples.comparisons
import samples.*
class ComparableOps {
@Sample
fun coerceAtLeast() {
assertPrints(10.coerceAtLeast(5), "10")
assertPrints(10.coerceAtLeast(20), "20")
}
}
@@ -652,6 +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
*/
public fun <T: Comparable<T>> T.coerceAtLeast(minimumValue: T): T {
return if (this < minimumValue) minimumValue else this
@@ -661,6 +662,7 @@ public 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.
* @sample samples.comparisons.ComparableOps.coerceAtLeast
*/
public fun Byte.coerceAtLeast(minimumValue: Byte): Byte {
return if (this < minimumValue) minimumValue else this
@@ -670,6 +672,7 @@ public fun Byte.coerceAtLeast(minimumValue: Byte): Byte {
* 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
*/
public fun Short.coerceAtLeast(minimumValue: Short): Short {
return if (this < minimumValue) minimumValue else this
@@ -679,6 +682,7 @@ public fun Short.coerceAtLeast(minimumValue: Short): Short {
* 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
*/
public fun Int.coerceAtLeast(minimumValue: Int): Int {
return if (this < minimumValue) minimumValue else this
@@ -688,6 +692,7 @@ public fun Int.coerceAtLeast(minimumValue: Int): Int {
* 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
*/
public fun Long.coerceAtLeast(minimumValue: Long): Long {
return if (this < minimumValue) minimumValue else this
@@ -697,6 +702,7 @@ public fun Long.coerceAtLeast(minimumValue: Long): Long {
* 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
*/
public fun Float.coerceAtLeast(minimumValue: Float): Float {
return if (this < minimumValue) minimumValue else this
@@ -706,6 +712,7 @@ public fun Float.coerceAtLeast(minimumValue: Float): Float {
* 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
*/
public fun Double.coerceAtLeast(minimumValue: Double): Double {
return if (this < minimumValue) minimumValue else this
@@ -36,6 +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
"""
}
body {