From 9b3d357371d7c2213aad2bad16997a423e32cf0b Mon Sep 17 00:00:00 2001 From: Christian Laakmann Date: Fri, 24 Jan 2014 18:12:45 +0400 Subject: [PATCH] added extension methods MutableCollection#addAll(Iterable) and MutableCollection#addAll(Iterator) (#KT-4202 fixed) --- .../stdlib/src/kotlin/MutableCollections.kt | 15 ++++++++++ .../stdlib/test/MutableCollectionsTest.kt | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 libraries/stdlib/src/kotlin/MutableCollections.kt create mode 100644 libraries/stdlib/test/MutableCollectionsTest.kt diff --git a/libraries/stdlib/src/kotlin/MutableCollections.kt b/libraries/stdlib/src/kotlin/MutableCollections.kt new file mode 100644 index 00000000000..fccea3c9d21 --- /dev/null +++ b/libraries/stdlib/src/kotlin/MutableCollections.kt @@ -0,0 +1,15 @@ +package kotlin + +/** + * Adds all elements of the given *iterator* to this [[MutableCollection]] + */ +public fun MutableCollection.addAll(iterator: Iterator): Unit { + for (e in iterator) add(e) +} + +/** + * Adds all elements of the given *iterable* to this [[MutableCollection]] + */ +public fun MutableCollection.addAll(iterable: Iterable): Unit { + for (e in iterable) add(e) +} diff --git a/libraries/stdlib/test/MutableCollectionsTest.kt b/libraries/stdlib/test/MutableCollectionsTest.kt new file mode 100644 index 00000000000..86b33d126db --- /dev/null +++ b/libraries/stdlib/test/MutableCollectionsTest.kt @@ -0,0 +1,29 @@ +package test.collections + +import kotlin.test.* + +import java.util.* + +import org.junit.Test as test + +class MutableCollectionTest { + + test fun fromIterable() { + val data: Iterable = arrayListOf("foo", "bar") + + val collection = ArrayList() + collection.addAll(data) + + assertEquals(data, collection) + } + + test fun fromIterator() { + val list = arrayListOf("foo", "bar") + val collection = ArrayList() + + collection.addAll(list.iterator()) + + assertEquals(list, collection) + } + +}