Package org.openhab.core.config.core
Class ConfigUtil
java.lang.Object
org.openhab.core.config.core.ConfigUtil
The configuration admin service provides us with a map of key->values. Values can be any
primitive type like String, int, double as well as the object variants of the number types
like Integer, Double etc. You find normalization utility methods in here to convert all
number Types to BigDecimal and all Collections to List. This works on a best-effort
strategy (e.g. "3" will always end up being a BigDecimal, never a String),
except if a
ConfigDescriptionParameter is given. In the latter case,
a conversion according to the type given in the description is performed.- Author:
- Kai Kreuzer - Initial contribution, Thomas Höfer - Minor changes for type normalization based on config description
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplyDefaultConfiguration(Configuration configuration, @Nullable ConfigDescription configDescription) Applies the default values from a giveConfigDescriptionto the givenConfiguration.static @Nullable ObjectMaps the provided (default) value of the givenConfigDescriptionParameterto the corresponding Java type.static @Nullable ObjectnormalizeType(@Nullable Object value) Normalizes the type of the parameter to the one allowed for configurations.static @Nullable ObjectnormalizeType(@Nullable Object value, @Nullable ConfigDescriptionParameter configDescriptionParameter) Normalizes the type of the parameter to the one allowed for configurations.normalizeTypes(Map<String, @Nullable Object> configuration) Normalizes the types to the ones allowed for configurations.normalizeTypes(Map<String, @Nullable Object> configuration, List<ConfigDescription> configDescriptions) Normalizes the given configuration according to the given config descriptions.
-
Constructor Details
-
ConfigUtil
public ConfigUtil()
-
-
Method Details
-
getDefaultValueAsCorrectType
Maps the provided (default) value of the givenConfigDescriptionParameterto the corresponding Java type. In case the provided (default) value is supposed to be a number and cannot be converted into the target type correctly, this method will returnnullwhile logging a warning.- Parameters:
parameter- theConfigDescriptionParameterwhich default value should be normalized (must not be null)- Returns:
- the default value as the corresponding Java type, or
a
Listof the corresponding Java type if the parameter contains multiple values. Returnsnullif the value could not be converted.
-
applyDefaultConfiguration
public static void applyDefaultConfiguration(Configuration configuration, @Nullable ConfigDescription configDescription) Applies the default values from a giveConfigDescriptionto the givenConfiguration.- Parameters:
configuration- theConfigurationwhere the default values should be added (must not be null)configDescription- theConfigDescriptionwhere the default values are located (may be null, but method won't have any effect then)
-
normalizeTypes
public static Map<String,@Nullable Object> normalizeTypes(Map<String, @Nullable Object> configuration) Normalizes the types to the ones allowed for configurations.- Parameters:
configuration- the configuration that needs to be normalized- Returns:
- normalized configuration
-
normalizeType
public static @Nullable Object normalizeType(@Nullable Object value, @Nullable ConfigDescriptionParameter configDescriptionParameter) Normalizes the type of the parameter to the one allowed for configurations.- Parameters:
value- the value to return as normalized typeconfigDescriptionParameter- the parameter that needs to be normalized- Returns:
- corresponding value as a valid type
- Throws:
IllegalArgumentException- if an invalid type has been given
-
normalizeTypes
public static Map<String,@Nullable Object> normalizeTypes(Map<String, @Nullable Object> configuration, List<ConfigDescription> configDescriptions) Normalizes the given configuration according to the given config descriptions. By doing so, it tries to convert types on a best-effort basis. The result will contain BigDecimals, Strings and Booleans wherever a conversion of similar types was possible. However, it does not check for general correctness of types. This can be done using theConfigDescriptionValidator. If multiple config descriptions are given and a parameter is described several times, then the first one (lower index in the list) wins.- Parameters:
configuration- the configuration to be normalizedconfigDescriptions- the configuration descriptions that should be applied (must not be empty).- Returns:
- the normalized configuration or null if given configuration was null
- Throws:
IllegalArgumentException- if given config description is null
-
normalizeType
Normalizes the type of the parameter to the one allowed for configurations. The conversion is performed 'best-effort' (e.g. "3" will always end up being a BigDecimal, never a String). UsenormalizeType(Object, ConfigDescriptionParameter)to make sure your field type ends up as intended.- Parameters:
value- the value to return as normalized type- Returns:
- corresponding value as a valid type
-