StdLib: Rename method parameters (Maps)

This commit is contained in:
Ilya Gorbunov
2015-11-19 22:40:13 +03:00
parent a3cd86d2cd
commit bbe6a3c7ca
3 changed files with 64 additions and 118 deletions
@@ -20,30 +20,30 @@ public fun <K, V> Map<K, V>.getOrImplicitDefault(key: K): V {
} }
/** /**
* Returns a wrapper of this read-only map, having the implicit default value provided with the specified function [default]. * Returns a wrapper of this read-only map, having the implicit default value provided with the specified function [defaultValue].
* This implicit default value is used when [getOrImplicitDefault] is called on the returned map, * This implicit default value is used when [getOrImplicitDefault] is called on the returned map,
* and that map doesn't contain value for the key specified. * and that map doesn't contain value for the key specified.
* *
* When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call. * When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call.
*/ */
public fun <K, V> Map<K, V>.withDefault(default: (key: K) -> V): Map<K, V> = public fun <K, V> Map<K, V>.withDefault(defaultValue: (key: K) -> V): Map<K, V> =
when (this) { when (this) {
is MapWithDefault -> this.map.withDefault(default) is MapWithDefault -> this.map.withDefault(defaultValue)
else -> MapWithDefaultImpl(this, default) else -> MapWithDefaultImpl(this, defaultValue)
} }
/** /**
* Returns a wrapper of this mutable map, having the implicit default value provided with the specified function [default]. * Returns a wrapper of this mutable map, having the implicit default value provided with the specified function [defaultValue].
* This implicit default value is used when [getOrImplicitDefault] is called on the returned map, * This implicit default value is used when [getOrImplicitDefault] is called on the returned map,
* and that map doesn't contain value for the key specified. * and that map doesn't contain value for the key specified.
* *
* When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call. * When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call.
*/ */
@kotlin.jvm.JvmName("withDefaultMutable") @kotlin.jvm.JvmName("withDefaultMutable")
public fun <K, V> MutableMap<K, V>.withDefault(default: (key: K) -> V): MutableMap<K, V> = public fun <K, V> MutableMap<K, V>.withDefault(defaultValue: (key: K) -> V): MutableMap<K, V> =
when (this) { when (this) {
is MutableMapWithDefault -> this.map.withDefault(default) is MutableMapWithDefault -> this.map.withDefault(defaultValue)
else -> MutableMapWithDefaultImpl(this, default) else -> MutableMapWithDefaultImpl(this, defaultValue)
} }
+50 -87
View File
@@ -34,7 +34,7 @@ public fun <K, V> emptyMap(): Map<K, V> = EmptyMap as Map<K, V>
* *
* The returned map is serializable (JVM). * The returned map is serializable (JVM).
*/ */
public fun <K, V> mapOf(vararg values: Pair<K, V>): Map<K, V> = if (values.size > 0) linkedMapOf(*values) else emptyMap() public fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> = if (pairs.size > 0) linkedMapOf(*pairs) else emptyMap()
/** Returns an empty read-only map. The returned map is serializable (JVM). */ /** Returns an empty read-only map. The returned map is serializable (JVM). */
public fun <K, V> mapOf(): Map<K, V> = emptyMap() public fun <K, V> mapOf(): Map<K, V> = emptyMap()
@@ -44,7 +44,7 @@ public fun <K, V> mapOf(): Map<K, V> = emptyMap()
* specified value. The returned map is serializable. * specified value. The returned map is serializable.
*/ */
@JvmVersion @JvmVersion
public fun <K, V> mapOf(keyValuePair: Pair<K, V>): Map<K, V> = Collections.singletonMap(keyValuePair.first, keyValuePair.second) public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = Collections.singletonMap(pair.first, pair.second)
/** /**
* Returns a new [HashMap] with the specified contents, given as a list of pairs * Returns a new [HashMap] with the specified contents, given as a list of pairs
@@ -52,11 +52,9 @@ public fun <K, V> mapOf(keyValuePair: Pair<K, V>): Map<K, V> = Collections.singl
* *
* @sample test.collections.MapTest.createUsingPairs * @sample test.collections.MapTest.createUsingPairs
*/ */
public fun <K, V> hashMapOf(vararg values: Pair<K, V>): HashMap<K, V> { public fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>
val answer = HashMap<K, V>(mapCapacity(values.size)) = HashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
answer.putAll(*values)
return answer
}
/** /**
* Returns a new [LinkedHashMap] with the specified contents, given as a list of pairs * Returns a new [LinkedHashMap] with the specified contents, given as a list of pairs
@@ -65,11 +63,8 @@ public fun <K, V> hashMapOf(vararg values: Pair<K, V>): HashMap<K, V> {
* *
* @sample test.collections.MapTest.createLinkedMap * @sample test.collections.MapTest.createLinkedMap
*/ */
public fun <K, V> linkedMapOf(vararg values: Pair<K, V>): LinkedHashMap<K, V> { public fun <K, V> linkedMapOf(vararg pairs: Pair<K, V>): LinkedHashMap<K, V>
val answer = LinkedHashMap<K, V>(mapCapacity(values.size)) = LinkedHashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
answer.putAll(*values)
return answer
}
/** /**
* Calculate the initial capacity of a map, based on Guava's com.google.common.collect.Maps approach. This is equivalent * Calculate the initial capacity of a map, based on Guava's com.google.common.collect.Maps approach. This is equivalent
@@ -220,10 +215,19 @@ public inline fun <K, V, R, C : MutableMap<R, V>> Map<K, V>.mapKeysTo(destinatio
} }
/** /**
* Puts all the given [values] into this [MutableMap] with the first component in the pair being the key and the second the value. * Puts all the given [pairs] into this [MutableMap] with the first component in the pair being the key and the second the value.
*/ */
public fun <K, V> MutableMap<K, V>.putAll(vararg values: Pair<K, V>): Unit { @kotlin.jvm.JvmName("putAllVararg")
for ((key, value) in values) { @Deprecated("Use an overload without vararg", ReplaceWith("putAll(pairs)"))
public fun <K, V> MutableMap<K, V>.putAll(vararg pairs: Pair<K, V>): Unit {
putAll(pairs)
}
/**
* Puts all the given [pairs] into this [MutableMap] with the first component in the pair being the key and the second the value.
*/
public fun <K, V> MutableMap<K, V>.putAll(pairs: Array<out Pair<K, V>>): Unit {
for ((key, value) in pairs) {
put(key, value) put(key, value)
} }
} }
@@ -231,8 +235,8 @@ public fun <K, V> MutableMap<K, V>.putAll(vararg values: Pair<K, V>): Unit {
/** /**
* Puts all the elements of the given collection into this [MutableMap] with the first component in the pair being the key and the second the value. * Puts all the elements of the given collection into this [MutableMap] with the first component in the pair being the key and the second the value.
*/ */
public fun <K, V> MutableMap<K, V>.putAll(values: Iterable<Pair<K,V>>): Unit { public fun <K, V> MutableMap<K, V>.putAll(pairs: Iterable<Pair<K,V>>): Unit {
for ((key, value) in values) { for ((key, value) in pairs) {
put(key, value) put(key, value)
} }
} }
@@ -240,8 +244,8 @@ public fun <K, V> MutableMap<K, V>.putAll(values: Iterable<Pair<K,V>>): Unit {
/** /**
* Puts all the elements of the given sequence into this [MutableMap] with the first component in the pair being the key and the second the value. * Puts all the elements of the given sequence into this [MutableMap] with the first component in the pair being the key and the second the value.
*/ */
public fun <K, V> MutableMap<K, V>.putAll(values: Sequence<Pair<K,V>>): Unit { public fun <K, V> MutableMap<K, V>.putAll(pairs: Sequence<Pair<K,V>>): Unit {
for ((key, value) in values) { for ((key, value) in pairs) {
put(key, value) put(key, value)
} }
} }
@@ -338,36 +342,21 @@ public inline fun <K, V> Map<K, V>.filterNot(predicate: (Map.Entry<K, V>) -> Boo
/** /**
* Returns a new map containing all key-value pairs from the given collection of pairs. * Returns a new map containing all key-value pairs from the given collection of pairs.
*/ */
public fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V> { public fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V>
val result = LinkedHashMap<K, V>(collectionSizeOrNull()?.let { mapCapacity(it) } ?: 16) = LinkedHashMap<K, V>(collectionSizeOrNull()?.let { mapCapacity(it) } ?: 16).apply { putAll(this@toMap) }
for (element in this) {
result.put(element.first, element.second)
}
return result
}
/** /**
* Returns a new map containing all key-value pairs from the given array of pairs. * Returns a new map containing all key-value pairs from the given array of pairs.
*/ */
public fun <K, V> Array<Pair<K, V>>.toMap(): Map<K, V> { public fun <K, V> Array<out Pair<K, V>>.toMap(): Map<K, V>
val result = LinkedHashMap<K, V>(mapCapacity(size)) = LinkedHashMap<K, V>(mapCapacity(size)).apply { putAll(this@toMap) }
for (element in this) {
result.put(element.first, element.second)
}
return result
}
/** /**
* Returns a new map containing all key-value pairs from the given sequence of pairs. * Returns a new map containing all key-value pairs from the given sequence of pairs.
*/ */
public fun <K, V> Sequence<Pair<K, V>>.toMap(): Map<K, V> { public fun <K, V> Sequence<Pair<K, V>>.toMap(): Map<K, V>
val result = LinkedHashMap<K, V>() = LinkedHashMap<K, V>().apply { putAll(this@toMap) }
for (element in this) {
result.put(element.first, element.second)
}
return result
}
/** /**
* Converts this [Map] to a [LinkedHashMap], maintaining the insertion order of elements added to that map afterwards. * Converts this [Map] to a [LinkedHashMap], maintaining the insertion order of elements added to that map afterwards.
@@ -377,47 +366,33 @@ public fun <K, V> Map<K, V>.toLinkedMap(): MutableMap<K, V> = LinkedHashMap(this
/** /**
* Creates a new read-only map by replacing or adding an entry to this map from a given key-value [pair]. * Creates a new read-only map by replacing or adding an entry to this map from a given key-value [pair].
*/ */
public operator fun <K, V> Map<K, V>.plus(pair: Pair<K, V>): Map<K, V> { public operator fun <K, V> Map<K, V>.plus(pair: Pair<K, V>): Map<K, V>
val newMap = this.toLinkedMap() = this.toLinkedMap().apply { put(pair.first, pair.second) }
newMap.put(pair.first, pair.second)
return newMap
}
/** /**
* Creates a new read-only map by replacing or adding entries to this map from a given collection of key-value [pairs]. * Creates a new read-only map by replacing or adding entries to this map from a given collection of key-value [pairs].
*/ */
public operator fun <K, V> Map<K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<K, V> { public operator fun <K, V> Map<K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<K, V>
val newMap = this.toLinkedMap() = this.toLinkedMap().apply { putAll(pairs) }
newMap.putAll(pairs)
return newMap
}
/** /**
* Creates a new read-only map by replacing or adding entries to this map from a given array of key-value [pairs]. * Creates a new read-only map by replacing or adding entries to this map from a given array of key-value [pairs].
*/ */
public operator fun <K, V> Map<K, V>.plus(pairs: Array<Pair<K, V>>): Map<K, V> { public operator fun <K, V> Map<K, V>.plus(pairs: Array<out Pair<K, V>>): Map<K, V>
val newMap = this.toLinkedMap() = this.toLinkedMap().apply { putAll(pairs) }
newMap.putAll(*pairs)
return newMap
}
/** /**
* Creates a new read-only map by replacing or adding entries to this map from a given sequence of key-value [pairs]. * Creates a new read-only map by replacing or adding entries to this map from a given sequence of key-value [pairs].
*/ */
public operator fun <K, V> Map<K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<K, V> { public operator fun <K, V> Map<K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<K, V>
val newMap = this.toLinkedMap() = this.toLinkedMap().apply { putAll(pairs) }
newMap.putAll(pairs)
return newMap
}
/** /**
* Creates a new read-only map by replacing or adding entries to this map from another [map]. * Creates a new read-only map by replacing or adding entries to this map from another [map].
*/ */
public operator fun <K, V> Map<K, V>.plus(map: Map<K, V>): Map<K, V> { public operator fun <K, V> Map<K, V>.plus(map: Map<K, V>): Map<K, V>
val newMap = this.toLinkedMap() = this.toLinkedMap().apply { putAll(map) }
newMap.putAll(map)
return newMap
}
/** /**
* Appends or replaces the given [pair] in this mutable map. * Appends or replaces the given [pair] in this mutable map.
@@ -436,8 +411,8 @@ public operator fun <K, V> MutableMap<K, V>.plusAssign(pairs: Iterable<Pair<K, V
/** /**
* Appends or replaces all pairs from the given array of [pairs] in this mutable map. * Appends or replaces all pairs from the given array of [pairs] in this mutable map.
*/ */
public operator fun <K, V> MutableMap<K, V>.plusAssign(pairs: Array<Pair<K, V>>) { public operator fun <K, V> MutableMap<K, V>.plusAssign(pairs: Array<out Pair<K, V>>) {
putAll(*pairs) putAll(pairs)
} }
/** /**
@@ -457,38 +432,26 @@ public operator fun <K, V> MutableMap<K, V>.plusAssign(map: Map<K, V>) {
/** /**
* Creates a new read-only map by removing a [key] from this map. * Creates a new read-only map by removing a [key] from this map.
*/ */
public operator fun <K, V> Map<K, V>.minus(key: K): Map<K, V> { public operator fun <K, V> Map<K, V>.minus(key: K): Map<K, V>
val result = LinkedHashMap<K, V>(this) = this.toLinkedMap().apply { minusAssign(key) }
result.minusAssign(key)
return result
}
/** /**
* Creates a new read-only map by removing a collection of [keys] from this map. * Creates a new read-only map by removing a collection of [keys] from this map.
*/ */
public operator fun <K, V> Map<K, V>.minus(keys: Iterable<K>): Map<K, V> { public operator fun <K, V> Map<K, V>.minus(keys: Iterable<K>): Map<K, V>
val result = LinkedHashMap<K, V>(this) = this.toLinkedMap().apply { minusAssign(keys) }
result.minusAssign(keys)
return result
}
/** /**
* Creates a new read-only map by removing a array of [keys] from this map. * Creates a new read-only map by removing a array of [keys] from this map.
*/ */
public operator fun <K, V> Map<K, V>.minus(keys: Array<K>): Map<K, V> { public operator fun <K, V> Map<K, V>.minus(keys: Array<K>): Map<K, V>
val result = LinkedHashMap<K, V>(this) = this.toLinkedMap().apply { minusAssign(keys) }
result.minusAssign(keys)
return result
}
/** /**
* Creates a new read-only map by removing a sequence of [keys] from this map. * Creates a new read-only map by removing a sequence of [keys] from this map.
*/ */
public operator fun <K, V> Map<K, V>.minus(keys: Sequence<K>): Map<K, V> { public operator fun <K, V> Map<K, V>.minus(keys: Sequence<K>): Map<K, V>
val result = LinkedHashMap<K, V>(this) = this.toLinkedMap().apply { minusAssign(keys) }
result.minusAssign(keys)
return result
}
/** /**
* Removes the given [key] from this mutable map. * Removes the given [key] from this mutable map.
@@ -59,11 +59,8 @@ public fun <K : Comparable<K>, V> Map<K, V>.toSortedMap(): SortedMap<K, V> = Tre
* *
* @sample test.collections.MapJVMTest.toSortedMapWithComparator * @sample test.collections.MapJVMTest.toSortedMapWithComparator
*/ */
public fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<in K>): SortedMap<K, V> { public fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<in K>): SortedMap<K, V>
val result = TreeMap<K, V>(comparator) = TreeMap<K, V>(comparator).apply { putAll(this@toSortedMap) }
result.putAll(this)
return result
}
/** /**
* Returns a new [SortedMap] with the specified contents, given as a list of pairs * Returns a new [SortedMap] with the specified contents, given as a list of pairs
@@ -71,17 +68,8 @@ public fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<in K>): SortedMap
* *
* @sample test.collections.MapJVMTest.createSortedMap * @sample test.collections.MapJVMTest.createSortedMap
*/ */
public fun <K, V> sortedMapOf(vararg values: Pair<K, V>): SortedMap<K, V> { public fun <K, V> sortedMapOf(vararg pairs: Pair<K, V>): SortedMap<K, V>
val answer = TreeMap<K, V>() = TreeMap<K, V>().apply { putAll(pairs) }
/**
TODO replace by this simpler call when we can pass vararg values into other methods
answer.putAll(values)
*/
for (v in values) {
answer.put(v.first, v.second)
}
return answer
}
/** /**
@@ -89,11 +77,6 @@ public fun <K, V> sortedMapOf(vararg values: Pair<K, V>): SortedMap<K, V> {
* *
* @sample test.collections.MapJVMTest.toProperties * @sample test.collections.MapJVMTest.toProperties
*/ */
public fun Map<String, String>.toProperties(): Properties { public fun Map<String, String>.toProperties(): Properties
val answer = Properties() = Properties().apply { putAll(this@toProperties) }
for (e in this) {
answer.put(e.key, e.value)
}
return answer
}