Rename toMap and toMapBy to associate and associateBy

This commit is contained in:
Ilya Gorbunov
2016-01-20 07:11:16 +03:00
parent 45c9cc4add
commit 8224a4e186
11 changed files with 622 additions and 416 deletions
+2 -2
View File
@@ -170,11 +170,11 @@ fun mapDefinitions(repository: Repository, definitions: Iterable<InterfaceDefini
definitions.filter { "NoInterfaceObject" !in it.extendedAttributes.map { it.call } }.map { generateTrait(repository, it) }
fun generateUnions(ifaces: List<GenerateTraitOrClass>, typedefs: Iterable<TypedefDefinition>): GenerateUnionTypes {
val declaredTypes = ifaces.toMapBy { it.name }
val declaredTypes = ifaces.associateBy { it.name }
val anonymousUnionTypes = collectUnionTypes(declaredTypes)
val anonymousUnionTypeTraits = generateUnionTypeTraits(anonymousUnionTypes)
val anonymousUnionsMap = anonymousUnionTypeTraits.toMapBy { it.name }
val anonymousUnionsMap = anonymousUnionTypeTraits.associateBy { it.name }
val typedefsToBeGenerated = typedefs.filter { it.types is UnionType }
.map { NamedValue(it.name, it.types as UnionType) }
@@ -227,7 +227,7 @@ private fun Pair<String, String>.betterName() = if (((0..9).map { it.toString()
fun <K, V> List<Pair<K, V>>.toMultiMap(): Map<K, List<V>> = groupBy { it.first }.mapValues { it.value.map { it.second } }
fun Appendable.render(namespace: String, ifaces: List<GenerateTraitOrClass>, unions : GenerateUnionTypes) {
val declaredTypes = ifaces.toMapBy { it.name }
val declaredTypes = ifaces.associateBy { it.name }
val allTypes = declaredTypes + unions.anonymousUnionsMap + unions.typedefsMarkersMap
declaredTypes.values.filter { it.namespace == namespace }.forEach {
@@ -111,6 +111,15 @@ fun snapshots(): List<GenericFunction> {
typeParam("V")
returns("Map<K, V>")
annotations("""@kotlin.jvm.JvmName("toMapOfPairs")""")
deprecate(Deprecation("Use associate instead.", replaceWith = "associate(transform)"))
}
templates add f("associate(transform: (T) -> Pair<K, V>)") {
inline(true)
include(CharSequences)
typeParam("K")
typeParam("V")
returns("Map<K, V>")
doc { f ->
"""
Returns a [Map] containing key-value pairs provided by [transform] function applied to ${f.element}s of the given ${f.collection}.
@@ -159,13 +168,21 @@ fun snapshots(): List<GenericFunction> {
}
templates add f("toMapBy(selector: (T) -> K)") {
inline(true)
typeParam("K")
returns("Map<K, T>")
include(CharSequences)
deprecate(Deprecation("Use associateBy instead.", replaceWith = "associateBy(selector)"))
}
templates add f("associateBy(keySelector: (T) -> K)") {
inline(true)
typeParam("K")
doc { f ->
"""
Returns a [Map] containing the ${f.element.pluralize()} from the given ${f.collection} indexed by the key
returned from [selector] function applied to each ${f.element}.
If any two ${f.element.pluralize()} would have the same key returned by [selector] the last one gets added to the map.
returned from [keySelector] function applied to each ${f.element}.
If any two ${f.element.pluralize()} would have the same key returned by [keySelector] the last one gets added to the map.
"""
}
returns("Map<K, T>")
@@ -180,7 +197,7 @@ fun snapshots(): List<GenericFunction> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(keySelector(element), element)
}
return result
"""
@@ -189,7 +206,7 @@ fun snapshots(): List<GenericFunction> {
"""
val result = LinkedHashMap<K, T>()
for (element in this) {
result.put(selector(element), element)
result.put(keySelector(element), element)
}
return result
"""
@@ -199,7 +216,7 @@ fun snapshots(): List<GenericFunction> {
val capacity = (length/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(keySelector(element), element)
}
return result
"""
@@ -209,7 +226,7 @@ fun snapshots(): List<GenericFunction> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
result.put(keySelector(element), element)
}
return result
"""
@@ -228,17 +245,26 @@ fun snapshots(): List<GenericFunction> {
"""
}
returns("Map<K, V>")
deprecate(Deprecation("Use toMapBy instead.", "toMapBy(selector, transform)"))
deprecate(Deprecation("Use associateBy instead.", "associateBy(selector, transform)"))
}
templates add f("toMapBy(selector: (T) -> K, transform: (T) -> V)") {
inline(true)
typeParam("K")
typeParam("V")
include(CharSequences)
returns("Map<K, V>")
deprecate(Deprecation("Use associateBy instead.", replaceWith = "associateBy(selector, transform)"))
}
templates add f("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") {
inline(true)
typeParam("K")
typeParam("V")
doc { f ->
"""
Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to ${f.element.pluralize()} of the given ${f.collection}.
If any two ${f.element.pluralize()} would have the same key returned by [selector] the last one gets added to the map.
Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to ${f.element.pluralize()} of the given ${f.collection}.
If any two ${f.element.pluralize()} would have the same key returned by [keySelector] the last one gets added to the map.
"""
}
returns("Map<K, V>")
@@ -253,7 +279,7 @@ fun snapshots(): List<GenericFunction> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(keySelector(element), valueTransform(element))
}
return result
"""
@@ -262,7 +288,7 @@ fun snapshots(): List<GenericFunction> {
"""
val result = LinkedHashMap<K, V>()
for (element in this) {
result.put(selector(element), transform(element))
result.put(keySelector(element), valueTransform(element))
}
return result
"""
@@ -272,7 +298,7 @@ fun snapshots(): List<GenericFunction> {
val capacity = (length/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(keySelector(element), valueTransform(element))
}
return result
"""
@@ -282,7 +308,7 @@ fun snapshots(): List<GenericFunction> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
result.put(keySelector(element), valueTransform(element))
}
return result
"""