提交 0325e68a 编写于 作者: J James Strachan

#KT-1795 Fixed - added hashMap() and sortedMap() helper functions for creating...

#KT-1795 Fixed - added hashMap() and sortedMap() helper functions for creating maps more easily using tuples
上级 376dc1cb
......@@ -23,14 +23,47 @@ public inline fun hashSet<T>(vararg values: T) : HashSet<T> = values.to(HashSet<
/** Returns a new SortedSet with a variable number of initial elements */
public inline fun sortedSet<T>(vararg values: T) : TreeSet<T> = values.to(TreeSet<T>())
public inline fun <K,V> hashMap(): HashMap<K,V> = HashMap<K,V>()
/**
* 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 <K,V> hashMap(vararg values: #(K,V)): HashMap<K,V> {
val answer = HashMap<K,V>()
/**
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 <K,V> sortedMap(): SortedMap<K,V> = TreeMap<K,V>()
/**
* 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 <K,V> sortedMap(vararg values: #(K,V)): SortedMap<K,V> {
val answer = TreeMap<K,V>()
/**
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 <T> java.util.Collection<T>.toArray() : Array<T> {
val answer = arrayOfNulls<T>(this.size)
var idx = 0
......
......@@ -113,3 +113,12 @@ public inline fun <K,V,R,C: java.util.Map<K,R>> java.util.Map<K,V>.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 <K,V> java.util.Map<K,V>.putAll(vararg values: #(K,V)): Unit {
for (v in values) {
put(v._1, v._2)
}
}
\ No newline at end of file
......@@ -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
......
......@@ -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<String, #(String,String)>()
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册