Minor, remove/simplify unnecessary utilities in util.runtime

This commit is contained in:
Alexander Udalov
2015-12-23 16:44:42 +03:00
parent b4addd0567
commit 57b96f38a9
11 changed files with 34 additions and 131 deletions
@@ -1,29 +0,0 @@
/*
* Copyright 2010-2015 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 org.jetbrains.kotlin.utils
import java.util.*
class HammingComparator<T>(private val referenceString: String, private val asString: T.() -> String) : Comparator<T> {
private fun countDifference(s1: String): Int {
return (0..Math.min(s1.lastIndex, referenceString.lastIndex)).count { s1[it] != referenceString[it] }
}
override fun compare(lookupItem1: T, lookupItem2: T): Int {
return countDifference(lookupItem1.asString()) - countDifference(lookupItem2.asString())
}
}
@@ -17,8 +17,7 @@
package org.jetbrains.kotlin.utils.addToStdlib
import java.lang.reflect.Modifier
import java.util.Collections
import java.util.NoSuchElementException
import java.util.*
import java.util.concurrent.ConcurrentHashMap
public fun <T: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
@@ -16,52 +16,10 @@
package org.jetbrains.kotlin.utils
import java.util.ArrayList
import java.util.HashMap
import java.util.HashSet
import java.util.LinkedHashMap
public fun <K, V> Sequence<V>.valuesToMap(key: (V) -> K): Map<K, V> {
val map = LinkedHashMap<K, V>()
for (v in this) {
map[key(v)] = v
}
return map
}
public fun <K, V> Sequence<K>.keysToMap(value: (K) -> V): Map<K, V> {
val map = LinkedHashMap<K, V>()
for (k in this) {
map[k] = value(k)
}
return map
}
public fun <K, V: Any> Sequence<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K, V> {
val map = LinkedHashMap<K, V>()
for (k in this) {
val v = value(k)
if (v != null) {
map[k] = v
}
}
return map
}
public fun <K, V> Iterable<V>.valuesToMap(key: (V) -> K): Map<K, V> {
val map = LinkedHashMap<K, V>()
for (v in this) {
map[key(v)] = v
}
return map
}
import java.util.*
public fun <K, V> Iterable<K>.keysToMap(value: (K) -> V): Map<K, V> {
val map = LinkedHashMap<K, V>()
for (k in this) {
map[k] = value(k)
}
return map
return toMapBy({ it }, value)
}
public fun <K, V: Any> Iterable<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K, V> {
@@ -87,7 +45,7 @@ public inline fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpt
public inline fun <T> Array<out T>.ifEmpty(body: () -> Array<out T>): Array<out T> = if (isEmpty()) body() else this
public fun <T: Any> emptyOrSingletonList(item: T?): List<T> = if (item == null) listOf() else listOf(item)
public fun <T: Any> emptyOrSingletonList(item: T?): List<T> = listOfNotNull(item)
public fun <T: Any> MutableCollection<T>.addIfNotNull(t: T?) {
if (t != null) add(t)
@@ -102,7 +60,7 @@ public fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> {
}
public fun <T> Collection<T>.toReadOnlyList(): List<T> =
when (size()) {
when (size) {
0 -> emptyList()
1 -> listOf(first())
else -> ArrayList(this)
@@ -110,8 +68,3 @@ public fun <T> Collection<T>.toReadOnlyList(): List<T> =
public fun <T: Any> T?.singletonOrEmptyList(): List<T> =
if (this != null) listOf(this) else emptyList()
public fun <T> MutableList<T>.removeLast(condition: (T) -> Boolean): T? {
val index = indexOfLast(condition)
return if (index >= 0) removeAt(index) else null
}
@@ -1,30 +0,0 @@
/*
* Copyright 2010-2015 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 org.jetbrains.kotlin.utils
import java.io.File
// Similar to kotlin.io.recurse, but processes each directory after its children
public fun File.recursePostOrder(block: (File) -> Unit): Unit {
val children = this.listFiles()
if (children != null) {
for (child in children) {
child.recursePostOrder(block)
}
}
block(this)
}