From 0325e68a863921322141006291fc05e536ad0e94 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Mon, 16 Apr 2012 12:00:47 +0100 Subject: [PATCH] #KT-1795 Fixed - added hashMap() and sortedMap() helper functions for creating maps more easily using tuples --- libraries/stdlib/src/kotlin/JUtil.kt | 37 ++++++++++++++++++- libraries/stdlib/src/kotlin/JUtilMaps.kt | 9 +++++ libraries/stdlib/test/MapTest.kt | 6 +++ .../jetbrains/kotlin/doc/model/KotlinModel.kt | 22 ++++++----- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/libraries/stdlib/src/kotlin/JUtil.kt b/libraries/stdlib/src/kotlin/JUtil.kt index ead01302ef4..2b0b5e39b5c 100644 --- a/libraries/stdlib/src/kotlin/JUtil.kt +++ b/libraries/stdlib/src/kotlin/JUtil.kt @@ -23,14 +23,47 @@ public inline fun hashSet(vararg values: T) : HashSet = values.to(HashSet< /** Returns a new SortedSet with a variable number of initial elements */ public inline fun sortedSet(vararg values: T) : TreeSet = values.to(TreeSet()) -public inline fun hashMap(): HashMap = HashMap() +/** + * Returns a new [[HashMap]] populated with the given tuple values where the first value in each tuple + * is the key and the second value is the value + * + * @includeFunctionBody ../../test/MapTest.kt createUsingTuples + */ +public inline fun hashMap(vararg values: #(K,V)): HashMap { + val answer = HashMap() + /** + 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._1, v._2) + } + return answer +} -public inline fun sortedMap(): SortedMap = TreeMap() +/** + * Returns a new [[SortedMap]] populated with the given tuple values where the first value in each tuple + * is the key and the second value is the value + */ +public inline fun sortedMap(vararg values: #(K,V)): SortedMap { + val answer = TreeMap() + /** + 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._1, v._2) + } + return answer +} val Collection<*>.indices : IntRange get() = 0..size-1 +/** + * Converts the collection to an array + */ public inline fun java.util.Collection.toArray() : Array { val answer = arrayOfNulls(this.size) var idx = 0 diff --git a/libraries/stdlib/src/kotlin/JUtilMaps.kt b/libraries/stdlib/src/kotlin/JUtilMaps.kt index 3117068869d..9f0fd4b9dd0 100644 --- a/libraries/stdlib/src/kotlin/JUtilMaps.kt +++ b/libraries/stdlib/src/kotlin/JUtilMaps.kt @@ -113,3 +113,12 @@ public inline fun > java.util.Map.mapValuesTo(r } return result } + +/** + * Puts all the entries into the map with the first value in the tuple being the key and the second the value + */ +public inline fun java.util.Map.putAll(vararg values: #(K,V)): Unit { + for (v in values) { + put(v._1, v._2) + } +} \ No newline at end of file diff --git a/libraries/stdlib/test/MapTest.kt b/libraries/stdlib/test/MapTest.kt index 3588c138479..1bb2115e556 100644 --- a/libraries/stdlib/test/MapTest.kt +++ b/libraries/stdlib/test/MapTest.kt @@ -112,6 +112,12 @@ class MapTest { assertEquals(arrayList("beer2", "Mells2"), m2.values().toList()) } + test fun createUsingTuples() { + val map = hashMap(#("a", 1), #("b", 2)) + assertEquals(2, map.size) + assertEquals(1, map.get("a")) + assertEquals(2, map.get("b")) + } /** TODO diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt index 4c8833e2638..b63454a7042 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt @@ -676,6 +676,18 @@ class KModel(var context: BindingContext, val config: KDocConfig) { } class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate): LinkRenderer() { + // TODO dirty hack - remove when this issue is fixed + // http://youtrack.jetbrains.com/issue/KT-1524 + val hackedLinks = hashMap( + #("IllegalArgumentException", #("java.lang", "java/lang/IllegalArgumentException.html")), + #("IllegalStateException", #("java.lang", "java/lang/IllegalStateException.html")), + #("Map.Entry", #("java.util", "java/util/Map.Entry.html")), + #("System.in", #("java.lang", "java/lang/System.html#in")), + #("System.out", #("java.lang", "java/lang/System.html#in")), + #("#equals()", #("java.lang", "java/lang/Object.html#equals(java.lang.Object)")), + #("#hashCode()", #("java.lang", "java/lang/Object.html#hashCode()")) + ) + public override fun render(node: WikiLinkNode?): Rendering? { val answer = super.render(node) @@ -683,7 +695,6 @@ class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate val text = answer.text if (text != null) { val qualified = resolveToQualifiedName(text) - var href = resolveClassNameLink(qualified) if (href != null) { answer.href = href @@ -706,15 +717,6 @@ class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate } if (href == null) { // TODO even hacker than the above hack! - val hackedLinks = HashMap() - hackedLinks.put("IllegalArgumentException", #("java.lang", "java/lang/IllegalArgumentException.html")) - hackedLinks.put("IllegalStateException", #("java.lang", "java/lang/IllegalStateException.html")) - hackedLinks.put("Map.Entry", #("java.util", "java/util/Map.Entry.html")) - hackedLinks.put("System.in", #("java.lang", "java/lang/System.html#in")) - hackedLinks.put("System.out", #("java.lang", "java/lang/System.html#in")) - hackedLinks.put("#equals()", #("java.lang", "java/lang/Object.html#equals(java.lang.Object)")) - hackedLinks.put("#hashCode()", #("java.lang", "java/lang/Object.html#hashCode()")) - val link = hackedLinks.get(text) if (link != null) { href = annotated.model.config.resolveLink(link._1) + link._2