public class ConfigWithOverrides extends Object
| Constructor and Description |
|---|
ConfigWithOverrides(Map<String,Object> configMap,
String moduleName) |
| Modifier and Type | Method and Description |
|---|---|
Map<String,Object> |
getConfigMap()
It returns the underlying config-map without any override applied.
|
<T> Optional<T> |
getOptionalSettingValue(String settingName,
Class<T> expectedType)
It retrieves the value wrapped in the
Optional from underlying configMap checking for
its type. |
<T> T |
getSettingValue(String settingName,
Class<T> expectedType)
It retrieves the value from underlying configMap checking for its presence and type.
|
<T> List<T> |
getSettingValueList(String settingName,
Class<T> expectedType)
It retrieves the list of values from underlying configMap checking for its presence and the
type.
|
String |
getWithOverrides(String settingName)
This is a convenience method that calls the
this#getWithOverrides(String, Function)
with a String.valueOf(Object) mapper function. |
String |
getWithOverrides(String settingName,
Function<Object,String> objectToStringMapper)
It returns a specific setting value for settingName.
|
public Map<String,Object> getConfigMap()
this#getWithOverrides(String, Function) or
this#getWithOverrides(String, Function) method.@Nonnull public <T> List<T> getSettingValueList(@Nonnull String settingName, @Nonnull Class<T> expectedType)
IllegalArgumentException. If the type of the setting value is not a List it throws
IllegalArgumentException. If the underlying type of any element in this list does not
match the expectedType, it throws IllegalArgumentException.settingName - - it will be used as a key of underlying configMap.expectedType - - it will be used to check if the type of an actual list element value
matches.@Nonnull public <T> T getSettingValue(@Nonnull String settingName, @Nonnull Class<T> expectedType)
IllegalArgumentException. If the type of the setting value does not match the expectedType, it
throws IllegalArgumentException.settingName - - it will be used as a key of underlying configMap.expectedType - - it will be used to check if the type of an actual setting value matches.@Nonnull public <T> Optional<T> getOptionalSettingValue(@Nonnull String settingName, @Nonnull Class<T> expectedType)
Optional from underlying configMap checking for
its type. If the value for a given settingName does not have a value, it returns Optional.empty() If the type of the setting value does not match the expectedType, it throws
IllegalArgumentException.settingName - - it will be used as a key of underlying configMap.expectedType - - it will be used to check if the type of an actual setting value matches.Optional associated with settingName, matching
expectedType.@Nullable public String getWithOverrides(@Nonnull String settingName, @Nonnull Function<Object,String> objectToStringMapper)
It looks for a setting value in the following order:
1. Java System property (System.getProperty()
2. OS environment variable
3. Underlying config map
When trying to retrieve the system property and environment variable, it will use the full setting name. It will add the module name for which this `ConfigWithOverrides` is created.
For example, if the String moduleName = "m_1" and you are calling this
getWithOverrides() method for settingName = "s" it will firstly try to System.getProperty("m_1.s"), then System.getenv("m_1.s") and finally get the config
from the underlying map using Map.get("s").
Prefixing with module name is done to avoid conflicts of overrides between modules.
Please keep in mind that if you are overriding settings via a System property or OS
environment variable, it will be a String value. If the underlying config map does contain a
value of non String type, and the override is not provided, the objectToStringMapper
function will be called. This function is transforming the object into a String type, and you
can provide a custom for this transformation. If the underlying configMap does not
contain a value for the given settingName, the objectToStringMapper is not called, so you don't
need to handle nulls.
@Nullable public String getWithOverrides(@Nonnull String settingName)
this#getWithOverrides(String, Function)
with a String.valueOf(Object) mapper function.Copyright © 2022. All rights reserved.