Change removeAll extension implementation to match the contract of member removeAll.
Add retainAll for sequences.
This commit is contained in:
@@ -47,31 +47,31 @@ public fun <T> MutableCollection<in T>.addAll(array: Array<out T>) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements of the given [iterable] from this [MutableCollection].
|
||||
* Removes all elements from this [MutableCollection] that are also contained in the given [iterable].
|
||||
*/
|
||||
public fun <T> MutableCollection<in T>.removeAll(iterable: Iterable<T>) {
|
||||
when (iterable) {
|
||||
is Collection -> removeAll(iterable)
|
||||
else -> for (item in iterable) remove(item)
|
||||
else -> removeAll(iterable.toHashSet())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements of the given [sequence] from this [MutableCollection].
|
||||
* Removes all elements from this [MutableCollection] that are also contained in the given [sequence].
|
||||
*/
|
||||
public fun <T> MutableCollection<in T>.removeAll(sequence: Sequence<T>) {
|
||||
for (item in sequence) remove(item)
|
||||
removeAll(sequence.toHashSet())
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements of the given [array] from this [MutableCollection].
|
||||
* Removes all elements from this [MutableCollection] that are also contained in the given [array].
|
||||
*/
|
||||
public fun <T> MutableCollection<in T>.removeAll(array: Array<out T>) {
|
||||
for (item in array) remove(item)
|
||||
removeAll(array.toHashSet())
|
||||
}
|
||||
|
||||
/**
|
||||
* Retains only elements of the given [iterable] in this [MutableCollection].
|
||||
* Retains only elements of this [MutableCollection] that are contained in the given [iterable].
|
||||
*/
|
||||
public fun <T> MutableCollection<in T>.retainAll(iterable: Iterable<T>) {
|
||||
when (iterable) {
|
||||
@@ -81,8 +81,15 @@ public fun <T> MutableCollection<in T>.retainAll(iterable: Iterable<T>) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retains only elements of the given [array] in this [MutableCollection].
|
||||
* Retains only elements of this [MutableCollection] that are contained in the given [array].
|
||||
*/
|
||||
public fun <T> MutableCollection<in T>.retainAll(array: Array<out T>) {
|
||||
retainAll(array.toHashSet())
|
||||
}
|
||||
|
||||
/**
|
||||
* Retains only elements of this [MutableCollection] that are contained in the given [sequence].
|
||||
*/
|
||||
public fun <T> MutableCollection<in T>.retainAll(sequence: Sequence<T>) {
|
||||
retainAll(sequence.toHashSet())
|
||||
}
|
||||
|
||||
@@ -12,6 +12,29 @@ import org.junit.Test as test
|
||||
|
||||
class CollectionJVMTest {
|
||||
|
||||
private fun <T> identitySetOf(vararg values: T): MutableSet<T> {
|
||||
val map = IdentityHashMap<T, String>()
|
||||
values.forEach { map.put(it, "") }
|
||||
return map.keySet()
|
||||
}
|
||||
|
||||
private data class IdentityData(public val value: Int)
|
||||
|
||||
test fun removeAllWithDifferentEquality() {
|
||||
val data = listOf(IdentityData(1), IdentityData(1))
|
||||
val list = data.toArrayList()
|
||||
list -= identitySetOf(data[0]) as Iterable<IdentityData>
|
||||
assertTrue(list.single() === data[1], "Identity contains should be used")
|
||||
|
||||
val list2 = data.toArrayList()
|
||||
list2 -= hashSetOf(data[0]) as Iterable<IdentityData>
|
||||
assertTrue(list2.isEmpty(), "Equality contains should be used")
|
||||
|
||||
val set3: MutableSet<IdentityData> = identitySetOf(*data.toTypedArray())
|
||||
set3 -= arrayOf(data[1])
|
||||
assertTrue(set3.isEmpty(), "Array doesn't have contains, equality contains is used instead")
|
||||
}
|
||||
|
||||
test fun flatMap() {
|
||||
val data = listOf("", "foo", "bar", "x", "")
|
||||
val characters = data.flatMap { it.toCharList() }
|
||||
|
||||
@@ -8,22 +8,44 @@ import org.junit.Test as test
|
||||
|
||||
class MutableCollectionTest {
|
||||
|
||||
test fun fromIterable() {
|
||||
val data: Iterable<String> = listOf("foo", "bar")
|
||||
// TODO: Use apply scope function
|
||||
|
||||
val collection = ArrayList<String>()
|
||||
collection.addAll(data)
|
||||
test fun addAll() {
|
||||
val data = listOf("foo", "bar")
|
||||
|
||||
assertEquals(data, collection)
|
||||
fun assertAdd(f: MutableList<String>.() -> Unit) = assertEquals(data, arrayListOf<String>().let { it.f(); it })
|
||||
|
||||
assertAdd { addAll(data) }
|
||||
assertAdd { addAll(data.toTypedArray()) }
|
||||
assertAdd { addAll(data.toTypedArray().asIterable()) }
|
||||
assertAdd { addAll(data.asSequence()) }
|
||||
}
|
||||
|
||||
test fun fromSequence() {
|
||||
val list = listOf("foo", "bar")
|
||||
val collection = ArrayList<String>()
|
||||
test fun removeAll() {
|
||||
val content = arrayOf("foo", "bar", "bar")
|
||||
val data = listOf("bar")
|
||||
val expected = listOf("foo")
|
||||
|
||||
collection.addAll(list.asSequence())
|
||||
fun assertRemove(f: MutableList<String>.() -> Unit) = assertEquals(expected, arrayListOf(*content).let { it.f(); it })
|
||||
|
||||
assertEquals(list, collection)
|
||||
assertRemove { removeAll(data) }
|
||||
assertRemove { removeAll(data.toTypedArray()) }
|
||||
assertRemove { removeAll(data.toTypedArray().asIterable()) }
|
||||
assertRemove { removeAll(data.asSequence()) }
|
||||
}
|
||||
|
||||
|
||||
test fun retainAll() {
|
||||
val content = arrayOf("foo", "bar", "bar")
|
||||
val data = listOf("bar")
|
||||
val expected = listOf("bar", "bar")
|
||||
|
||||
fun assertRetain(f: MutableList<String>.() -> Unit) = assertEquals(expected, arrayListOf(*content).let { it.f(); it })
|
||||
|
||||
assertRetain { retainAll(data) }
|
||||
assertRetain { retainAll(data.toTypedArray()) }
|
||||
assertRetain { retainAll(data.toTypedArray().asIterable()) }
|
||||
assertRetain { retainAll(data.asSequence()) }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user