jvmMain.kotlin.collections.SequencesJVM.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-stdlib Show documentation
Show all versions of kotlin-stdlib Show documentation
Kotlin Standard Library for JVM
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("SequencesKt")
package kotlin.sequences
/**
* Creates a sequence that returns all values from this enumeration. The sequence is constrained to be iterated only once.
* @sample samples.collections.Sequences.Building.sequenceFromEnumeration
*/
@kotlin.internal.InlineOnly
public inline fun java.util.Enumeration.asSequence(): Sequence = this.iterator().asSequence()
internal actual class ConstrainedOnceSequence actual constructor(sequence: Sequence) : Sequence {
private val sequenceRef = java.util.concurrent.atomic.AtomicReference(sequence)
actual override fun iterator(): Iterator {
val sequence = sequenceRef.getAndSet(null) ?: throw IllegalStateException("This sequence can be consumed only once.")
return sequence.iterator()
}
}