Class ConfigParser


  • @NonNullByDefault
    public final class ConfigParser
    extends Object
    Map an OSGi configuration map Map<String, Object> or type-less value to an individual configuration bean or typed value.
    Author:
    David Graeff - Initial contribution, Jan N. Klug - Extended and refactored to an exposed utility class
    • Method Detail

      • configurationAs

        public static <T> @Nullable T configurationAs​(Map<String,​@Nullable Object> properties,
                                                      Class<T> configurationClass)
        Use this method to automatically map a configuration collection to a Configuration holder object. A common use-case would be within a service. Usage example:
         
           public void modified(Map<String, Object> properties) {
             YourConfig config = ConfigParser.configurationAs(properties, YourConfig.class);
           }
         
         
        Parameters:
        properties - The configuration map.
        configurationClass - The configuration holder class. An instance of this will be created so make sure that a default constructor is available.
        Returns:
        The configuration holder object. All fields that matched a configuration option are set. If a required field is not set, null is returned.
      • valueAsOrElse

        public static <T> T valueAsOrElse​(@Nullable Object value,
                                          Class<T> type,
                                          T defaultValue)
        Convert a value to a given type or return default value
        Parameters:
        value - input value or String representation of that value
        type - desired target class
        defaultValue - default value to be used if conversion fails or input value is null
        Returns:
        the converted value or the default value if value is null or conversion fails
      • valueAs

        public static <T> @Nullable T valueAs​(@Nullable Object value,
                                              Class<T> type)
        Convert a value to a given type
        Parameters:
        value - input value or String representation of that value
        type - desired target class
        Returns:
        the converted value or null if conversion fails or input value is null