From 4d13ea89b2a0ba2663ae8a5124a5623dbb4ebb5e Mon Sep 17 00:00:00 2001 From: kenji tomita Date: Mon, 6 Nov 2017 23:38:18 +0900 Subject: [PATCH] Add samples for iterator-related extensions (KT-20357) --- .../test/samples/collections/iterators.kt | 72 +++++++++++++++++++ .../src/kotlin/collections/Iterators.kt | 4 ++ 2 files changed, 76 insertions(+) create mode 100644 libraries/stdlib/samples/test/samples/collections/iterators.kt diff --git a/libraries/stdlib/samples/test/samples/collections/iterators.kt b/libraries/stdlib/samples/test/samples/collections/iterators.kt new file mode 100644 index 00000000000..b3c0c339718 --- /dev/null +++ b/libraries/stdlib/samples/test/samples/collections/iterators.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package samples.collections + +import samples.Sample +import java.util.* + +class Iterators { + + @Sample + fun iteratorForEnumeration() { + val vector = Vector().apply { + add("RED") + add("GREEN") + add("BLUE") + } + + for (e in vector.elements()) { + println("The element is $e") + } + } + + @Sample + fun iterator() { + val mutableList = mutableListOf(1, 2, 3) + val mutableIterator = mutableList.iterator() + + if (mutableIterator.hasNext()) { + mutableIterator.next() + mutableIterator.remove() + } + + for (e in mutableIterator) { + println("The element is $e") + } + } + + @Sample + fun withIndexIterator() { + val iterator = ('a'..'c').iterator() + + for ((index, value) in iterator.withIndex()) { + println("The element at $index is $value") + } + } + + @Sample + fun forEachIterator() { + val iterator = (1..3).iterator() + if (iterator.hasNext()) { + iterator.next() + } + + iterator.forEach { + println("The element is $it") + } + } +} \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/collections/Iterators.kt b/libraries/stdlib/src/kotlin/collections/Iterators.kt index 2e0dc5ca3ab..c4dcf96eb6a 100644 --- a/libraries/stdlib/src/kotlin/collections/Iterators.kt +++ b/libraries/stdlib/src/kotlin/collections/Iterators.kt @@ -5,6 +5,7 @@ package kotlin.collections /** * Creates an [Iterator] for an [Enumeration], allowing to use it in `for` loops. + * @sample samples.collections.Iterators.iteratorForEnumeration */ @kotlin.jvm.JvmVersion public operator fun java.util.Enumeration.iterator(): Iterator = object : Iterator { @@ -15,6 +16,7 @@ public operator fun java.util.Enumeration.iterator(): Iterator = objec /** * Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop. + * @sample samples.collections.Iterators.iterator */ @kotlin.internal.InlineOnly public inline operator fun Iterator.iterator(): Iterator = this @@ -22,11 +24,13 @@ public inline operator fun Iterator.iterator(): Iterator = this /** * Returns an [Iterator] wrapping each value produced by this [Iterator] with the [IndexedValue], * containing value and it's index. + * @sample samples.collections.Iterators.withIndexIterator */ public fun Iterator.withIndex(): Iterator> = IndexingIterator(this) /** * Performs the given [operation] on each element of this [Iterator]. + * @sample samples.collections.Iterators.forEachIterator */ public inline fun Iterator.forEach(operation: (T) -> Unit) : Unit { for (element in this) operation(element)