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

tech.harmonysoft.oss.common.template.service.KeyValueConfigurationContext.kt Maven / Gradle / Ivy

package tech.harmonysoft.oss.common.template.service

import tech.harmonysoft.oss.common.data.DataModificationStrategy

/**
 * As defined in [KeyValueConfigurer] documentation, sometimes we need to configure target [DataModificationStrategy]
 * with respect to some info available in runtime. This interface defines contract for such dynamic info.
 */
interface KeyValueConfigurationContext {

    /**
     * Target configuration rules might be defined as below:
     * 1. Take current runtime value for key `key1`
     * 2. If that value is equal to `baseValue1` then use `derivedValue1`
     * 3. If that value is equal to `baseValue2` then use `derivedValue2`
     * 4. Else use `derivedValue3`
     *
     * This method defines contract for obtaining base dynamic value for the given key from the given context.
     */
    fun getByStaticKey(key: K): Any?

    /**
     * We might have a finite state of possible keys (e.g. values of some enum) but want to allow flexible
     * configuration based on some custom key as well.
     *
     * Example: suppose that target [DataModificationStrategy] is a result of database select, essentially
     * a collection of column names to their row values. Keys set is finite here - all columns in that table.
     * Let's assume that we want to modify value for particular column based on some meta-info. For example,
     * use different values when we work with different database types (like oracle vs mysql). We can define
     * such config rules as below:
     *   1. If ` = oracle` then use `oracleValue`
     *   2. If ` = mysql` then use `mysqlValue`
     *   3. Else don't modify the value
     *
     * In this situation `db-type` is a custom dynamic key and target [KeyValueConfigurationContext] implementation
     * should be aware of it
     */
    fun getByDynamicKey(key: String): Any?

    companion object {

        private val EMPTY = object : KeyValueConfigurationContext {
            override fun getByStaticKey(key: Any): Any? {
                return null
            }

            override fun getByDynamicKey(key: String): Any? {
                return null
            }
        }

        @Suppress("UNCHECKED_CAST")
        fun  empty(): KeyValueConfigurationContext {
            return EMPTY as KeyValueConfigurationContext
        }

        fun  wrapStaticData(data: Map): KeyValueConfigurationContext {
            return object : KeyValueConfigurationContext {
                override fun getByStaticKey(key: K): Any? {
                    return data[key]
                }

                override fun getByDynamicKey(key: String): Any? {
                    return null
                }
            }
        }

        fun  wrapDynamicData(data: Map): KeyValueConfigurationContext {
            return object : KeyValueConfigurationContext {
                override fun getByStaticKey(key: K): Any? {
                    return null
                }

                override fun getByDynamicKey(key: String): Any? {
                    return data[key]
                }
            }
        }

        fun  wrap(
            staticKey2data: Map,
            dynamicKey2data: Map
        ): KeyValueConfigurationContext {
            return object : KeyValueConfigurationContext {
                override fun getByStaticKey(key: K): Any? {
                    return staticKey2data[key]
                }

                override fun getByDynamicKey(key: String): Any? {
                    return dynamicKey2data[key]
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy