kotlin.test.AsserterLookup.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
package kotlin.test
import java.util.*
import java.util.concurrent.atomic.*
import java.util.concurrent.locks.*
private val inited = AtomicBoolean()
private val lock = ReentrantLock()
private val contributors = ArrayList()
internal fun lookup(): Asserter {
initContributorsIfNeeded()
for (contributor in contributors) {
val asserter = contributor.contribute()
if (asserter != null) {
return asserter
}
}
return DefaultAsserter()
}
private fun initContributors() {
contributors.clear()
val loader = ServiceLoader.load(AsserterContributor::class.java)
for (contributor in loader) {
if (contributor != null) {
contributors.add(contributor)
}
}
}
private fun initContributorsIfNeeded() {
if (!inited.get()) {
lock.withLock {
if (inited.compareAndSet(false, true)) {
initContributors()
}
}
}
}
private inline fun Lock.withLock(block: () -> Unit) {
lockInterruptibly()
try {
block()
} finally {
unlock()
}
}