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

io.smooth.constraint.resolution.ConstraintResolutionService.kt Maven / Gradle / Ivy

The newest version!
package io.smooth.constraint.resolution

import io.smooth.constraint.Constraint
import io.smooth.store.Store
import io.smooth.store.memory.PerpetualInMemoryStore
import kotlinx.coroutines.flow.firstOrNull
import javax.inject.Provider
import kotlin.reflect.KClass

internal class ConstraintResolutionService {

    private val resolutionsStore =
        PerpetualInMemoryStore>, MutableList>>>()


    internal suspend fun > addConstraintsResolutions(
        constraint: Constraint
    ) {
        val resolutions = constraint.resolutions() ?: return
        addConstraintsResolutions(constraint::class, resolutions)
    }

    internal suspend fun addConstraintsResolutions(
        constraintClass: KClass>,
        resolutions: List, *>>>
    ) {
        var resolutionsProviders = resolutionsStore.getById(constraintClass).firstOrNull()
        if (resolutionsProviders == null) {
            resolutionsProviders = resolutions.toMutableList()
        } else {
            resolutionsProviders.addAll(resolutions)
        }
        resolutionsStore.save(
            Store.SaveDto(constraintClass, resolutionsProviders)
        )
    }


    internal suspend fun > addConstraintResolution(
        constraintClass: KClass,
        resolutionProvider: Provider>
    ) {
        var resolutionsProviders = resolutionsStore.getById(constraintClass).firstOrNull()
        if (resolutionsProviders == null) {
            resolutionsProviders = arrayListOf()
        }
        resolutionsProviders.add(resolutionProvider)
        resolutionsStore.save(
            Store.SaveDto(constraintClass, resolutionsProviders)
        )
    }

    internal suspend fun > getConstraintResolutions(constraintClass: KClass): List>>? =
        resolutionsStore.getById(constraintClass).firstOrNull()?.map {
            it as Provider>
        }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy