Commit ec418017 authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Init implementation of fixed values

parent de397edd
......@@ -91,6 +91,7 @@ class Config(var map: Map[String, Any],
}
protected[config] var notFoundCache: List[ConfigValueIndex] = List()
protected[config] var fixedCache: Map[ConfigValueIndex, ConfigValue] = Map()
protected[config] var foundCache: Map[ConfigValueIndex, ConfigValue] = Map()
protected[config] var defaultCache: Map[ConfigValueIndex, ConfigValue] = Map()
protected[config] def clearCache(): Unit = {
......@@ -112,17 +113,32 @@ class Config(var map: Map[String, Any],
* @param requestedIndex Index to value
* @return True if exist
*/
def contains(requestedIndex: ConfigValueIndex): Boolean =
def contains(requestedIndex: ConfigValueIndex): Boolean = contains(requestedIndex, Map())
/**
* Checks if value exist in config
* @param requestedIndex Index to value
* @param fixedValues Fixed values
* @return True if exist
*/
def contains(requestedIndex: ConfigValueIndex, fixedValues: Map[String, Any]): Boolean =
if (notFoundCache.contains(requestedIndex)) false
else if (fixedCache.contains(requestedIndex)) true
else if (foundCache.contains(requestedIndex)) true
else {
val value = Config.getValueFromMap(map, requestedIndex)
if (value.isDefined && value.get.value != None) {
foundCache += (requestedIndex -> value.get)
val fixedValue = Config.getValueFromMap(fixedValues, requestedIndex)
if (fixedValue.isDefined) {
fixedCache += (requestedIndex -> fixedValue.get)
true
} else {
notFoundCache +:= requestedIndex
false
val value = Config.getValueFromMap(map, requestedIndex)
if (value.isDefined && value.get.value != None) {
foundCache += (requestedIndex -> value.get)
true
} else {
notFoundCache +:= requestedIndex
false
}
}
}
......@@ -134,9 +150,12 @@ class Config(var map: Map[String, Any],
* @param freeVar Default true, if set false value must exist in module
* @return True if exist
*/
def contains(module: String, path: List[String], key: String, freeVar: Boolean = true): Boolean = {
def contains(module: String, path: List[String],
key: String,
freeVar: Boolean = true,
fixedValues: Map[String, Any] = Map()): Boolean = {
val requestedIndex = ConfigValueIndex(module, path, key, freeVar)
contains(requestedIndex)
contains(requestedIndex, fixedValues)
}
/**
......@@ -148,9 +167,14 @@ class Config(var map: Map[String, Any],
* @param freeVar Default true, if set false value must exist in module
* @return Config value
*/
protected[config] def apply(module: String, path: List[String], key: String, default: Any = null, freeVar: Boolean = true): ConfigValue = {
protected[config] def apply(module: String,
path: List[String],
key: String,
default: Any = null,
freeVar: Boolean = true,
fixedValues: Map[String, Any] = Map()): ConfigValue = {
val requestedIndex = ConfigValueIndex(module, path, key, freeVar)
if (contains(requestedIndex)) foundCache(requestedIndex)
if (contains(requestedIndex, fixedValues)) fixedCache.get(requestedIndex).getOrElse(foundCache(requestedIndex))
else if (default != null) {
defaultCache += (requestedIndex -> ConfigValue(requestedIndex, null, default, freeVar))
defaultCache(requestedIndex)
......
......@@ -40,6 +40,12 @@ trait Configurable extends ImplicitConversions {
else globalConfig.defaults
}
/** All values found in this map will be skipped from the user config */
def fixedValues: Map[String, Any] = {
if (root != null) root.fixedValues
else Map()
}
val config = new ConfigFunctions
/**
......@@ -93,8 +99,8 @@ trait Configurable extends ImplicitConversions {
val value = Config.getValueFromMap(defaults, ConfigValueIndex(m, p, key, freeVar))
if (value.isDefined) value.get.value else default
}
if (d == null) globalConfig(m, p, key, freeVar = freeVar)
else globalConfig(m, p, key, d, freeVar)
if (d == null) globalConfig(m, p, key, freeVar = freeVar, fixedValues = fixedValues)
else globalConfig(m, p, key, d, freeVar, fixedValues = fixedValues)
}
/**
......@@ -117,7 +123,7 @@ trait Configurable extends ImplicitConversions {
val m = if (submodule != null) submodule else configName
val p = if (path == null) getConfigPath(s, l, submodule) ::: subPath else path
globalConfig.contains(m, p, key, freeVar) || Config.getValueFromMap(defaults, ConfigValueIndex(m, p, key, freeVar)).isDefined
globalConfig.contains(m, p, key, freeVar, fixedValues) || Config.getValueFromMap(defaults, ConfigValueIndex(m, p, key, freeVar)).isDefined
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment