Class ConfigUtil

java.lang.Object
org.openhab.core.config.core.ConfigUtil

@NonNullByDefault public class ConfigUtil extends Object
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 Details

    • ConfigUtil

      public ConfigUtil()
  • Method Details

    • getDefaultValueAsCorrectType

      public static @Nullable Object getDefaultValueAsCorrectType(ConfigDescriptionParameter parameter)
      Maps the provided (default) value of the given ConfigDescriptionParameter to 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 return null while logging a warning.
      Parameters:
      parameter - the ConfigDescriptionParameter which default value should be normalized (must not be null)
      Returns:
      the given value as the corresponding Java type or null if the value could not be converted
    • applyDefaultConfiguration

      public static void applyDefaultConfiguration(Configuration configuration, @Nullable ConfigDescription configDescription)
      Applies the default values from a give ConfigDescription to the given Configuration.
      Parameters:
      configuration - the Configuration where the default values should be added (must not be null)
      configDescription - the ConfigDescription where 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 Object normalizeType(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 type
      configDescriptionParameter - 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,Object> normalizeTypes(Map<String,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 the ConfigDescriptionValidator. 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 normalized
      configDescriptions - 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

      public static Object normalizeType(Object value)
      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). Use normalizeType(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