49 lines
1.8 KiB
Kotlin
49 lines
1.8 KiB
Kotlin
package de.pheerai.rcdbquery.dsl.params
|
|
|
|
import de.pheerai.rcdbquery.dataMappings.internal.RcdbParamOption
|
|
import de.pheerai.rcdbquery.dataMappings.searchType.SearchType
|
|
import de.pheerai.rcdbquery.dsl.RcdbParams
|
|
|
|
class ParamsCollector {
|
|
private val multiParams: MutableMap<String, List<RcdbParamOption<Any>>> = mutableMapOf()
|
|
private val singleParams: MutableMap<String, RcdbParamOption<Any>> = mutableMapOf()
|
|
|
|
fun build() = RcdbParams(buildMulti(), buildSingle())
|
|
|
|
/**
|
|
* Creates the Query Params from this builder.
|
|
* If no Search type has been set, this will default to "Coaster"
|
|
*/
|
|
private fun buildMulti() = multiParams.apply {
|
|
// Defaults go here, if any
|
|
}
|
|
.filter { it.value.isNotEmpty() }
|
|
.mapValues { e -> e.value.map { o -> o.paramValue.toString() } }
|
|
.let { MultiParams(it) }
|
|
|
|
private fun buildSingle() = singleParams.apply {
|
|
putIfAbsent(SearchType.paramKey, SearchType.COASTER)
|
|
}
|
|
.mapValues { e -> e.value.paramValue.toString() }
|
|
.let { SingleParams(it) }
|
|
|
|
operator fun set(paramName: String, options: List<RcdbParamOption<Any>>) {
|
|
this.multiParams[paramName] = options
|
|
}
|
|
|
|
operator fun set(paramName: String, option: RcdbParamOption<Any>) {
|
|
if (paramName !in singleParams && paramName !in multiParams) {
|
|
this.singleParams[paramName] = option
|
|
} else {
|
|
throw ParamAlreadySetException("Parameter $paramName already set.")
|
|
}
|
|
}
|
|
|
|
fun getSingle(paramName: String) = this.multiParams[paramName] ?: this.singleParams[paramName]
|
|
fun getMulti(paramName: String) = this.multiParams[paramName]
|
|
|
|
fun keys() = this.multiKeys().plus(singleKeys())
|
|
private fun multiKeys() = this.multiParams.keys.toSet()
|
|
private fun singleKeys() = this.singleParams.keys.toSet()
|
|
}
|