From 8fd5677fe048f34c61d41f19986a42b713824c50 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 15 Oct 2015 21:47:46 +0300 Subject: [PATCH] Move map-related *Raw methods to Maps.kt and do some cleanup there. Correct deprecation messages and replacements. --- .../stdlib/src/kotlin/collections/Maps.kt | 49 +++++--- .../kotlin/collections/MutableCollections.kt | 108 +++++++++--------- 2 files changed, 83 insertions(+), 74 deletions(-) diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 80df5cd251d..51d4807ca5a 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -25,7 +25,7 @@ private object EmptyMap : Map, Serializable { } /** Returns an empty read-only map of specified type. The returned map is serializable (JVM). */ -public fun emptyMap(): Map = EmptyMap as Map +public fun emptyMap(): Map = EmptyMap as Map /** * Returns a new read-only map with the specified contents, given as a list of pairs @@ -34,17 +34,17 @@ public fun emptyMap(): Map = EmptyMap as Map * * The returned map is serializable (JVM). */ -public fun mapOf(vararg values: Pair): Map = if (values.size() > 0) linkedMapOf(*values) else emptyMap() +public fun mapOf(vararg values: Pair): Map = if (values.size() > 0) linkedMapOf(*values) else emptyMap() /** Returns an empty read-only map. The returned map is serializable (JVM). */ -public fun mapOf(): Map = emptyMap() +public fun mapOf(): Map = emptyMap() /** * Returns an immutable map, mapping only the specified key to the * specified value. The returned map is serializable. */ @JvmVersion -public fun mapOf(keyValuePair: Pair): Map = Collections.singletonMap(keyValuePair.first, keyValuePair.second) +public fun mapOf(keyValuePair: Pair): Map = Collections.singletonMap(keyValuePair.first, keyValuePair.second) /** * Returns a new [HashMap] with the specified contents, given as a list of pairs @@ -103,6 +103,24 @@ public fun Map?.orEmpty() : Map = this ?: emptyMap() */ public operator fun Map.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 Map.getRaw(key: Any?): V? = (this as Map).get(key) + +/** + * Returns `true` if the map contains the specified [key]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Map.containsKeyRaw(key: Any?): Boolean = (this as Map).containsKey(key) + +/** + * Returns `true` if the map maps one or more keys to the specified [value]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Map.containsValueRaw(value: Any?): Boolean = (this as Map).containsValue(value) + /** * Returns the key component of the map entry. * @@ -113,9 +131,8 @@ public operator fun Map.contains(key : K) : Boolean = containsKey(key * } * ``` */ -public operator fun Map.Entry.component1(): K { - return getKey() -} +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Map.Entry.component1(): K = key /** * Returns the value component of the map entry. @@ -126,16 +143,13 @@ public operator fun Map.Entry.component1(): K { * } * ``` */ -public operator fun Map.Entry.component2(): V { - return getValue() -} +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Map.Entry.component2(): V = value /** * Converts entry to [Pair] with key being first component and value being second. */ -public fun Map.Entry.toPair(): Pair { - return Pair(getKey(), getValue()) -} +public fun Map.Entry.toPair(): Pair = Pair(key, value) /** * 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 MutableMap.getOrPut(key: K, defaultValue: () -> V * * @sample test.collections.MapTest.iterateWithProperties */ -public operator fun Map.iterator(): Iterator> { - val entrySet = entrySet() - return entrySet.iterator() -} +public operator fun Map.iterator(): Iterator> = entries.iterator() /** * Populates the given `destination` [Map] with entries having the keys of this map and the values obtained @@ -236,7 +247,7 @@ public fun MutableMap.putAll(values: Sequence>): Unit { * @sample test.collections.MapTest.mapValues */ public inline fun Map.mapValues(transform: (Map.Entry) -> R): Map { - return mapValuesTo(LinkedHashMap(size()), transform) + return mapValuesTo(LinkedHashMap(size), transform) } /** @@ -246,7 +257,7 @@ public inline fun Map.mapValues(transform: (Map.Entry) -> * @sample test.collections.MapTest.mapKeys */ public inline fun Map.mapKeys(transform: (Map.Entry) -> R): Map { - return mapKeysTo(LinkedHashMap(size()), transform) + return mapKeysTo(LinkedHashMap(size), transform) } /** diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 5de6ae83a97..0694f5258ec 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -3,108 +3,106 @@ 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): Boolean = (this as Collection).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 MutableCollection.removeRaw(element: Any?): Boolean = (this as MutableCollection).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 MutableCollection.removeAllRaw(collection: Collection): Boolean = (this as MutableCollection).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 MutableCollection.retainAllRaw(collection: Collection): Boolean = (this as MutableCollection).retainAll(collection) + + + @Deprecated("Use operator 'get' instead", ReplaceWith("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 -@Deprecated("Use property 'size' instead", ReplaceWith("this.size")) +@Deprecated("Use property 'size' instead", ReplaceWith("size")) public inline fun Map<*, *>.size() = size -@Deprecated("Use property 'key' instead", ReplaceWith("this.key")) +@Deprecated("Use property 'key' instead", ReplaceWith("key")) public fun Map.Entry.getKey(): K = key - @Deprecated("Use containsAllRaw() instead.", ReplaceWith("containsAllRaw(collection)")) public fun Collection.containsAll(collection: Collection): Boolean = containsAllRaw(collection) - -/* -@kotlin.jvm.JvmName("containsAny") -@Deprecated("Use containsRaw() instead.", ReplaceWith("containsRaw(element)")) -public operator fun Array.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 Sequence.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): Boolean = (this as Collection).containsAll(collection) - -public fun MutableCollection.removeRaw(element: Any?): Boolean = (this as MutableCollection).remove(element) - -public fun MutableCollection.removeAllRaw(collection: Collection): Boolean = (this as MutableCollection).removeAll(collection) - -public fun MutableCollection.retainAllRaw(collection: Collection): Boolean = (this as MutableCollection).retainAll(collection) - -public fun Map.getRaw(key: Any?): V? = (this as Map).get(key) - -public fun Map.containsKeyRaw(key: Any?): Boolean = (this as Map).containsKey(key) - -public fun Map.containsValueRaw(value: Any?): Boolean = (this as Map).containsValue(value) - -@Deprecated("Use property 'value' instead", ReplaceWith("this.value")) +@Deprecated("Use property 'value' instead.", ReplaceWith("value")) public fun Map.Entry.getValue(): V = value -@Deprecated("Use 'removeAt' instead", ReplaceWith("this.removeAt(index)")) +@Deprecated("Use 'removeAt' instead.", ReplaceWith("removeAt(index)")) public fun MutableList.remove(index: Int): E = removeAt(index) -@Deprecated("Use explicit cast to MutableCollection instead.", ReplaceWith("this.removeRaw(o)")) +@Deprecated("Use 'removeRaw' instead.", ReplaceWith("removeRaw(o)")) public fun MutableCollection.remove(o: Any?): Boolean = removeRaw(o) -@Deprecated("Use explicit cast to MutableCollection instead", ReplaceWith("removeAllRaw(c)")) +@Deprecated("Use 'removeAllRaw' instead.", ReplaceWith("removeAllRaw(c)")) public fun MutableCollection.removeAll(c: Collection): Boolean = removeAllRaw(c) -@Deprecated("Use explicit cast to MutableCollection instead", ReplaceWith("retainAllRaw(c)")) +@Deprecated("Use 'retainAllRaw' instead.", ReplaceWith("retainAllRaw(c)")) public fun MutableCollection.retainAll(c: Collection): Boolean = retainAllRaw(c) -@Deprecated("Use explicit cast to List instead", ReplaceWith("indexOfRaw(o)")) +@Deprecated("Use 'indexOfRaw' instead.", ReplaceWith("indexOfRaw(o)")) public fun List.indexOf(o: Any?): Int = indexOfRaw(o) -@Deprecated("Use explicit cast to List instead", ReplaceWith("lastIndexOfRaw(o)")) +@Deprecated("Use 'lastIndexOfRaw' instead.", ReplaceWith("lastIndexOfRaw(o)")) public fun List.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 -@Deprecated("Use explicit cast to Map instead", ReplaceWith("getRaw(key)")) +@Deprecated("Use 'getRaw' instead.", ReplaceWith("getRaw(key)")) public inline operator fun Map.get(key: Any?): V? = getRaw(key) -@Deprecated("Use explicit cast to Map instead", ReplaceWith("containsKeyRaw(key)")) +@Deprecated("Use 'containsKeyRaw' instead.", ReplaceWith("containsKeyRaw(key)")) public inline fun Map.containsKey(key: Any?): Boolean = containsKeyRaw(key) -@Deprecated("Use explicit cast to Map instead", ReplaceWith("containsValueRaw(value)")) +@Deprecated("Use 'containsValueRaw' instead.", ReplaceWith("containsValueRaw(value)")) public inline fun Map.containsValue(value: Any?): Boolean = containsValueRaw(value) -@Deprecated("Use property 'keys' instead", ReplaceWith("this.keys")) +@Deprecated("Use property 'keys' instead.", ReplaceWith("keys")) public inline fun Map.keySet(): Set = keys @kotlin.jvm.JvmName("mutableKeys") -@Deprecated("Use property 'keys' instead", ReplaceWith("this.keys")) +@Deprecated("Use property 'keys' instead.", ReplaceWith("keys")) public fun MutableMap.keySet(): MutableSet = keys -@Deprecated("Use property 'entries' instead", ReplaceWith("this.entries")) +@Deprecated("Use property 'entries' instead.", ReplaceWith("entries")) public inline fun Map.entrySet(): Set> = entries @kotlin.jvm.JvmName("mutableEntrySet") -@Deprecated("Use property 'entries' instead", ReplaceWith("this.entries")) +@Deprecated("Use property 'entries' instead.", ReplaceWith("entries")) public fun MutableMap.entrySet(): MutableSet> = entries -@Deprecated("Use property 'values' instead", ReplaceWith("this.values")) +@Deprecated("Use property 'values' instead.", ReplaceWith("values")) public inline fun Map.values(): Collection = values @kotlin.jvm.JvmName("mutableValues") -@Deprecated("Use property 'values' instead", ReplaceWith("this.values")) +@Deprecated("Use property 'values' instead.", ReplaceWith("values")) public fun MutableMap.values(): MutableCollection = values /**