Move map-related *Raw methods to Maps.kt and do some cleanup there.

Correct deprecation messages and replacements.
This commit is contained in:
Ilya Gorbunov
2015-10-15 21:47:46 +03:00
parent e2cf8a919c
commit 8fd5677fe0
2 changed files with 83 additions and 74 deletions
+30 -19
View File
@@ -25,7 +25,7 @@ private object EmptyMap : Map<Any, Nothing>, Serializable {
} }
/** Returns an empty read-only map of specified type. The returned map is serializable (JVM). */ /** Returns an empty read-only map of specified type. The returned map is serializable (JVM). */
public fun emptyMap<K, V>(): Map<K, V> = EmptyMap as Map<K, V> public fun <K, V> emptyMap(): Map<K, V> = EmptyMap as Map<K, V>
/** /**
* Returns a new read-only map with the specified contents, given as a list of pairs * Returns a new read-only map with the specified contents, given as a list of pairs
@@ -34,17 +34,17 @@ public fun emptyMap<K, V>(): Map<K, V> = EmptyMap as Map<K, V>
* *
* The returned map is serializable (JVM). * The returned map is serializable (JVM).
*/ */
public fun mapOf<K, V>(vararg values: Pair<K, V>): Map<K, V> = if (values.size() > 0) linkedMapOf(*values) else emptyMap() public fun <K, V> mapOf(vararg values: Pair<K, V>): Map<K, V> = if (values.size() > 0) linkedMapOf(*values) else emptyMap()
/** Returns an empty read-only map. The returned map is serializable (JVM). */ /** Returns an empty read-only map. The returned map is serializable (JVM). */
public fun mapOf<K, V>(): Map<K, V> = emptyMap() public fun <K, V> mapOf(): Map<K, V> = emptyMap()
/** /**
* Returns an immutable map, mapping only the specified key to the * Returns an immutable map, mapping only the specified key to the
* specified value. The returned map is serializable. * specified value. The returned map is serializable.
*/ */
@JvmVersion @JvmVersion
public fun mapOf<K, V>(keyValuePair: Pair<K, V>): Map<K, V> = Collections.singletonMap(keyValuePair.first, keyValuePair.second) public fun <K, V> mapOf(keyValuePair: Pair<K, V>): Map<K, V> = Collections.singletonMap(keyValuePair.first, keyValuePair.second)
/** /**
* Returns a new [HashMap] with the specified contents, given as a list of pairs * Returns a new [HashMap] with the specified contents, given as a list of pairs
@@ -103,6 +103,24 @@ public fun <K,V> Map<K,V>?.orEmpty() : Map<K,V> = this ?: emptyMap()
*/ */
public operator fun <K,V> Map<K,V>.contains(key : K) : Boolean = containsKey(key) public operator fun <K,V> Map<K,V>.contains(key : K) : Boolean = containsKey(key)
/**
* Returns the value corresponding to the given [key], or `null` if such a key is not present in the map.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <K, V> Map<K, V>.getRaw(key: Any?): V? = (this as Map<Any?, V>).get(key)
/**
* Returns `true` if the map contains the specified [key].
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <K> Map<K, *>.containsKeyRaw(key: Any?): Boolean = (this as Map<Any?, *>).containsKey(key)
/**
* Returns `true` if the map maps one or more keys to the specified [value].
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <K> Map<K, *>.containsValueRaw(value: Any?): Boolean = (this as Map<K, Any?>).containsValue(value)
/** /**
* Returns the key component of the map entry. * Returns the key component of the map entry.
* *
@@ -113,9 +131,8 @@ public operator fun <K,V> Map<K,V>.contains(key : K) : Boolean = containsKey(key
* } * }
* ``` * ```
*/ */
public operator fun <K, V> Map.Entry<K, V>.component1(): K { @Suppress("NOTHING_TO_INLINE")
return getKey() public inline operator fun <K, V> Map.Entry<K, V>.component1(): K = key
}
/** /**
* Returns the value component of the map entry. * Returns the value component of the map entry.
@@ -126,16 +143,13 @@ public operator fun <K, V> Map.Entry<K, V>.component1(): K {
* } * }
* ``` * ```
*/ */
public operator fun <K, V> Map.Entry<K, V>.component2(): V { @Suppress("NOTHING_TO_INLINE")
return getValue() public inline operator fun <K, V> Map.Entry<K, V>.component2(): V = value
}
/** /**
* Converts entry to [Pair] with key being first component and value being second. * Converts entry to [Pair] with key being first component and value being second.
*/ */
public fun <K, V> Map.Entry<K, V>.toPair(): Pair<K, V> { public fun <K, V> Map.Entry<K, V>.toPair(): Pair<K, V> = Pair(key, value)
return Pair(getKey(), getValue())
}
/** /**
* Returns the value for the given key, or the result of the [defaultValue] function if there was no entry for the given key. * Returns the value for the given key, or the result of the [defaultValue] function if there was no entry for the given key.
@@ -173,10 +187,7 @@ public inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V
* *
* @sample test.collections.MapTest.iterateWithProperties * @sample test.collections.MapTest.iterateWithProperties
*/ */
public operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> { public operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> = entries.iterator()
val entrySet = entrySet()
return entrySet.iterator()
}
/** /**
* Populates the given `destination` [Map] with entries having the keys of this map and the values obtained * Populates the given `destination` [Map] with entries having the keys of this map and the values obtained
@@ -236,7 +247,7 @@ public fun <K, V> MutableMap<K, V>.putAll(values: Sequence<Pair<K,V>>): Unit {
* @sample test.collections.MapTest.mapValues * @sample test.collections.MapTest.mapValues
*/ */
public inline fun <K, V, R> Map<K, V>.mapValues(transform: (Map.Entry<K, V>) -> R): Map<K, R> { public inline fun <K, V, R> Map<K, V>.mapValues(transform: (Map.Entry<K, V>) -> R): Map<K, R> {
return mapValuesTo(LinkedHashMap<K, R>(size()), transform) return mapValuesTo(LinkedHashMap<K, R>(size), transform)
} }
/** /**
@@ -246,7 +257,7 @@ public inline fun <K, V, R> Map<K, V>.mapValues(transform: (Map.Entry<K, V>) ->
* @sample test.collections.MapTest.mapKeys * @sample test.collections.MapTest.mapKeys
*/ */
public inline fun <K, V, R> Map<K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V> { public inline fun <K, V, R> Map<K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V> {
return mapKeysTo(LinkedHashMap<R, V>(size()), transform) return mapKeysTo(LinkedHashMap<R, V>(size), transform)
} }
/** /**
@@ -3,108 +3,106 @@
package kotlin package kotlin
/**
* Checks if all elements in the specified collection are contained in this collection.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun Collection<*>.containsAllRaw(collection: Collection<Any?>): Boolean = (this as Collection<Any?>).containsAll(collection)
/**
* Removes a single instance of the specified element from this
* collection, if it is present.
*
* @return `true` if the element has been successfully removed; `false` if it was not present in the collection.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <E> MutableCollection<E>.removeRaw(element: Any?): Boolean = (this as MutableCollection<Any?>).remove(element)
/**
* Removes all of this collection's elements that are also contained in the specified collection.
*
* @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <E> MutableCollection<E>.removeAllRaw(collection: Collection<Any?>): Boolean = (this as MutableCollection<Any?>).removeAll(collection)
/**
* Retains only the elements in this collection that are contained in the specified collection.
*
* @return `true` if any element was removed from the collection, `false` if the collection was not modified.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <E> MutableCollection<E>.retainAllRaw(collection: Collection<Any?>): Boolean = (this as MutableCollection<Any?>).retainAll(collection)
@Deprecated("Use operator 'get' instead", ReplaceWith("this[index]")) @Deprecated("Use operator 'get' instead", ReplaceWith("this[index]"))
public fun CharSequence.charAt(index: Int): Char = this[index] public fun CharSequence.charAt(index: Int): Char = this[index]
@Deprecated("Use property 'size' instead", ReplaceWith("this.size")) @Deprecated("Use property 'size' instead", ReplaceWith("size"))
public inline fun Collection<*>.size() = size public inline fun Collection<*>.size() = size
@Deprecated("Use property 'size' instead", ReplaceWith("this.size")) @Deprecated("Use property 'size' instead", ReplaceWith("size"))
public inline fun Map<*, *>.size() = size public inline fun Map<*, *>.size() = size
@Deprecated("Use property 'key' instead", ReplaceWith("this.key")) @Deprecated("Use property 'key' instead", ReplaceWith("key"))
public fun <K, V> Map.Entry<K, V>.getKey(): K = key public fun <K, V> Map.Entry<K, V>.getKey(): K = key
@Deprecated("Use containsAllRaw() instead.", ReplaceWith("containsAllRaw(collection)")) @Deprecated("Use containsAllRaw() instead.", ReplaceWith("containsAllRaw(collection)"))
public fun <E> Collection<E>.containsAll(collection: Collection<Any?>): Boolean = containsAllRaw(collection) public fun <E> Collection<E>.containsAll(collection: Collection<Any?>): Boolean = containsAllRaw(collection)
@Deprecated("Use property 'value' instead.", ReplaceWith("value"))
/*
@kotlin.jvm.JvmName("containsAny")
@Deprecated("Use containsRaw() instead.", ReplaceWith("containsRaw(element)"))
public operator fun <T> Array<T>.contains(element: Any?): Boolean = containsRaw(element)
@kotlin.jvm.JvmName("containsAny")
@Deprecated("Use containsRaw() instead.", ReplaceWith("containsRaw(element)"))
public operator fun Iterable<*>.contains(element: Any?): Boolean = containsRaw(element)
@kotlin.jvm.JvmName("containsAny")
@Deprecated("Use containsRaw() instead.", ReplaceWith("containsRaw(element)"))
public operator fun <T> Sequence<T>.contains(element: Any?): Boolean = containsRaw(element)
*/
/**
* Checks if all elements in the specified collection are contained in this collection.
*/
public fun Collection<*>.containsAllRaw(collection: Collection<Any?>): Boolean = (this as Collection<Any?>).containsAll(collection)
public fun <E> MutableCollection<E>.removeRaw(element: Any?): Boolean = (this as MutableCollection<Any?>).remove(element)
public fun <E> MutableCollection<E>.removeAllRaw(collection: Collection<Any?>): Boolean = (this as MutableCollection<Any?>).removeAll(collection)
public fun <E> MutableCollection<E>.retainAllRaw(collection: Collection<Any?>): Boolean = (this as MutableCollection<Any?>).retainAll(collection)
public fun <K, V> Map<K, V>.getRaw(key: Any?): V? = (this as Map<Any?, V>).get(key)
public fun <K> Map<K, *>.containsKeyRaw(key: Any?): Boolean = (this as Map<Any?, *>).containsKey(key)
public fun <K> Map<K, *>.containsValueRaw(value: Any?): Boolean = (this as Map<K, Any?>).containsValue(value)
@Deprecated("Use property 'value' instead", ReplaceWith("this.value"))
public fun <K, V> Map.Entry<K, V>.getValue(): V = value public fun <K, V> Map.Entry<K, V>.getValue(): V = value
@Deprecated("Use 'removeAt' instead", ReplaceWith("this.removeAt(index)")) @Deprecated("Use 'removeAt' instead.", ReplaceWith("removeAt(index)"))
public fun <E> MutableList<E>.remove(index: Int): E = removeAt(index) public fun <E> MutableList<E>.remove(index: Int): E = removeAt(index)
@Deprecated("Use explicit cast to MutableCollection<Any?> instead.", ReplaceWith("this.removeRaw(o)")) @Deprecated("Use 'removeRaw' instead.", ReplaceWith("removeRaw(o)"))
public fun <E> MutableCollection<E>.remove(o: Any?): Boolean = removeRaw(o) public fun <E> MutableCollection<E>.remove(o: Any?): Boolean = removeRaw(o)
@Deprecated("Use explicit cast to MutableCollection<Any?> instead", ReplaceWith("removeAllRaw(c)")) @Deprecated("Use 'removeAllRaw' instead.", ReplaceWith("removeAllRaw(c)"))
public fun <E> MutableCollection<E>.removeAll(c: Collection<Any?>): Boolean = removeAllRaw(c) public fun <E> MutableCollection<E>.removeAll(c: Collection<Any?>): Boolean = removeAllRaw(c)
@Deprecated("Use explicit cast to MutableCollection<Any?> instead", ReplaceWith("retainAllRaw(c)")) @Deprecated("Use 'retainAllRaw' instead.", ReplaceWith("retainAllRaw(c)"))
public fun <E> MutableCollection<E>.retainAll(c: Collection<Any?>): Boolean = retainAllRaw(c) public fun <E> MutableCollection<E>.retainAll(c: Collection<Any?>): Boolean = retainAllRaw(c)
@Deprecated("Use explicit cast to List<Any?> instead", ReplaceWith("indexOfRaw(o)")) @Deprecated("Use 'indexOfRaw' instead.", ReplaceWith("indexOfRaw(o)"))
public fun <E> List<E>.indexOf(o: Any?): Int = indexOfRaw(o) public fun <E> List<E>.indexOf(o: Any?): Int = indexOfRaw(o)
@Deprecated("Use explicit cast to List<Any?> instead", ReplaceWith("lastIndexOfRaw(o)")) @Deprecated("Use 'lastIndexOfRaw' instead.", ReplaceWith("lastIndexOfRaw(o)"))
public fun <E> List<E>.lastIndexOf(o: Any?): Int = lastIndexOfRaw(o) public fun <E> List<E>.lastIndexOf(o: Any?): Int = lastIndexOfRaw(o)
@Deprecated("Use property 'length' instead", ReplaceWith("this.length")) @Deprecated("Use property 'length' instead.", ReplaceWith("length"))
public fun CharSequence.length(): Int = length public fun CharSequence.length(): Int = length
@Deprecated("Use explicit cast to Map<Any?, V> instead", ReplaceWith("getRaw(key)")) @Deprecated("Use 'getRaw' instead.", ReplaceWith("getRaw(key)"))
public inline operator fun <K, V> Map<K, V>.get(key: Any?): V? = getRaw(key) public inline operator fun <K, V> Map<K, V>.get(key: Any?): V? = getRaw(key)
@Deprecated("Use explicit cast to Map<Any?, V> instead", ReplaceWith("containsKeyRaw(key)")) @Deprecated("Use 'containsKeyRaw' instead.", ReplaceWith("containsKeyRaw(key)"))
public inline fun <K, V> Map<K, V>.containsKey(key: Any?): Boolean = containsKeyRaw(key) public inline fun <K, V> Map<K, V>.containsKey(key: Any?): Boolean = containsKeyRaw(key)
@Deprecated("Use explicit cast to Map<K, Any?> instead", ReplaceWith("containsValueRaw(value)")) @Deprecated("Use 'containsValueRaw' instead.", ReplaceWith("containsValueRaw(value)"))
public inline fun <K, V> Map<K, V>.containsValue(value: Any?): Boolean = containsValueRaw(value) public inline fun <K, V> Map<K, V>.containsValue(value: Any?): Boolean = containsValueRaw(value)
@Deprecated("Use property 'keys' instead", ReplaceWith("this.keys")) @Deprecated("Use property 'keys' instead.", ReplaceWith("keys"))
public inline fun <K, V> Map<K, V>.keySet(): Set<K> = keys public inline fun <K, V> Map<K, V>.keySet(): Set<K> = keys
@kotlin.jvm.JvmName("mutableKeys") @kotlin.jvm.JvmName("mutableKeys")
@Deprecated("Use property 'keys' instead", ReplaceWith("this.keys")) @Deprecated("Use property 'keys' instead.", ReplaceWith("keys"))
public fun <K, V> MutableMap<K, V>.keySet(): MutableSet<K> = keys public fun <K, V> MutableMap<K, V>.keySet(): MutableSet<K> = keys
@Deprecated("Use property 'entries' instead", ReplaceWith("this.entries")) @Deprecated("Use property 'entries' instead.", ReplaceWith("entries"))
public inline fun <K, V> Map<K, V>.entrySet(): Set<Map.Entry<K, V>> = entries public inline fun <K, V> Map<K, V>.entrySet(): Set<Map.Entry<K, V>> = entries
@kotlin.jvm.JvmName("mutableEntrySet") @kotlin.jvm.JvmName("mutableEntrySet")
@Deprecated("Use property 'entries' instead", ReplaceWith("this.entries")) @Deprecated("Use property 'entries' instead.", ReplaceWith("entries"))
public fun <K, V> MutableMap<K, V>.entrySet(): MutableSet<MutableMap.MutableEntry<K, V>> = entries public fun <K, V> MutableMap<K, V>.entrySet(): MutableSet<MutableMap.MutableEntry<K, V>> = entries
@Deprecated("Use property 'values' instead", ReplaceWith("this.values")) @Deprecated("Use property 'values' instead.", ReplaceWith("values"))
public inline fun <K, V> Map<K, V>.values(): Collection<V> = values public inline fun <K, V> Map<K, V>.values(): Collection<V> = values
@kotlin.jvm.JvmName("mutableValues") @kotlin.jvm.JvmName("mutableValues")
@Deprecated("Use property 'values' instead", ReplaceWith("this.values")) @Deprecated("Use property 'values' instead.", ReplaceWith("values"))
public fun <K, V> MutableMap<K, V>.values(): MutableCollection<V> = values public fun <K, V> MutableMap<K, V>.values(): MutableCollection<V> = values
/** /**