From 9ba50bcdffeb6f66e1e3e28be81a7e4f13fa9910 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 22 Sep 2015 20:40:18 +0300 Subject: [PATCH] Move map- and set-related methods to corresponding parts. --- .../stdlib/src/kotlin/collections/JUtil.kt | 40 ++++--------- .../stdlib/src/kotlin/collections/JUtilJVM.kt | 38 ------------ .../stdlib/src/kotlin/collections/Maps.kt | 7 +++ .../stdlib/src/kotlin/collections/Sets.kt | 59 +++++++++++++++++++ 4 files changed, 78 insertions(+), 66 deletions(-) delete mode 100644 libraries/stdlib/src/kotlin/collections/JUtilJVM.kt create mode 100644 libraries/stdlib/src/kotlin/collections/Sets.kt diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index 18bf9149a3f..c3d5441b85d 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -41,25 +41,10 @@ private object EmptyList : List, Serializable { private fun readResolve(): Any = EmptyList } -private object EmptySet : Set, Serializable { - override fun equals(other: Any?): Boolean = other is Set<*> && other.isEmpty() - override fun hashCode(): Int = 0 - override fun toString(): String = "[]" - override fun size(): Int = 0 - override fun isEmpty(): Boolean = true - override fun contains(o: Any?): Boolean = false - override fun containsAll(c: Collection): Boolean = c.isEmpty() - - override fun iterator(): Iterator = EmptyIterator - - private fun readResolve(): Any = EmptySet -} /** Returns an empty read-only list. The returned list is serializable (JVM). */ public fun emptyList(): List = EmptyList -/** Returns an empty read-only set. The returned set is serializable (JVM). */ -public fun emptySet(): Set = EmptySet /** Returns a new read-only list of given elements. The returned list is serializable (JVM). */ public fun listOf(vararg values: T): List = if (values.size() > 0) arrayListOf(*values) else emptyList() @@ -67,11 +52,12 @@ public fun listOf(vararg values: T): List = if (values.size() > 0) arrayLi /** Returns an empty read-only list. The returned list is serializable (JVM). */ public fun listOf(): List = emptyList() -/** Returns a new read-only ordered set with the given elements. The returned set is serializable (JVM). */ -public fun setOf(vararg values: T): Set = if (values.size() > 0) values.toSet() else emptySet() - -/** Returns an empty read-only set. The returned set is serializable (JVM). */ -public fun setOf(): Set = emptySet() +/** + * Returns an immutable list containing only the specified object [value]. + * The returned list is serializable. + */ +@JvmVersion +public fun listOf(value: T): List = Collections.singletonList(value) /** Returns a new [LinkedList] with the given elements. */ public fun linkedListOf(vararg values: T): LinkedList = values.toCollection(LinkedList()) @@ -79,12 +65,6 @@ public fun linkedListOf(vararg values: T): LinkedList = values.toCollectio /** Returns a new [ArrayList] with the given elements. */ public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size())) -/** Returns a new [HashSet] with the given elements. */ -public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(mapCapacity(values.size()))) - -/** Returns a new [LinkedHashSet] with the given elements. */ -public fun linkedSetOf(vararg values: T): LinkedHashSet = values.toCollection(LinkedHashSet(mapCapacity(values.size()))) - /** Returns a new read-only list either of single given element, if it is not null, or empty list it the element is null. The returned list is serializable (JVM). */ public fun listOfNotNull(value: T?): List = if (value != null) listOf(value) else emptyList() @@ -121,8 +101,12 @@ public fun Collection?.orEmpty(): Collection = this ?: emptyList() /** Returns this List if it's not `null` and the empty list otherwise. */ public fun List?.orEmpty(): List = this ?: emptyList() -/** Returns this Set if it's not `null` and the empty set otherwise. */ -public fun Set?.orEmpty(): Set = this ?: emptySet() +/** + * Returns a list containing the elements returned by this enumeration + * in the order they are returned by the enumeration. + */ +@JvmVersion +public fun Enumeration.toList(): List = Collections.list(this) /** * Returns the size of this iterable if it is known, or `null` otherwise. diff --git a/libraries/stdlib/src/kotlin/collections/JUtilJVM.kt b/libraries/stdlib/src/kotlin/collections/JUtilJVM.kt deleted file mode 100644 index 3a98e756d19..00000000000 --- a/libraries/stdlib/src/kotlin/collections/JUtilJVM.kt +++ /dev/null @@ -1,38 +0,0 @@ -package kotlin - -import java.util.* - - -/** - * Returns an immutable list containing only the specified object [value]. - * The returned list is serializable. - */ -public fun listOf(value: T): List = Collections.singletonList(value) - -/** - * Returns an immutable set containing only the specified object [value]. - * The returned set is serializable. - */ -public fun setOf(value: T): Set = Collections.singleton(value) - -/** - * Returns an immutable map, mapping only the specified key to the - * specified value. The returned map is serializable. - */ -public fun mapOf(keyValuePair: Pair): Map = Collections.singletonMap(keyValuePair.first, keyValuePair.second) - -/** - * Returns a new [SortedSet] with the given elements. - */ -public fun sortedSetOf(vararg values: T): TreeSet = values.toCollection(TreeSet()) - -/** - * Returns a new [SortedSet] with the given [comparator] and elements. - */ -public fun sortedSetOf(comparator: Comparator, vararg values: T): TreeSet = values.toCollection(TreeSet(comparator)) - -/** - * Returns a list containing the elements returned by this enumeration - * in the order they are returned by the enumeration. - */ -public fun Enumeration.toList(): List = Collections.list(this) diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 8cbc17c4744..667dab3e864 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -36,6 +36,13 @@ public fun mapOf(vararg values: Pair): Map = if (values.size() /** Returns an empty read-only map. The returned map is serializable (JVM). */ 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) + /** * Returns a new [HashMap] with the specified contents, given as a list of pairs * where the first component is the key and the second is the value. diff --git a/libraries/stdlib/src/kotlin/collections/Sets.kt b/libraries/stdlib/src/kotlin/collections/Sets.kt new file mode 100644 index 00000000000..184be93177a --- /dev/null +++ b/libraries/stdlib/src/kotlin/collections/Sets.kt @@ -0,0 +1,59 @@ +package kotlin + +import java.io.Serializable +import java.util.* + + +private object EmptySet : Set, Serializable { + override fun equals(other: Any?): Boolean = other is Set<*> && other.isEmpty() + override fun hashCode(): Int = 0 + override fun toString(): String = "[]" + + override fun size(): Int = 0 + override fun isEmpty(): Boolean = true + override fun contains(o: Any?): Boolean = false + override fun containsAll(c: Collection): Boolean = c.isEmpty() + + override fun iterator(): Iterator = EmptyIterator + + private fun readResolve(): Any = EmptySet +} + + +/** Returns an empty read-only set. The returned set is serializable (JVM). */ +public fun emptySet(): Set = EmptySet +/** Returns a new read-only ordered set with the given elements. The returned set is serializable (JVM). */ +public fun setOf(vararg values: T): Set = if (values.size() > 0) values.toSet() else emptySet() + +/** Returns an empty read-only set. The returned set is serializable (JVM). */ +public fun setOf(): Set = emptySet() + + +/** Returns a new [HashSet] with the given elements. */ +public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(mapCapacity(values.size()))) + +/** Returns a new [LinkedHashSet] with the given elements. */ +public fun linkedSetOf(vararg values: T): LinkedHashSet = values.toCollection(LinkedHashSet(mapCapacity(values.size()))) + +/** Returns this Set if it's not `null` and the empty set otherwise. */ +public fun Set?.orEmpty(): Set = this ?: emptySet() + +/** + * Returns an immutable set containing only the specified object [value]. + * The returned set is serializable. + */ +@JvmVersion +public fun setOf(value: T): Set = Collections.singleton(value) + + +/** + * Returns a new [SortedSet] with the given elements. + */ +@JvmVersion +public fun sortedSetOf(vararg values: T): TreeSet = values.toCollection(TreeSet()) + +/** + * Returns a new [SortedSet] with the given [comparator] and elements. + */ +@JvmVersion +public fun sortedSetOf(comparator: Comparator, vararg values: T): TreeSet = values.toCollection(TreeSet(comparator)) \ No newline at end of file