All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.test.AsserterLookup.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
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()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy