Drop Range<T> interface and its extensions.

This commit is contained in:
Ilya Gorbunov
2015-11-27 18:29:23 +03:00
parent face6d449b
commit e8621cb738
5 changed files with 5 additions and 438 deletions
@@ -46,50 +46,6 @@ fun comparables(): List<GenericFunction> {
}
}
templates add f("coerceIn(range: Range<T>)") {
sourceFile(SourceFile.Ranges)
only(Generic, Primitives)
only(numericPrimitives.filter { it.isIntegral() })
typeParam("T: Comparable<T>")
returns("SELF")
deprecate("Range<T> is deprecated. Use ClosedRange<T> instead.")
deprecate(Generic) { forBinaryCompatibility }
doc {
"""
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.end if this value is greater than range.end.
"""
}
body {
"""
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
return if (this < range.start) range.start else if (this > range.end) range.end else this
"""
}
}
templates add f("coerceIn(range: Range<T>)") {
sourceFile(SourceFile.Ranges)
only(Primitives)
only(numericPrimitives.filterNot { it.isIntegral() })
returns("SELF")
deprecate { forBinaryCompatibility } // force use generic overload instead
doc {
"""
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.end if this value is greater than range.end.
"""
}
body {
"""
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.")
return if (this < range.start) range.start else if (this > range.end) range.end else this
"""
}
}
templates add f("coerceIn(range: ClosedRange<T>)") {
sourceFile(SourceFile.Ranges)
only(Primitives, Generic)
@@ -238,27 +238,12 @@ fun ranges(): List<GenericFunction> {
templates addAll integralPermutations.map { until(it.first, it.second) }
templates addAll listOf(PrimitiveType.Byte, PrimitiveType.Short).permutations().map { untilDeprecated(it.first, it.second) }
fun containsDeprecated(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(value: $itemType)") {
operator(true)
deprecate("Range<T> is deprecated. Use ClosedRange<T> instead.")
check(rangeType.isNumeric() == itemType.isNumeric()) { "Required rangeType and itemType both to be numeric or both not, got: $rangeType, $itemType" }
only(Ranges)
onlyPrimitives(Ranges, rangeType)
platformName("${rangeType.name.decapitalize()}RangeContains")
returns("Boolean")
doc { "Checks if the specified [value] belongs to this range." }
body { "return start <= value && value <= end" }
}
fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(value: $itemType)") {
operator(true)
check(rangeType.isNumeric() == itemType.isNumeric()) { "Required rangeType and itemType both to be numeric or both not, got: $rangeType, $itemType" }
only(Ranges)
onlyPrimitives(Ranges, rangeType)
customReceiver("ClosedRange<T>")
platformName("${rangeType.name.decapitalize()}RangeContains")
returns("Boolean")
doc { "Checks if the specified [value] belongs to this range." }
@@ -266,8 +251,7 @@ fun ranges(): List<GenericFunction> {
}
templates addAll numericPermutations.filter { it.first != it.second }
.flatMap { listOf(containsDeprecated(it.first, it.second), contains(it.first, it.second)) }
templates addAll numericPermutations.filter { it.first != it.second }.map { contains(it.first, it.second) }
return templates
@@ -305,7 +305,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
ArraysOfObjects -> "Array<${isAsteriskOrT.replace("T", "out T")}>"
Strings -> "String"
CharSequences -> "CharSequence"
Ranges -> "Range<$isAsteriskOrT>"
Ranges -> "ClosedRange<$isAsteriskOrT>"
ArraysOfPrimitives -> primitive?.let { it.name + "Array" } ?: throw IllegalArgumentException("Primitive array should specify primitive type")
RangesOfPrimitives -> primitive?.let { it.name + "Range" } ?: throw IllegalArgumentException("Primitive range should specify primitive type")
ProgressionsOfPrimitives -> primitive?.let { it.name + "Progression" } ?: throw IllegalArgumentException("Primitive progression should specify primitive type")