From 5ab2f471017a9aec667a908f7536dde60088a9be Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sun, 22 Nov 2015 09:52:56 +0300 Subject: [PATCH] Make MutableCollection extensions return Boolean indicating whether the operation resulted collection to be changed. --- .../kotlin/collections/MutableCollections.kt | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 04756efcbbd..5de72445502 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -174,6 +174,19 @@ public operator fun MutableCollection.minusAssign(elements: Sequence MutableCollection.addAll(elements: Iterable): Boolean { + when (elements) { + is Collection -> return addAll(elements) + else -> { + var result: Boolean = false + for (item in elements) + if (add(item)) result = true + return result + } + } +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.addAll(elements: Iterable) { when (elements) { is Collection -> addAll(elements) @@ -184,6 +197,15 @@ public fun MutableCollection.addAll(elements: Iterable) { /** * Adds all elements of the given [elements] sequence to this [MutableCollection]. */ +public fun MutableCollection.addAll(elements: Sequence): Boolean { + var result: Boolean = false + for (item in elements) { + if (add(item)) result = true + } + return result +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.addAll(elements: Sequence) { for (item in elements) add(item) } @@ -191,6 +213,11 @@ public fun MutableCollection.addAll(elements: Sequence) { /** * Adds all elements of the given [elements] array to this [MutableCollection]. */ +public fun MutableCollection.addAll(elements: Array): Boolean { + return addAll(elements.asList()) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.addAll(elements: Array) { addAll(elements.asList()) } @@ -198,6 +225,12 @@ public fun MutableCollection.addAll(elements: Array) { /** * Removes all elements from this [MutableCollection] that are also contained in the given [elements] collection. */ + +public fun MutableCollection.removeAll(elements: Iterable): Boolean { + return removeAll(elements.convertToSetForSetOperationWith(this)) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.removeAll(elements: Iterable) { removeAll(elements.convertToSetForSetOperationWith(this)) } @@ -205,6 +238,12 @@ public fun MutableCollection.removeAll(elements: Iterable) { /** * Removes all elements from this [MutableCollection] that are also contained in the given [elements] sequence. */ +public fun MutableCollection.removeAll(elements: Sequence): Boolean { + val set = elements.toHashSet() + return set.isNotEmpty() && removeAll(set) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.removeAll(elements: Sequence) { val set = elements.toHashSet() if (set.isNotEmpty()) @@ -214,6 +253,11 @@ public fun MutableCollection.removeAll(elements: Sequence) { /** * Removes all elements from this [MutableCollection] that are also contained in the given [elements] array. */ +public fun MutableCollection.removeAll(elements: Array): Boolean { + return elements.isNotEmpty() && removeAll(elements.toHashSet()) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.removeAll(elements: Array) { if (elements.isNotEmpty()) removeAll(elements.toHashSet()) @@ -224,6 +268,11 @@ public fun MutableCollection.removeAll(elements: Array) { /** * Retains only elements of this [MutableCollection] that are contained in the given [elements] collection. */ +public fun MutableCollection.retainAll(elements: Iterable): Boolean { + return retainAll(elements.convertToSetForSetOperationWith(this)) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.retainAll(elements: Iterable) { retainAll(elements.convertToSetForSetOperationWith(this)) } @@ -231,6 +280,14 @@ public fun MutableCollection.retainAll(elements: Iterable) { /** * Retains only elements of this [MutableCollection] that are contained in the given [elements] array. */ +public fun MutableCollection.retainAll(elements: Array): Boolean { + if (elements.isNotEmpty()) + return retainAll(elements.toHashSet()) + else + return retainNothing() +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.retainAll(elements: Array) { if (elements.isNotEmpty()) retainAll(elements.toHashSet()) @@ -242,6 +299,15 @@ public fun MutableCollection.retainAll(elements: Array) { /** * Retains only elements of this [MutableCollection] that are contained in the given [elements] sequence. */ +public fun MutableCollection.retainAll(elements: Sequence): Boolean { + val set = elements.toHashSet() + if (set.isNotEmpty()) + return retainAll(set) + else + return retainNothing() +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun MutableCollection.retainAll(elements: Sequence) { val set = elements.toHashSet() if (set.isNotEmpty()) @@ -250,6 +316,12 @@ public fun MutableCollection.retainAll(elements: Sequence) { clear() } +private fun MutableCollection<*>.retainNothing(): Boolean { + val result = isNotEmpty() + clear() + return result +} + /** * Sorts elements in the list in-place according to their natural sort order. * */