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

com.nhaarman.mockito_kotlin.createinstance.NonNullProvider.kt Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package com.nhaarman.mockito_kotlin.createinstance

import kotlin.reflect.KClass
import java.lang.reflect.Array as JavaArray

interface NonNullProvider {

    fun  createInstance(kClass: KClass): T
}

fun nonNullProvider(): NonNullProvider = NonNullProviderImpl(listOf(NullCaster(), InstanceCreator()))

internal class NonNullProviderImpl(
        private val nonNullProviders: List
) : NonNullProvider {

    override fun  createInstance(kClass: KClass): T {
        return firstNonErroring(
                nonNullProviders.map { { it.createInstance(kClass) } }
        )
    }

    private fun  firstNonErroring(functions: List<() -> T>): T {
        var error: Throwable? = null
        functions.forEach { f ->
            try {
                return f()
            } catch(e: Throwable) {
                error = e
            }
        }
        throw error ?: IllegalStateException()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy