StdLib deprecations cleanup: length, size, indices and other collection operations.

This commit is contained in:
Ilya Gorbunov
2015-06-25 18:36:52 +03:00
parent 2c31a1a345
commit f3a19ebe11
11 changed files with 42 additions and 42 deletions
@@ -58,7 +58,7 @@ fun ResultSet.getColumnNames() : Array<String> {
* @columnNames you can specify column names to extract otherwise all columns will be extracted * @columnNames you can specify column names to extract otherwise all columns will be extracted
*/ */
fun ResultSet.getValues(columnNames : Array<String> = getColumnNames()) : Array<Any?> { fun ResultSet.getValues(columnNames : Array<String> = getColumnNames()) : Array<Any?> {
return Array<Any?>(columnNames.size, { return Array<Any?>(columnNames.size(), {
this[columnNames[it]] this[columnNames[it]]
}) })
} }
@@ -68,7 +68,7 @@ fun ResultSet.getValues(columnNames : Array<String> = getColumnNames()) : Array<
* @param columnNames you can specify column names to extract otherwise all columns will be extracted * @param columnNames you can specify column names to extract otherwise all columns will be extracted
*/ */
fun ResultSet.getValuesAsMap(columnNames : Array<String> = getColumnNames()) : Map<String, Any?> { fun ResultSet.getValuesAsMap(columnNames : Array<String> = getColumnNames()) : Map<String, Any?> {
val result = java.util.HashMap<String, Any?>(columnNames.size) val result = java.util.HashMap<String, Any?>(columnNames.size())
columnNames.forEach { columnNames.forEach {
result[it] = this[it] result[it] = this[it]
+2 -2
View File
@@ -1064,7 +1064,7 @@ public inline fun <T, C : MutableCollection<in T>> Stream<T>.filterTo(destinatio
* Appends all characters matching the given [predicate] to the given [destination]. * Appends all characters matching the given [predicate] to the given [destination].
*/ */
public inline fun <C : Appendable> String.filterTo(destination: C, predicate: (Char) -> Boolean): C { public inline fun <C : Appendable> String.filterTo(destination: C, predicate: (Char) -> Boolean): C {
for (index in 0..length - 1) { for (index in 0..length() - 1) {
val element = get(index) val element = get(index)
if (predicate(element)) destination.append(element) if (predicate(element)) destination.append(element)
} }
@@ -1819,7 +1819,7 @@ public fun <T> Stream<T>.takeWhile(predicate: (T) -> Boolean): Stream<T> {
* Returns a string containing the first characters that satisfy the given [predicate]. * Returns a string containing the first characters that satisfy the given [predicate].
*/ */
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String { public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0..length - 1) for (index in 0..length() - 1)
if (!predicate(get(index))) { if (!predicate(get(index))) {
return substring(0, index) return substring(0, index)
} }
+10 -10
View File
@@ -190,7 +190,7 @@ public fun CharSequence.iterator(): CharIterator = object : CharIterator() {
public override fun nextChar(): Char = get(index++) public override fun nextChar(): Char = get(index++)
public override fun hasNext(): Boolean = index < length public override fun hasNext(): Boolean = index < length()
} }
/** Returns the string if it is not `null`, or the empty string otherwise. */ /** Returns the string if it is not `null`, or the empty string otherwise. */
@@ -298,7 +298,7 @@ public fun String.substringBefore(delimiter: String, missingDelimiterValue: Stri
*/ */
public fun String.substringAfter(delimiter: Char, missingDelimiterValue: String = this): String { public fun String.substringAfter(delimiter: Char, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter) val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + 1, length) return if (index == -1) missingDelimiterValue else substring(index + 1, length())
} }
/** /**
@@ -307,7 +307,7 @@ public fun String.substringAfter(delimiter: Char, missingDelimiterValue: String
*/ */
public fun String.substringAfter(delimiter: String, missingDelimiterValue: String = this): String { public fun String.substringAfter(delimiter: String, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter) val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + delimiter.length, length) return if (index == -1) missingDelimiterValue else substring(index + delimiter.length(), length())
} }
/** /**
@@ -334,7 +334,7 @@ public fun String.substringBeforeLast(delimiter: String, missingDelimiterValue:
*/ */
public fun String.substringAfterLast(delimiter: Char, missingDelimiterValue: String = this): String { public fun String.substringAfterLast(delimiter: Char, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter) val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + 1, length) return if (index == -1) missingDelimiterValue else substring(index + 1, length())
} }
/** /**
@@ -343,7 +343,7 @@ public fun String.substringAfterLast(delimiter: Char, missingDelimiterValue: Str
*/ */
public fun String.substringAfterLast(delimiter: String, missingDelimiterValue: String = this): String { public fun String.substringAfterLast(delimiter: String, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter) val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + delimiter.length, length) return if (index == -1) missingDelimiterValue else substring(index + delimiter.length(), length())
} }
/** /**
@@ -357,7 +357,7 @@ public fun String.replaceRange(firstIndex: Int, lastIndex: Int, replacement: Str
val sb = StringBuilder() val sb = StringBuilder()
sb.append(this, 0, firstIndex) sb.append(this, 0, firstIndex)
sb.append(replacement) sb.append(replacement)
sb.append(this, lastIndex, length) sb.append(this, lastIndex, length())
return sb.toString() return sb.toString()
} }
@@ -460,7 +460,7 @@ public fun String.replaceBefore(delimiter: String, replacement: String, missingD
*/ */
public fun String.replaceAfter(delimiter: Char, replacement: String, missingDelimiterValue: String = this): String { public fun String.replaceAfter(delimiter: Char, replacement: String, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter) val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length, replacement) return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length(), replacement)
} }
/** /**
@@ -469,7 +469,7 @@ public fun String.replaceAfter(delimiter: Char, replacement: String, missingDeli
*/ */
public fun String.replaceAfter(delimiter: String, replacement: String, missingDelimiterValue: String = this): String { public fun String.replaceAfter(delimiter: String, replacement: String, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter) val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length, length, replacement) return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length(), length(), replacement)
} }
/** /**
@@ -478,7 +478,7 @@ public fun String.replaceAfter(delimiter: String, replacement: String, missingDe
*/ */
public fun String.replaceAfterLast(delimiter: String, replacement: String, missingDelimiterValue: String = this): String { public fun String.replaceAfterLast(delimiter: String, replacement: String, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter) val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length, length, replacement) return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length(), length(), replacement)
} }
/** /**
@@ -487,7 +487,7 @@ public fun String.replaceAfterLast(delimiter: String, replacement: String, missi
*/ */
public fun String.replaceAfterLast(delimiter: Char, replacement: String, missingDelimiterValue: String = this): String { public fun String.replaceAfterLast(delimiter: Char, replacement: String, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter) val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length, replacement) return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length(), replacement)
} }
/** /**
@@ -394,7 +394,7 @@ class CollectionTest {
val indices = data.indices val indices = data.indices
assertEquals(0, indices.start) assertEquals(0, indices.start)
assertEquals(1, indices.end) assertEquals(1, indices.end)
assertEquals(indices, data.size().indices) assertEquals(0..data.size() - 1, indices)
} }
test fun contains() { test fun contains() {
@@ -423,8 +423,8 @@ class CollectionTest {
expect(2000000000000, { listOf(3000000000000, 2000000000000).min() }) expect(2000000000000, { listOf(3000000000000, 2000000000000).min() })
expect('a', { listOf('a', 'b').min() }) expect('a', { listOf('a', 'b').min() })
expect("a", { listOf("a", "b").min() }) expect("a", { listOf("a", "b").min() })
expect(null, { listOf<Int>().asSequence().min<Int>() }) expect(null, { listOf<Int>().asSequence().min() })
expect(2, { listOf(2, 3).asSequence().min<Int>() }) expect(2, { listOf(2, 3).asSequence().min() })
} }
test fun max() { test fun max() {
@@ -434,8 +434,8 @@ class CollectionTest {
expect(3000000000000, { listOf(3000000000000, 2000000000000).max() }) expect(3000000000000, { listOf(3000000000000, 2000000000000).max() })
expect('b', { listOf('a', 'b').max() }) expect('b', { listOf('a', 'b').max() })
expect("b", { listOf("a", "b").max() }) expect("b", { listOf("a", "b").max() })
expect(null, { listOf<Int>().asSequence().max<Int>() }) expect(null, { listOf<Int>().asSequence().max() })
expect(3, { listOf(2, 3).asSequence().max<Int>() }) expect(3, { listOf(2, 3).asSequence().max() })
} }
test fun minBy() { test fun minBy() {
@@ -444,8 +444,8 @@ class CollectionTest {
expect(3, { listOf(2, 3).minBy { -it } }) expect(3, { listOf(2, 3).minBy { -it } })
expect('a', { listOf('a', 'b').minBy { "x$it" } }) expect('a', { listOf('a', 'b').minBy { "x$it" } })
expect("b", { listOf("b", "abc").minBy { it.length() } }) expect("b", { listOf("b", "abc").minBy { it.length() } })
expect(null, { listOf<Int>().asSequence().minBy<Int, Int> { it } }) expect(null, { listOf<Int>().asSequence().minBy { it } })
expect(3, { listOf(2, 3).asSequence().minBy<Int, Int> { -it } }) expect(3, { listOf(2, 3).asSequence().minBy { -it } })
} }
test fun maxBy() { test fun maxBy() {
@@ -454,8 +454,8 @@ class CollectionTest {
expect(2, { listOf(2, 3).maxBy { -it } }) expect(2, { listOf(2, 3).maxBy { -it } })
expect('b', { listOf('a', 'b').maxBy { "x$it" } }) expect('b', { listOf('a', 'b').maxBy { "x$it" } })
expect("abc", { listOf("b", "abc").maxBy { it.length() } }) expect("abc", { listOf("b", "abc").maxBy { it.length() } })
expect(null, { listOf<Int>().asSequence().maxBy<Int, Int> { it } }) expect(null, { listOf<Int>().asSequence().maxBy { it } })
expect(2, { listOf(2, 3).asSequence().maxBy<Int, Int> { -it } }) expect(2, { listOf(2, 3).asSequence().maxBy { -it } })
} }
test fun minByEvaluateOnce() { test fun minByEvaluateOnce() {
@@ -463,7 +463,7 @@ class CollectionTest {
expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } }) expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
assertEquals(5, c) assertEquals(5, c)
c = 0 c = 0
expect(1, { listOf(5, 4, 3, 2, 1).asSequence().minBy<Int, Int> { c++; it * it } }) expect(1, { listOf(5, 4, 3, 2, 1).asSequence().minBy { c++; it * it } })
assertEquals(5, c) assertEquals(5, c)
} }
@@ -472,7 +472,7 @@ class CollectionTest {
expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } }) expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
assertEquals(5, c) assertEquals(5, c)
c = 0 c = 0
expect(5, { listOf(5, 4, 3, 2, 1).asSequence().maxBy<Int, Int> { c++; it * it } }) expect(5, { listOf(5, 4, 3, 2, 1).asSequence().maxBy { c++; it * it } })
assertEquals(5, c) assertEquals(5, c)
} }
+3 -3
View File
@@ -262,12 +262,12 @@ abstract class MapJsTest {
test fun setViaIndexOperators() { test fun setViaIndexOperators() {
val map = HashMap<String, String>() val map = HashMap<String, String>()
assertTrue{ map.empty } assertTrue{ map.isEmpty() }
assertEquals(map.size, 0) assertEquals(map.size(), 0)
map["name"] = "James" map["name"] = "James"
assertTrue{ !map.empty } assertTrue{ !map.isEmpty() }
assertEquals(map.size(), 1) assertEquals(map.size(), 1)
assertEquals("James", map["name"]) assertEquals("James", map["name"])
} }
+2 -2
View File
@@ -272,7 +272,7 @@ class StringJVMTest {
fails { fails {
data.drop(-2) data.drop(-2)
} }
assertEquals("", data.drop(data.length + 5)) assertEquals("", data.drop(data.length() + 5))
} }
test fun takeWhile() { test fun takeWhile() {
@@ -288,7 +288,7 @@ class StringJVMTest {
fails { fails {
data.take(-7) data.take(-7)
} }
assertEquals(data, data.take(data.length + 42)) assertEquals(data, data.take(data.length() + 42))
} }
test fun formatter() { test fun formatter() {
@@ -35,7 +35,7 @@ class Html2CompilerPlugin(private val compilerArguments: KDocArguments) : Doclet
val filePath = file.getPath() val filePath = file.getPath()
for (sourceDirPath in sourceDirPaths) { for (sourceDirPath in sourceDirPaths) {
if (filePath.startsWith(sourceDirPath) && filePath.length() > sourceDirPath.length()) { if (filePath.startsWith(sourceDirPath) && filePath.length() > sourceDirPath.length()) {
val relativePath = filePath.substring(sourceDirPath.length + 1) val relativePath = filePath.substring(sourceDirPath.length() + 1)
return relativePath return relativePath
} }
} }
@@ -196,7 +196,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs
val filePath = file.getPath() val filePath = file.getPath()
for (sourceDirPath in normalizedSourceDirs) { for (sourceDirPath in normalizedSourceDirs) {
if (filePath.startsWith(sourceDirPath) && filePath.length() > sourceDirPath.length()) { if (filePath.startsWith(sourceDirPath) && filePath.length() > sourceDirPath.length()) {
return filePath.substring(sourceDirPath.length + 1) return filePath.substring(sourceDirPath.length() + 1)
} }
} }
throw Exception("$file is not a child of any source roots $normalizedSourceDirs") throw Exception("$file is not a child of any source roots $normalizedSourceDirs")
@@ -483,7 +483,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs
val lines = nodeText.trim().split('\n') val lines = nodeText.trim().split('\n')
// lets remove the /** ... * ... */ tokens // lets remove the /** ... * ... */ tokens
val buffer = StringBuilder() val buffer = StringBuilder()
val last = lines.size - 1 val last = lines.size() - 1
for (i in 0.rangeTo(last)) { for (i in 0.rangeTo(last)) {
var text = lines[i] var text = lines[i]
text = text.trim() text = text.trim()
@@ -518,7 +518,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs
// TODO we could default the test function name to match that of the // TODO we could default the test function name to match that of the
// source code function if folks adopted a convention of naming the test method after the // source code function if folks adopted a convention of naming the test method after the
// method its acting as a demo/test for // method its acting as a demo/test for
if (words.size > 1) { if (words.size() > 1) {
val includeFile = words[0] val includeFile = words[0]
val fnName = words[1] val fnName = words[1]
val content = findFunctionInclude(psiElement, includeFile, fnName) val content = findFunctionInclude(psiElement, includeFile, fnName)
@@ -597,7 +597,7 @@ $highlight"""
break break
} }
var count = 1 var count = 1
for (i in 0.rangeTo(remaining.size - 1)) { for (i in 0.rangeTo(remaining.length() - 1)) {
val ch = remaining[i] val ch = remaining[i]
if (ch == '{') count ++ if (ch == '{') count ++
else if (ch == '}') { else if (ch == '}') {
@@ -619,7 +619,7 @@ $highlight"""
// lets try resolve the include name relative to this file // lets try resolve the include name relative to this file
val paths = relativeName.split('/') val paths = relativeName.split('/')
val size = paths.size val size = paths.size()
for (i in 0.rangeTo(size - 2)) { for (i in 0.rangeTo(size - 2)) {
val path = paths[i] val path = paths[i]
if (path == ".") continue if (path == ".") continue
@@ -951,7 +951,7 @@ class KPackage(
val nameAsRelativePath: String val nameAsRelativePath: String
get() { get() {
val answer = namePaths.map{ ".." }.makeString("/") val answer = namePaths.map{ ".." }.makeString("/")
return if (answer.length == 0) "" else answer + "/" return if (answer.length() == 0) "" else answer + "/"
} }
override fun description(template: KDocTemplate): String { override fun description(template: KDocTemplate): String {
@@ -190,10 +190,10 @@ Class ${klass.simpleName}</H2>
<P>""") <P>""")
println(klass.detailedDescription(this)) println(klass.detailedDescription(this))
if (klass.since.size > 0 || klass.authors.size > 0) { if (klass.since.length() > 0 || klass.authors.size() > 0) {
println("""<P> println("""<P>
<DL>""") <DL>""")
if (klass.since.size > 0) { if (klass.since.length() > 0) {
println("""<DT><B>Since:</B></DT> println("""<DT><B>Since:</B></DT>
<DD>${klass.since}</DD>""") <DD>${klass.since}</DD>""")
} }
@@ -137,7 +137,7 @@ abstract class KDocTemplate() : TextTemplate() {
if (cname.startsWith("kotlin.Function") && arguments.isNotEmpty()) { if (cname.startsWith("kotlin.Function") && arguments.isNotEmpty()) {
val rt = arguments.last() val rt = arguments.last()
// TODO use drop() // TODO use drop()
val rest = arguments.subList(0, arguments.size - 1).orEmpty() val rest = arguments.dropLast(1)
"${typeArguments(rest, "(", ")", "()")}&nbsp;<A HREF=\"${href(c)}\" title=\"${c.kind} in ${c.packageName}\">-&gt;</a>&nbsp;${link(rt)}" "${typeArguments(rest, "(", ")", "()")}&nbsp;<A HREF=\"${href(c)}\" title=\"${c.kind} in ${c.packageName}\">-&gt;</a>&nbsp;${link(rt)}"
} else { } else {
val name = if (fullName) cname else c.simpleName val name = if (fullName) cname else c.simpleName
@@ -236,7 +236,7 @@ fun filtering(): List<GenericFunction> {
returns(Strings) { "String" } returns(Strings) { "String" }
body(Strings) { body(Strings) {
""" """
for (index in 0..length - 1) for (index in 0..length() - 1)
if (!predicate(get(index))) { if (!predicate(get(index))) {
return substring(0, index) return substring(0, index)
} }
@@ -357,7 +357,7 @@ fun filtering(): List<GenericFunction> {
doc(Strings) { "Appends all characters matching the given [predicate] to the given [destination]." } doc(Strings) { "Appends all characters matching the given [predicate] to the given [destination]." }
body(Strings) { body(Strings) {
""" """
for (index in 0..length - 1) { for (index in 0..length() - 1) {
val element = get(index) val element = get(index)
if (predicate(element)) destination.append(element) if (predicate(element)) destination.append(element)
} }