Convert iterable of pairs to map, improve generic toMap(mutableMap) function #KT-4166 Fixed

This commit is contained in:
Ilya Ryzhenkov
2014-05-30 17:16:33 +04:00
committed by Andrey Breslav
parent 3a1b9cc658
commit ab45439256
5 changed files with 37 additions and 7 deletions
@@ -1,3 +1,5 @@
package kotlin package kotlin
public class EmptyIterableException(val it : Iterable<*>) : RuntimeException("$it is empty") public class EmptyIterableException(val it : Iterable<*>) : RuntimeException("$it is empty")
public class DuplicateKeyException(val message : String = "Duplicate keys detected") : RuntimeException(message)
@@ -18,7 +18,7 @@ public fun <K, V> MutableMap<K, V>.set(key : K, value : V) : V? = this.put(key,
/** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */ /** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */
public fun <K,V> Map<K,V>?.orEmpty() : Map<K,V> public fun <K,V> Map<K,V>?.orEmpty() : Map<K,V>
= if (this != null) this else Collections.emptyMap<K,V>() as Map<K,V> = if (this != null) this else stdlib_emptyMap()
public fun <K,V> Map<K,V>.contains(key : K) : Boolean = containsKey(key) public fun <K,V> Map<K,V>.contains(key : K) : Boolean = containsKey(key)
@@ -94,19 +94,39 @@ public inline fun <K,V,R,C: MutableMap<K,R>> Map<K,V>.mapValuesTo(result: C, tra
* Puts all the entries into this [[MutableMap]] with the first value in the pair being the key and the second the value * Puts all the entries into this [[MutableMap]] with the first value 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 { public fun <K,V> MutableMap<K,V>.putAll(vararg values: Pair<K, V>): Unit {
for (v in values) { for ((key, value) in values) {
put(v.first, v.second) put(key, value)
} }
} }
/** /**
* Copies the entries in this [[Map]] to the given mutable *map* * Copies the entries in this [[Map]] to the given mutable *map*
*/ */
public fun <K,V> Map<K,V>.toMap(map: MutableMap<K,V>): Map<K,V> { public fun <K,V, C : MutableMap<K, in V>> Map<K,V>.toMap(map: C): C {
map.putAll(this) map.putAll(this)
return map return map
} }
/**
* Copies the entries from given iterable of pairs to the given mutable *map*
*/
public fun <K,V, C : MutableMap<K, in V>> Iterable<Pair<K,V>>.toMap(map: C): C {
for((key,value) in this) {
if (map.containsKey(key)) {
throw DuplicateKeyException()
}
map.put(key,value)
}
return map
}
/**
* Creates map from given iterable of pairs
*/
public fun <K,V> Iterable<Pair<K,V>>.toMap() : Map<K,V> {
return toMap(HashMap<K,V>())
}
/** /**
* Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] * Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
* *
@@ -11,14 +11,14 @@ import java.util.Properties
/** /**
* Converts this [[Map]] to a [[LinkedHashMap]] so future insertion orders are maintained * Converts this [[Map]] to a [[LinkedHashMap]] so future insertion orders are maintained
*/ */
public fun <K,V> Map<K,V>.toLinkedMap(): LinkedHashMap<K,V> = toMap<K,V>(LinkedHashMap(size)) as LinkedHashMap<K,V> public fun <K,V> Map<K,V>.toLinkedMap(): LinkedHashMap<K,V> = toMap(LinkedHashMap<K,V>(size))
/** /**
* Converts this [[Map]] to a [[SortedMap]] so iteration order will be in key order * Converts this [[Map]] to a [[SortedMap]] so iteration order will be in key order
* *
* @includeFunctionBody ../../test/collections/MapTest.kt toSortedMap * @includeFunctionBody ../../test/collections/MapTest.kt toSortedMap
*/ */
public fun <K,V> Map<K,V>.toSortedMap(): SortedMap<K,V> = toMap<K,V>(TreeMap()) as SortedMap<K,V> public fun <K,V> Map<K,V>.toSortedMap(): SortedMap<K,V> = toMap(TreeMap<K,V>())
/** /**
* Converts this [[Map]] to a [[SortedMap]] using the given *comparator* so that iteration order will be in the order * Converts this [[Map]] to a [[SortedMap]] using the given *comparator* so that iteration order will be in the order
@@ -26,7 +26,7 @@ public fun <K,V> Map<K,V>.toSortedMap(): SortedMap<K,V> = toMap<K,V>(TreeMap())
* *
* @includeFunctionBody ../../test/collections/MapTest.kt toSortedMapWithComparator * @includeFunctionBody ../../test/collections/MapTest.kt toSortedMapWithComparator
*/ */
public fun <K,V> Map<K,V>.toSortedMap(comparator: Comparator<K>): SortedMap<K,V> = toMap<K,V>(TreeMap(comparator)) as SortedMap<K,V> public fun <K,V> Map<K,V>.toSortedMap(comparator: Comparator<K>): SortedMap<K,V> = toMap(TreeMap<K,V>(comparator))
/** /**
@@ -140,6 +140,13 @@ class MapTest {
assertEquals(2, map.get("b")) assertEquals(2, map.get("b"))
} }
test fun createFromIterable() {
val map = listOf(Pair("a", 1), Pair("b", 2)).toMap()
assertEquals(2, map.size)
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
}
test fun createUsingTo() { test fun createUsingTo() {
val map = hashMapOf("a" to 1, "b" to 2) val map = hashMapOf("a" to 1, "b" to 2)
assertEquals(2, map.size) assertEquals(2, map.size)
@@ -62,6 +62,7 @@
<include name="collections/Arrays.kt"/> <include name="collections/Arrays.kt"/>
<include name="collections/JUtil.kt"/> <include name="collections/JUtil.kt"/>
<include name="collections/Maps.kt"/> <include name="collections/Maps.kt"/>
<include name="collections/Exceptions.kt"/>
<include name="collections/Stream.kt"/> <include name="collections/Stream.kt"/>
<include name="collections/MutableCollections.kt"/> <include name="collections/MutableCollections.kt"/>
<include name="Functions.kt"/> <include name="Functions.kt"/>