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

org.ooverkommelig.jvmreflect.retrievabledefinitions.JvmReflectRetrievableDefinitions.kt Maven / Gradle / Ivy

There is a newer version: 1beta4
Show newest version
package org.ooverkommelig.jvmreflect.retrievabledefinitions

import org.ooverkommelig.Definition
import org.ooverkommelig.DefinitionCriteria
import org.ooverkommelig.RetrievableDefinitions
import org.ooverkommelig.SubGraphDefinition
import kotlin.reflect.KProperty
import kotlin.reflect.full.isSubtypeOf

internal class JvmReflectRetrievableDefinitions(private val owner: SubGraphDefinition) : RetrievableDefinitions {
    private val definitionProperties = mutableListOf()

    override fun addDefinitionProperty(property: KProperty<*>, returnsSameObjectForAllRetrievals: Boolean) {
        @Suppress("UNCHECKED_CAST")
        definitionProperties += DefinitionProperty(property as KProperty>, returnsSameObjectForAllRetrievals)
    }

    @Suppress("ConvertCallChainIntoSequence")
    override fun  transitiveRetrievableDefinitions(criteria: DefinitionCriteria) =
            definitionProperties.filter { candidateDefinitionProperty ->
                candidateDefinitionProperty.type.isSubtypeOf(criteria.getType())
                        && (!criteria.mustReturnSameObjectForAllRetrievals
                        || candidateDefinitionProperty.returnsSameObjectForAllRetrievals)
            }.map { (definitionProperty) ->
                @Suppress("UNCHECKED_CAST")
                definitionProperty.getter.call(owner) as Definition
            }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy