From ab45439256d80f231ed05699f28da4fc925eb8d0 Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Fri, 30 May 2014 17:16:33 +0400 Subject: [PATCH] Convert iterable of pairs to map, improve generic toMap(mutableMap) function #KT-4166 Fixed --- .../src/kotlin/collections/Exceptions.kt | 2 ++ .../stdlib/src/kotlin/collections/Maps.kt | 28 ++++++++++++++++--- .../stdlib/src/kotlin/collections/MapsJVM.kt | 6 ++-- libraries/stdlib/test/collections/MapTest.kt | 7 +++++ libraries/tools/kotlin-js-library/pom.xml | 1 + 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/libraries/stdlib/src/kotlin/collections/Exceptions.kt b/libraries/stdlib/src/kotlin/collections/Exceptions.kt index da54b0ba0b3..38c6d51319d 100644 --- a/libraries/stdlib/src/kotlin/collections/Exceptions.kt +++ b/libraries/stdlib/src/kotlin/collections/Exceptions.kt @@ -1,3 +1,5 @@ package kotlin public class EmptyIterableException(val it : Iterable<*>) : RuntimeException("$it is empty") + +public class DuplicateKeyException(val message : String = "Duplicate keys detected") : RuntimeException(message) \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index cc981ecb2c0..75d99d79b77 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -18,7 +18,7 @@ public fun MutableMap.set(key : K, value : V) : V? = this.put(key, /** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */ public fun Map?.orEmpty() : Map -= if (this != null) this else Collections.emptyMap() as Map += if (this != null) this else stdlib_emptyMap() public fun Map.contains(key : K) : Boolean = containsKey(key) @@ -94,19 +94,39 @@ public inline fun > Map.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 */ public fun MutableMap.putAll(vararg values: Pair): Unit { - for (v in values) { - put(v.first, v.second) + for ((key, value) in values) { + put(key, value) } } /** * Copies the entries in this [[Map]] to the given mutable *map* */ -public fun Map.toMap(map: MutableMap): Map { +public fun > Map.toMap(map: C): C { map.putAll(this) return map } +/** + * Copies the entries from given iterable of pairs to the given mutable *map* + */ +public fun > Iterable>.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 Iterable>.toMap() : Map { + return toMap(HashMap()) +} + /** * Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] * diff --git a/libraries/stdlib/src/kotlin/collections/MapsJVM.kt b/libraries/stdlib/src/kotlin/collections/MapsJVM.kt index 9638f9124f2..00aa9607741 100644 --- a/libraries/stdlib/src/kotlin/collections/MapsJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/MapsJVM.kt @@ -11,14 +11,14 @@ import java.util.Properties /** * Converts this [[Map]] to a [[LinkedHashMap]] so future insertion orders are maintained */ -public fun Map.toLinkedMap(): LinkedHashMap = toMap(LinkedHashMap(size)) as LinkedHashMap +public fun Map.toLinkedMap(): LinkedHashMap = toMap(LinkedHashMap(size)) /** * Converts this [[Map]] to a [[SortedMap]] so iteration order will be in key order * * @includeFunctionBody ../../test/collections/MapTest.kt toSortedMap */ -public fun Map.toSortedMap(): SortedMap = toMap(TreeMap()) as SortedMap +public fun Map.toSortedMap(): SortedMap = toMap(TreeMap()) /** * 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 Map.toSortedMap(): SortedMap = toMap(TreeMap()) * * @includeFunctionBody ../../test/collections/MapTest.kt toSortedMapWithComparator */ -public fun Map.toSortedMap(comparator: Comparator): SortedMap = toMap(TreeMap(comparator)) as SortedMap +public fun Map.toSortedMap(comparator: Comparator): SortedMap = toMap(TreeMap(comparator)) /** diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 31f1f316a98..43dc8cb5de1 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -140,6 +140,13 @@ class MapTest { 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() { val map = hashMapOf("a" to 1, "b" to 2) assertEquals(2, map.size) diff --git a/libraries/tools/kotlin-js-library/pom.xml b/libraries/tools/kotlin-js-library/pom.xml index b72a1121203..01c94c1834f 100644 --- a/libraries/tools/kotlin-js-library/pom.xml +++ b/libraries/tools/kotlin-js-library/pom.xml @@ -62,6 +62,7 @@ +