Add samples for range construction operators
This commit is contained in:
committed by
Ilya Gorbunov
parent
31d650a041
commit
4cb2b12f01
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 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.ranges
|
||||||
|
|
||||||
|
import samples.*
|
||||||
|
import java.sql.Date
|
||||||
|
|
||||||
|
class Ranges {
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun rangeFromComparable() {
|
||||||
|
val start = Date.valueOf("2017-01-01")
|
||||||
|
val end = Date.valueOf("2017-12-31")
|
||||||
|
val range = start..end
|
||||||
|
assertPrints(range, "2017-01-01..2017-12-31")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun rangeFromDouble() {
|
||||||
|
val range = 1.0..100.0
|
||||||
|
assertPrints(range, "1.0..100.0")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun rangeFromFloat() {
|
||||||
|
val range = 1f..100f
|
||||||
|
assertPrints(range, "1.0..100.0")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -105,6 +105,7 @@ private class ClosedFloatRange (
|
|||||||
* Creates a range from this [Comparable] value to the specified [that] value.
|
* Creates a range from this [Comparable] value to the specified [that] value.
|
||||||
*
|
*
|
||||||
* This value needs to be smaller than [that] value, otherwise the returned range will be empty.
|
* This value needs to be smaller than [that] value, otherwise the returned range will be empty.
|
||||||
|
* @sample samples.ranges.Ranges.rangeFromComparable
|
||||||
*/
|
*/
|
||||||
public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T> = ComparableRange(this, that)
|
public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T> = ComparableRange(this, that)
|
||||||
|
|
||||||
@@ -112,6 +113,7 @@ public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T> = Comp
|
|||||||
* Creates a range from this [Double] value to the specified [that] value.
|
* Creates a range from this [Double] value to the specified [that] value.
|
||||||
*
|
*
|
||||||
* Numbers are compared with the ends of this range according to IEEE-754.
|
* Numbers are compared with the ends of this range according to IEEE-754.
|
||||||
|
* @sample samples.ranges.Ranges.rangeFromDouble
|
||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
public operator fun Double.rangeTo(that: Double): ClosedFloatingPointRange<Double> = ClosedDoubleRange(this, that)
|
public operator fun Double.rangeTo(that: Double): ClosedFloatingPointRange<Double> = ClosedDoubleRange(this, that)
|
||||||
@@ -120,6 +122,7 @@ public operator fun Double.rangeTo(that: Double): ClosedFloatingPointRange<Doubl
|
|||||||
* Creates a range from this [Float] value to the specified [that] value.
|
* Creates a range from this [Float] value to the specified [that] value.
|
||||||
*
|
*
|
||||||
* Numbers are compared with the ends of this range according to IEEE-754.
|
* Numbers are compared with the ends of this range according to IEEE-754.
|
||||||
|
* @sample samples.ranges.Ranges.rangeFromFloat
|
||||||
*/
|
*/
|
||||||
@JvmVersion
|
@JvmVersion
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
|
|||||||
Reference in New Issue
Block a user