Breaking change. Map operations revisited.
This commit is contained in:
@@ -431,19 +431,6 @@ public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
|
||||
return filterTo(ArrayList<T>(), predicate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all key-value pairs matching the given *predicate*
|
||||
*/
|
||||
public inline fun <K, V> Map<K, V>.filter(predicate: (Map.Entry<K, V>) -> Boolean): Map<K, V> {
|
||||
val result = HashMap<K,V>()
|
||||
for (entry in this) {
|
||||
if (predicate(entry)) {
|
||||
result.put(entry.key, entry.value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a stream containing all elements matching the given *predicate*
|
||||
*/
|
||||
@@ -528,13 +515,6 @@ public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T>
|
||||
return filterNotTo(ArrayList<T>(), predicate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements not matching the given *predicate*
|
||||
*/
|
||||
public inline fun <K, V> Map<K, V>.filterNot(predicate: (Map.Entry<K, V>) -> Boolean): List<Map.Entry<K, V>> {
|
||||
return filterNotTo(ArrayList<Map.Entry<K, V>>(), predicate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a stream containing all elements not matching the given *predicate*
|
||||
*/
|
||||
@@ -674,14 +654,6 @@ public inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterNotTo(desti
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements not matching the given *predicate* to the given *destination*
|
||||
*/
|
||||
public inline fun <K, V, C : MutableCollection<in Map.Entry<K, V>>> Map<K, V>.filterNotTo(destination: C, predicate: (Map.Entry<K, V>) -> Boolean): C {
|
||||
for (element in this) if (!predicate(element)) destination.add(element)
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements not matching the given *predicate* to the given *destination*
|
||||
*/
|
||||
@@ -778,14 +750,6 @@ public inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destinat
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements matching the given *predicate* into the given *destination*
|
||||
*/
|
||||
public inline fun <K, V, C : MutableCollection<in Map.Entry<K, V>>> Map<K, V>.filterTo(destination: C, predicate: (Map.Entry<K, V>) -> Boolean): C {
|
||||
for (element in this) if (predicate(element)) destination.add(element)
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements matching the given *predicate* into the given *destination*
|
||||
*/
|
||||
|
||||
@@ -6,37 +6,37 @@ import java.util.HashMap
|
||||
// Map APIs
|
||||
|
||||
/** Returns the size of the map */
|
||||
public val Map<*,*>.size : Int
|
||||
get() = size()
|
||||
public val Map<*, *>.size: Int
|
||||
get() = size()
|
||||
|
||||
/** Returns true if this map is empty */
|
||||
public val Map<*,*>.empty : Boolean
|
||||
get() = isEmpty()
|
||||
public val Map<*, *>.empty: Boolean
|
||||
get() = isEmpty()
|
||||
|
||||
/** Provides [] access to maps */
|
||||
public fun <K, V> MutableMap<K, V>.set(key : K, value : V) : V? = this.put(key, value)
|
||||
public fun <K, V> MutableMap<K, V>.set(key: K, value: V): V? = this.put(key, value)
|
||||
|
||||
/** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */
|
||||
public fun <K,V> Map<K,V>?.orEmpty() : Map<K,V>
|
||||
= if (this != null) this else stdlib_emptyMap()
|
||||
= if (this != null) this else stdlib_emptyMap()
|
||||
|
||||
public fun <K,V> Map<K,V>.contains(key : K) : Boolean = containsKey(key)
|
||||
|
||||
/** Returns the key of the entry */
|
||||
public val <K,V> Map.Entry<K,V>.key : K
|
||||
public val <K, V> Map.Entry<K, V>.key: K
|
||||
get() = getKey()
|
||||
|
||||
/** Returns the value of the entry */
|
||||
public val <K,V> Map.Entry<K,V>.value : V
|
||||
public val <K, V> Map.Entry<K, V>.value: V
|
||||
get() = getValue()
|
||||
|
||||
/** Returns the key of the entry */
|
||||
fun <K,V> Map.Entry<K,V>.component1() : K {
|
||||
fun <K, V> Map.Entry<K, V>.component1(): K {
|
||||
return getKey()
|
||||
}
|
||||
|
||||
/** Returns the value of the entry */
|
||||
fun <K,V> Map.Entry<K,V>.component2() : V {
|
||||
fun <K, V> Map.Entry<K, V>.component2(): V {
|
||||
return getValue()
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ fun <K,V> Map.Entry<K,V>.component2() : V {
|
||||
*
|
||||
* @includeFunctionBody ../../test/collections/MapTest.kt getOrElse
|
||||
*/
|
||||
public inline fun <K,V> Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
|
||||
public inline fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V): V {
|
||||
if (this.containsKey(key)) {
|
||||
return this.get(key) as V
|
||||
} else {
|
||||
@@ -58,7 +58,7 @@ public inline fun <K,V> Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
|
||||
*
|
||||
* @includeFunctionBody ../../test/collections/MapTest.kt getOrPut
|
||||
*/
|
||||
public inline fun <K,V> MutableMap<K,V>.getOrPut(key: K, defaultValue: ()-> V) : V {
|
||||
public inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V {
|
||||
if (this.containsKey(key)) {
|
||||
return this.get(key) as V
|
||||
} else {
|
||||
@@ -68,63 +68,54 @@ public inline fun <K,V> MutableMap<K,V>.getOrPut(key: K, defaultValue: ()-> V) :
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an [[Iterator]] over the entries in the [[Map]]
|
||||
*
|
||||
* @includeFunctionBody ../../test/collections/MapTest.kt iterateWithProperties
|
||||
*/
|
||||
public fun <K,V> Map<K,V>.iterator(): Iterator<Map.Entry<K,V>> {
|
||||
public fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> {
|
||||
val entrySet = this.entrySet()
|
||||
return entrySet.iterator()
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the given *result* [[Map]] with the value returned by applying the *transform* function on each [[Map.Entry]] in this [[Map]]
|
||||
* Populates the given *destination* [[Map]] with the value returned by applying the *transform* function on each [[Map.Entry]] in this [[Map]]
|
||||
*/
|
||||
public inline fun <K,V,R,C: MutableMap<K,R>> Map<K,V>.mapValuesTo(result: C, transform : (Map.Entry<K,V>) -> R) : C {
|
||||
for (e in this) {
|
||||
val newValue = transform(e)
|
||||
result.put(e.key, newValue)
|
||||
}
|
||||
return result
|
||||
public inline fun <K, V, R, C : MutableMap<K, R>> Map<K, V>.mapValuesTo(destination: C, transform: (Map.Entry<K, V>) -> R): C {
|
||||
for (e in this) {
|
||||
val newValue = transform(e)
|
||||
destination.put(e.key, newValue)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the given *destination* [[Map]] with the value returned by applying the *transform* function on each [[Map.Entry]] in this [[Map]]
|
||||
*/
|
||||
public inline fun <K, V, R, C : MutableMap<R, V>> Map<K, V>.mapKeysTo(destination: C, transform: (Map.Entry<K, V>) -> R): C {
|
||||
for (e in this) {
|
||||
val newKey = transform(e)
|
||||
destination.put(newKey, e.value)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ((key, value) in values) {
|
||||
put(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the entries in this [[Map]] to the given mutable *map*
|
||||
* 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, C : MutableMap<K, in V>> Map<K,V>.toMap(map: C): C {
|
||||
map.putAll(this)
|
||||
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)
|
||||
public fun <K, V> MutableMap<K, V>.putAll(values: Iterable<Pair<K,V>>): Unit {
|
||||
for ((key, value) in values) {
|
||||
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>())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,6 +123,98 @@ public fun <K,V> Iterable<Pair<K,V>>.toMap() : Map<K,V> {
|
||||
*
|
||||
* @includeFunctionBody ../../test/collections/MapTest.kt mapValues
|
||||
*/
|
||||
public inline fun <K,V,R> Map<K,V>.mapValues(transform : (Map.Entry<K,V>) -> R): Map<K,R> {
|
||||
return mapValuesTo(java.util.HashMap<K,R>(this.size), transform)
|
||||
public inline fun <K, V, R> Map<K, V>.mapValues(transform: (Map.Entry<K, V>) -> R): Map<K, R> {
|
||||
return mapValuesTo(HashMap<K, R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
|
||||
*
|
||||
* @includeFunctionBody ../../test/collections/MapTest.kt mapKeys
|
||||
*/
|
||||
public inline fun <K, V, R> Map<K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V> {
|
||||
return mapKeysTo(HashMap<R, V>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all key-value pairs matching keys with the given *predicate*
|
||||
*/
|
||||
public inline fun <K, V> Map<K, V>.filterKeys(predicate: (K) -> Boolean): Map<K, V> {
|
||||
val result = HashMap<K, V>()
|
||||
for (entry in this) {
|
||||
if (predicate(entry.key)) {
|
||||
result.put(entry.key, entry.value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all key-value pairs matching values with the given *predicate*
|
||||
*/
|
||||
public inline fun <K, V> Map<K, V>.filterValues(predicate: (V) -> Boolean): Map<K, V> {
|
||||
val result = HashMap<K, V>()
|
||||
for (entry in this) {
|
||||
if (predicate(entry.value)) {
|
||||
result.put(entry.key, entry.value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends all elements matching the given *predicate* into the given *destination*
|
||||
*/
|
||||
public inline fun <K, V, C : MutableMap<K, V>> Map<K, V>.filterTo(destination: C, predicate: (Map.Entry<K, V>) -> Boolean): C {
|
||||
for (element in this) {
|
||||
if (predicate(element)) {
|
||||
destination.put(element.key, element.value)
|
||||
}
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all key-value pairs matching the given *predicate*
|
||||
*/
|
||||
public inline fun <K, V> Map<K, V>.filter(predicate: (Map.Entry<K, V>) -> Boolean): Map<K, V> {
|
||||
return filterTo(HashMap<K, V>(), predicate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements matching the given *predicate* into the given *destination*
|
||||
*/
|
||||
public inline fun <K, V, C : MutableMap<K, V>> Map<K, V>.filterNotTo(destination: C, predicate: (Map.Entry<K, V>) -> Boolean): C {
|
||||
for (element in this) {
|
||||
if (!predicate(element)) {
|
||||
destination.put(element.key, element.value)
|
||||
}
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all key-value pairs matching the given *predicate*
|
||||
*/
|
||||
public inline fun <K, V> Map<K, V>.filterNot(predicate: (Map.Entry<K, V>) -> Boolean): Map<K, V> {
|
||||
return filterNotTo(HashMap<K, V>(), predicate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends given [pair] to the mutable map.
|
||||
*/
|
||||
public fun <K, V> MutableMap<K, V>.plusAssign(pair: Pair<K, V>) {
|
||||
put(pair.first, pair.second)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all key-value pairs from the given collection
|
||||
*/
|
||||
public fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V> {
|
||||
val result = HashMap<K, V>()
|
||||
for (element in this) {
|
||||
result.put(element.first, element.second)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -11,14 +11,14 @@ import java.util.Properties
|
||||
/**
|
||||
* Converts this [[Map]] to a [[LinkedHashMap]] so future insertion orders are maintained
|
||||
*/
|
||||
public fun <K,V> Map<K,V>.toLinkedMap(): LinkedHashMap<K,V> = toMap(LinkedHashMap<K,V>(size))
|
||||
public fun <K, V> Map<K, V>.toLinkedMap(): LinkedHashMap<K, V> = LinkedHashMap(this)
|
||||
|
||||
/**
|
||||
* Converts this [[Map]] to a [[SortedMap]] so iteration order will be in key order
|
||||
*
|
||||
* @includeFunctionBody ../../test/collections/MapTest.kt toSortedMap
|
||||
*/
|
||||
public fun <K,V> Map<K,V>.toSortedMap(): SortedMap<K,V> = toMap(TreeMap<K,V>())
|
||||
public fun <K, V> Map<K, V>.toSortedMap(): SortedMap<K, V> = TreeMap(this)
|
||||
|
||||
/**
|
||||
* Converts this [[Map]] to a [[SortedMap]] using the given *comparator* so that iteration order will be in the order
|
||||
@@ -26,8 +26,11 @@ public fun <K,V> Map<K,V>.toSortedMap(): SortedMap<K,V> = toMap(TreeMap<K,V>())
|
||||
*
|
||||
* @includeFunctionBody ../../test/collections/MapTest.kt toSortedMapWithComparator
|
||||
*/
|
||||
public fun <K,V> Map<K,V>.toSortedMap(comparator: Comparator<K>): SortedMap<K,V> = toMap(TreeMap<K,V>(comparator))
|
||||
|
||||
public fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<K>): SortedMap<K, V> {
|
||||
val result = TreeMap<K, V>(comparator)
|
||||
result.putAll(this)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this [[Map]] to a [[Properties]] object
|
||||
|
||||
Reference in New Issue
Block a user