com.h3c.sdn.misc

## Class ConfigUtils

- java.lang.Object
- - com.h3c.sdn.misc.ConfigUtils

-     public class ConfigUtils
      extends Object

  Provides facilities for parsing configuration property values from a configuration map.

  The configuration map is constructed with a `String` key and an `Object` value. The value is read from the configuration map and converted to the proper type.

  If an error occurs reading the desired value from the configuration map,the error is reflected in the log and a default value (provided) will be returned.

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static boolean` | `readBoolean(Map<String,Object> cfg, String key, boolean defaultValue)` Read a boolean value from the configuration map.
    `static int` | `readInt(Map<String,Object> cfg, String key, Validator<Integer> validator, int defaultValue)` Read an integer value from the configuration map.
    `static long` | `readLong(Map<String,Object> cfg, String key, Validator<Long> validator, long defaultValue)` Read a long value from the configuration map.
    `static int` | `readPort(Map<String,Object> cfg, String key, Validator<Integer> validator, int defaultValue)` Read a port number from the configuration map.
    `static String` | `readString(Map<String,Object> cfg, String key, Validator<String> validator, String defaultValue)` Read a string value from the configuration.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### readInt

          public static int readInt(Map<String,Object> cfg,
                    String key,
                    Validator<Integer> validator,
                    int defaultValue)

      Read an integer value from the configuration map.

      The property must be represented in the configuration map as either an Integer, an array of integers or as a String, otherwise the given default value is returned.

      If the property is not found in the configuration map, based on the supplied key, the given default value is returned.

      If the property is represented as an array of integers the given default value is returned.

      If the property is represented as a String, the string is converted to an integer. In the case of an empty string or one that cannot be converted to an integer, the given default value is returned.

      If the supplied validator is not null, the integer read from the configuration map is passed to the validator for additional validation. If the validator does not validate the integer, the default value is returned.

      Parameters:
        `cfg` - the configuration map
        `key` - the key to locate the property in the map
        `validator` - the validator for additional validation
        `defaultValue` - the default value to use
      Returns:
        the positive integer read from the configuration

    - #### readLong

          public static long readLong(Map<String,Object> cfg,
                      String key,
                      Validator<Long> validator,
                      long defaultValue)

      Read a long value from the configuration map.

      The property must be represented in the configuration map as either a Long, an array of longs or as a String, otherwise the given default value is returned.

      If the property is not found in the configuration map, based on the supplied key, the given default value is returned.

      If the property is represented as an array of longs the given default value is returned.

      If the property is represented as a String, the string is converted to a long. In the case of an empty string or one that cannot be converted to a long, the given default value is returned.

      If the property found in the configuration map is less than zero the given default value is returned.

      If the supplied validator is not null, the long read from the configuration map is passed to the validator for additional validation. If the validator does not validate the long, the default value is returned.

      Parameters:
        `cfg` - the configuration map
        `key` - the key to locate the property in the map
        `validator` - the validator for additional validation
        `defaultValue` - the default value to use
      Returns:
        the positive long read from the configuration

    - #### readPort

          public static int readPort(Map<String,Object> cfg,
                     String key,
                     Validator<Integer> validator,
                     int defaultValue)

      Read a port number from the configuration map.

      The property must be represented in the configuration map as either an Integer, an array of integers or as a String, otherwise the given default value is returned.

      If the property is not found in the configuration map, based on the supplied key, the given default value is returned.

      If the property is represented as an array of integers the first entry in the integer array is returned.

      If the property is represented as a String, the string is converted to an integer. In the case of an empty string or one that cannot be converted to an integer, the given default value is returned.

      If the property found in the configuration map will not fit in an unsigned 16-bit field the given default value is returned.

      If the supplied validator is not null, the long read from the configuration map is passed to the validator for additional validation. If the validator does not validate the long, the default value is returned.

      Parameters:
        `cfg` - the configuration map
        `key` - the key to locate the property in the map
        `validator` - the validator for additional validation
        `defaultValue` - the default value to use
      Returns:
        the port number read from the configuration

    - #### readBoolean

          public static boolean readBoolean(Map<String,Object> cfg,
                            String key,
                            boolean defaultValue)

      Read a boolean value from the configuration map.

      The property must be represented in the configuration map as either a Boolean or as a String, otherwise the given default value is returned.

      If the property is not found in the configuration map, based on the supplied key, the given default value is returned.

      If the property is represented as a String, the string is converted to an boolean. In the case of an empty string the given default value is returned.

      Parameters:
        `cfg` - the configuration map
        `key` - the key to locate the property in the map
        `defaultValue` - the default value to use
      Returns:
        the boolean read from the configuration

    - #### readString

          public static String readString(Map<String,Object> cfg,
                          String key,
                          Validator<String> validator,
                          String defaultValue)

      Read a string value from the configuration.

      The property must be represented in the configuration map as either a String or as an array of Strings, otherwise the given default value is returned.

      If the property is not found in the configuration map, based on the supplied key, the given default value is returned.

      If the property is represented as an array of Strings the first entry in the String array is returned.

      If the property is an empty String the given default value is returned.

      If the supplied validator is not null, the long read from the configuration map is passed to the validator for additional validation. If the validator does not validate the long, the default value is returned.

      Parameters:
        `cfg` - the configuration map
        `key` - the key to locate the property in the map
        `validator` - the validator for additional validation
        `defaultValue` - the default value to use
      Returns:
        the string value read from the configuration

Copyright © 2014. All Rights Reserved.